xref: /reactos/dll/win32/advpack/install.c (revision 07b120bd)
1 /*
2  * Advpack install functions
3  *
4  * Copyright 2006 James Hawkins
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include <stdarg.h>
22 #include <stdlib.h>
23 
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "winnls.h"
30 #include "setupapi.h"
31 #include "advpub.h"
32 #include "ole2.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35 #include "advpack_private.h"
36 
37 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
38 
39 #define SPAPI_ERROR     0xE0000000L
40 #define SPAPI_PREFIX    0x800F0000L
41 #define SPAPI_MASK      0xFFFFL
42 #define HRESULT_FROM_SPAPI(x)   ((HRESULT)((x & SPAPI_MASK) | SPAPI_PREFIX))
43 
44 #define ADV_HRESULT(x)  ((x & SPAPI_ERROR) ? HRESULT_FROM_SPAPI(x) : HRESULT_FROM_WIN32(x))
45 
46 #define ADV_SUCCESS     0
47 #define ADV_FAILURE     1
48 
49 /* contains information about a specific install instance */
50 typedef struct _ADVInfo
51 {
52     HINF hinf;
53     LPWSTR inf_path;
54     LPWSTR inf_filename;
55     LPWSTR install_sec;
56     LPWSTR working_dir;
57     DWORD flags;
58     BOOL need_reboot;
59 } ADVInfo;
60 
61 typedef HRESULT (*iterate_fields_func)(HINF hinf, PCWSTR field, const void *arg);
62 
63 /* Advanced INF commands */
64 static const WCHAR CheckAdminRights[] = {
65     'C','h','e','c','k','A','d','m','i','n','R','i','g','h','t','s',0
66 };
67 static const WCHAR DelDirs[] = {'D','e','l','D','i','r','s',0};
68 static const WCHAR PerUserInstall[] = {'P','e','r','U','s','e','r','I','n','s','t','a','l','l',0};
69 static const WCHAR RegisterOCXs[] = {'R','e','g','i','s','t','e','r','O','C','X','s',0};
70 static const WCHAR RunPreSetupCommands[] = {
71     'R','u','n','P','r','e','S','e','t','u','p','C','o','m','m','a','n','d','s',0
72 };
73 static const WCHAR RunPostSetupCommands[] = {
74     'R','u','n','P','o','s','t','S','e','t','u','p','C','o','m','m','a','n','d','s',0
75 };
76 
77 /* Advanced INF callbacks */
78 static HRESULT del_dirs_callback(HINF hinf, PCWSTR field, const void *arg)
79 {
80     INFCONTEXT context;
81     HRESULT hr = S_OK;
82     DWORD size;
83 
84     BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
85 
86     for (; ok; ok = SetupFindNextLine(&context, &context))
87     {
88         WCHAR directory[MAX_INF_STRING_LENGTH];
89 
90         if (!SetupGetLineTextW(&context, NULL, NULL, NULL, directory,
91                                MAX_INF_STRING_LENGTH, &size))
92             continue;
93 
94         if (DelNodeW(directory, ADN_DEL_IF_EMPTY) != S_OK)
95             hr = E_FAIL;
96     }
97 
98     return hr;
99 }
100 
101 static HRESULT per_user_install_callback(HINF hinf, PCWSTR field, const void *arg)
102 {
103     PERUSERSECTIONW per_user;
104     INFCONTEXT context;
105     DWORD size;
106 
107     static const WCHAR disp_name[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
108     static const WCHAR version[] = {'V','e','r','s','i','o','n',0};
109     static const WCHAR is_installed[] = {'I','s','I','n','s','t','a','l','l','e','d',0};
110     static const WCHAR comp_id[] = {'C','o','m','p','o','n','e','n','t','I','D',0};
111     static const WCHAR guid[] = {'G','U','I','D',0};
112     static const WCHAR locale[] = {'L','o','c','a','l','e',0};
113     static const WCHAR stub_path[] = {'S','t','u','b','P','a','t','h',0};
114 
115     per_user.bRollback = FALSE;
116     per_user.dwIsInstalled = 0;
117 
118     SetupGetLineTextW(NULL, hinf, field, disp_name, per_user.szDispName, ARRAY_SIZE(per_user.szDispName), &size);
119 
120     SetupGetLineTextW(NULL, hinf, field, version, per_user.szVersion, ARRAY_SIZE(per_user.szVersion), &size);
121 
122     if (SetupFindFirstLineW(hinf, field, is_installed, &context))
123     {
124         SetupGetIntField(&context, 1, (PINT)&per_user.dwIsInstalled);
125     }
126 
127     SetupGetLineTextW(NULL, hinf, field, comp_id, per_user.szCompID, ARRAY_SIZE(per_user.szCompID), &size);
128 
129     SetupGetLineTextW(NULL, hinf, field, guid, per_user.szGUID, ARRAY_SIZE(per_user.szGUID), &size);
130 
131     SetupGetLineTextW(NULL, hinf, field, locale, per_user.szLocale, ARRAY_SIZE(per_user.szLocale), &size);
132 
133     SetupGetLineTextW(NULL, hinf, field, stub_path, per_user.szStub, ARRAY_SIZE(per_user.szStub), &size);
134 
135     return SetPerUserSecValuesW(&per_user);
136 }
137 
138 static HRESULT register_ocxs_callback(HINF hinf, PCWSTR field, const void *arg)
139 {
140     HMODULE hm;
141     INFCONTEXT context;
142     HRESULT hr = S_OK;
143 
144     BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
145 
146     for (; ok; ok = SetupFindNextLine(&context, &context))
147     {
148         WCHAR buffer[MAX_INF_STRING_LENGTH];
149 
150         /* get OCX filename */
151         if (!SetupGetStringFieldW(&context, 1, buffer, ARRAY_SIZE(buffer), NULL))
152             continue;
153 
154         hm = LoadLibraryExW(buffer, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
155         if (hm)
156         {
157             if (do_ocx_reg(hm, TRUE, NULL, NULL) != S_OK)
158                 hr = E_FAIL;
159 
160             FreeLibrary(hm);
161         }
162         else
163             hr = E_FAIL;
164 
165         if (FAILED(hr))
166         {
167             /* FIXME: display a message box */
168             break;
169         }
170     }
171 
172     return hr;
173 }
174 
175 static HRESULT run_setup_commands_callback(HINF hinf, PCWSTR field, const void *arg)
176 {
177     const ADVInfo *info = (const ADVInfo *)arg;
178     INFCONTEXT context;
179     HRESULT hr = S_OK;
180     DWORD size;
181 
182     BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
183 
184     for (; ok; ok = SetupFindNextLine(&context, &context))
185     {
186         WCHAR buffer[MAX_INF_STRING_LENGTH];
187 
188         if (!SetupGetLineTextW(&context, NULL, NULL, NULL, buffer,
189                                MAX_INF_STRING_LENGTH, &size))
190             continue;
191 
192         if (launch_exe(buffer, info->working_dir, NULL) != S_OK)
193             hr = E_FAIL;
194     }
195 
196     return hr;
197 }
198 
199 /* sequentially returns pointers to parameters in a parameter list
200  * returns NULL if the parameter is empty, e.g. one,,three  */
201 LPWSTR get_parameter(LPWSTR *params, WCHAR separator, BOOL quoted)
202 {
203     LPWSTR token = *params;
204 
205     if (!*params)
206         return NULL;
207 
208     if (quoted && *token == '"')
209     {
210         WCHAR *end = strchrW(token + 1, '"');
211         if (end)
212         {
213             *end = 0;
214             *params = end + 1;
215             token = token + 1;
216         }
217     }
218 
219     *params = strchrW(*params, separator);
220     if (*params)
221         *(*params)++ = '\0';
222 
223     if (!*token)
224         return NULL;
225 
226     return token;
227 }
228 
229 static BOOL is_full_path(LPCWSTR path)
230 {
231     const int MIN_PATH_LEN = 3;
232 
233     if (!path || lstrlenW(path) < MIN_PATH_LEN)
234         return FALSE;
235 
236     if ((path[1] == ':' && path[2] == '\\') || (path[0] == '\\' && path[1] == '\\'))
237         return TRUE;
238 
239     return FALSE;
240 }
241 
242 /* retrieves the contents of a field, dynamically growing the buffer if necessary */
243 static WCHAR *get_field_string(INFCONTEXT *context, DWORD index, WCHAR *buffer,
244                                const WCHAR *static_buffer, DWORD *size)
245 {
246     DWORD required;
247 
248     if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
249 
250     if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
251     {
252         /* now grow the buffer */
253         if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
254         if (!(buffer = HeapAlloc(GetProcessHeap(), 0, required*sizeof(WCHAR)))) return NULL;
255         *size = required;
256         if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
257     }
258 
259     if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
260     return NULL;
261 }
262 
263 /* iterates over all fields of a certain key of a certain section */
264 static HRESULT iterate_section_fields(HINF hinf, PCWSTR section, PCWSTR key,
265                                       iterate_fields_func callback, void *arg)
266 {
267     WCHAR static_buffer[200];
268     WCHAR *buffer = static_buffer;
269     DWORD size = ARRAY_SIZE(static_buffer);
270     INFCONTEXT context;
271     HRESULT hr = E_FAIL;
272 
273     BOOL ok = SetupFindFirstLineW(hinf, section, key, &context);
274     while (ok)
275     {
276         UINT i, count = SetupGetFieldCount(&context);
277 
278         for (i = 1; i <= count; i++)
279         {
280             if (!(buffer = get_field_string(&context, i, buffer, static_buffer, &size)))
281                 goto done;
282 
283             if ((hr = callback(hinf, buffer, arg)) != S_OK)
284                 goto done;
285         }
286 
287         ok = SetupFindNextMatchLineW(&context, key, &context);
288     }
289 
290     hr = S_OK;
291 
292  done:
293     if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
294     return hr;
295 }
296 
297 static HRESULT check_admin_rights(const ADVInfo *info)
298 {
299     INT check;
300     INFCONTEXT context;
301     HRESULT hr = S_OK;
302 
303     if (!SetupFindFirstLineW(info->hinf, info->install_sec,
304                              CheckAdminRights, &context))
305         return S_OK;
306 
307     if (!SetupGetIntField(&context, 1, &check))
308         return S_OK;
309 
310     if (check == 1)
311         hr = IsNTAdmin(0, NULL) ? S_OK : E_FAIL;
312 
313     return hr;
314 }
315 
316 /* performs a setupapi-level install of the INF file */
317 static HRESULT spapi_install(const ADVInfo *info)
318 {
319     BOOL ret;
320     HRESULT res;
321     PVOID context;
322 
323     context = SetupInitDefaultQueueCallbackEx(NULL, INVALID_HANDLE_VALUE, 0, 0, NULL);
324     if (!context)
325         return ADV_HRESULT(GetLastError());
326 
327     ret = SetupInstallFromInfSectionW(NULL, info->hinf, info->install_sec,
328                                       SPINST_FILES, NULL, info->working_dir,
329                                       SP_COPY_NEWER, SetupDefaultQueueCallbackW,
330                                       context, NULL, NULL);
331     if (!ret)
332     {
333         res = ADV_HRESULT(GetLastError());
334         SetupTermDefaultQueueCallback(context);
335 
336         return res;
337     }
338 
339     SetupTermDefaultQueueCallback(context);
340 
341     ret = SetupInstallFromInfSectionW(NULL, info->hinf, info->install_sec,
342                                       SPINST_INIFILES | SPINST_REGISTRY | SPINST_REGSVR,
343                                       HKEY_LOCAL_MACHINE, NULL, 0,
344                                       NULL, NULL, NULL, NULL);
345     if (!ret)
346         return ADV_HRESULT(GetLastError());
347 
348     return S_OK;
349 }
350 
351 /* processes the Advanced INF commands */
352 static HRESULT adv_install(ADVInfo *info)
353 {
354     HRESULT hr;
355 
356     hr = check_admin_rights(info);
357     if (hr != S_OK)
358         return hr;
359 
360     hr = iterate_section_fields(info->hinf, info->install_sec, RunPreSetupCommands,
361                                 run_setup_commands_callback, info);
362     if (hr != S_OK)
363         return hr;
364 
365     OleInitialize(NULL);
366     hr = iterate_section_fields(info->hinf, info->install_sec,
367                                 RegisterOCXs, register_ocxs_callback, NULL);
368     OleUninitialize();
369     if (hr != S_OK)
370         return hr;
371 
372     hr = iterate_section_fields(info->hinf, info->install_sec,
373                                 PerUserInstall, per_user_install_callback, info);
374     if (hr != S_OK)
375         return hr;
376 
377     hr = iterate_section_fields(info->hinf, info->install_sec, RunPostSetupCommands,
378                                 run_setup_commands_callback, info);
379     if (hr != S_OK)
380         return hr;
381 
382     hr = iterate_section_fields(info->hinf, info->install_sec,
383                                 DelDirs, del_dirs_callback, info);
384     if (hr != S_OK)
385         return hr;
386 
387     return hr;
388 }
389 
390 /* determines the proper working directory for the INF file */
391 static HRESULT get_working_dir(ADVInfo *info, LPCWSTR inf_filename, LPCWSTR working_dir)
392 {
393     WCHAR path[MAX_PATH];
394     LPCWSTR ptr;
395     DWORD len;
396 
397     static const WCHAR backslash[] = {'\\',0};
398     static const WCHAR inf_dir[] = {'\\','I','N','F',0};
399 
400     if ((ptr = strrchrW(inf_filename, '\\')))
401     {
402         len = ptr - inf_filename + 1;
403         ptr = inf_filename;
404     }
405     else if (working_dir && *working_dir)
406     {
407         len = lstrlenW(working_dir) + 1;
408         ptr = working_dir;
409     }
410     else
411     {
412         GetCurrentDirectoryW(MAX_PATH, path);
413         lstrcatW(path, backslash);
414         lstrcatW(path, inf_filename);
415 
416         /* check if the INF file is in the current directory */
417         if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
418         {
419             GetCurrentDirectoryW(MAX_PATH, path);
420         }
421         else
422         {
423             /* default to the windows\inf directory if all else fails */
424             GetWindowsDirectoryW(path, MAX_PATH);
425             lstrcatW(path, inf_dir);
426         }
427 
428         len = lstrlenW(path) + 1;
429         ptr = path;
430     }
431 
432     info->working_dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
433     if (!info->working_dir)
434         return E_OUTOFMEMORY;
435 
436     lstrcpynW(info->working_dir, ptr, len);
437 
438     return S_OK;
439 }
440 
441 /* loads the INF file and performs checks on it */
442 static HRESULT install_init(LPCWSTR inf_filename, LPCWSTR install_sec,
443                             LPCWSTR working_dir, DWORD flags, ADVInfo *info)
444 {
445     DWORD len;
446     HRESULT hr;
447     LPCWSTR ptr, path;
448 
449     static const WCHAR backslash[] = {'\\',0};
450     static const WCHAR default_install[] = {
451         'D','e','f','a','u','l','t','I','n','s','t','a','l','l',0
452     };
453 
454     if (!(ptr = strrchrW(inf_filename, '\\')))
455         ptr = inf_filename;
456 
457     len = lstrlenW(ptr);
458 
459     info->inf_filename = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
460     if (!info->inf_filename)
461         return E_OUTOFMEMORY;
462 
463     lstrcpyW(info->inf_filename, ptr);
464 
465     /* FIXME: determine the proper platform to install (NTx86, etc) */
466     if (!install_sec || !*install_sec)
467     {
468         len = sizeof(default_install) - 1;
469         ptr = default_install;
470     }
471     else
472     {
473         len = lstrlenW(install_sec);
474         ptr = install_sec;
475     }
476 
477     info->install_sec = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
478     if (!info->install_sec)
479         return E_OUTOFMEMORY;
480 
481     lstrcpyW(info->install_sec, ptr);
482 
483     hr = get_working_dir(info, inf_filename, working_dir);
484     if (FAILED(hr))
485         return hr;
486 
487     len = lstrlenW(info->working_dir) + lstrlenW(info->inf_filename) + 2;
488     info->inf_path = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
489     if (!info->inf_path)
490         return E_OUTOFMEMORY;
491 
492     lstrcpyW(info->inf_path, info->working_dir);
493     lstrcatW(info->inf_path, backslash);
494     lstrcatW(info->inf_path, info->inf_filename);
495 
496     /* RunSetupCommand opens unmodifed filename parameter */
497     if (flags & RSC_FLAG_INF)
498         path = inf_filename;
499     else
500         path = info->inf_path;
501 
502     info->hinf = SetupOpenInfFileW(path, NULL, INF_STYLE_WIN4, NULL);
503     if (info->hinf == INVALID_HANDLE_VALUE)
504         return ADV_HRESULT(GetLastError());
505 
506     set_ldids(info->hinf, info->install_sec, info->working_dir);
507 
508     /* FIXME: check that the INF is advanced */
509 
510     info->flags = flags;
511     info->need_reboot = FALSE;
512 
513     return S_OK;
514 }
515 
516 /* release the install instance information */
517 static void install_release(const ADVInfo *info)
518 {
519     SetupCloseInfFile(info->hinf);
520 
521     HeapFree(GetProcessHeap(), 0, info->inf_path);
522     HeapFree(GetProcessHeap(), 0, info->inf_filename);
523     HeapFree(GetProcessHeap(), 0, info->install_sec);
524     HeapFree(GetProcessHeap(), 0, info->working_dir);
525 }
526 
527 /* this structure very closely resembles parameters of RunSetupCommand() */
528 typedef struct
529 {
530     HWND hwnd;
531     LPCSTR title;
532     LPCSTR inf_name;
533     LPCSTR dir;
534     LPCSTR section_name;
535 } SETUPCOMMAND_PARAMS;
536 
537 typedef struct
538 {
539     HWND hwnd;
540     LPCWSTR title;
541     LPCWSTR inf_name;
542     LPCWSTR dir;
543     LPCWSTR section_name;
544 } SETUPCOMMAND_PARAMSW;
545 
546 /* internal: see DoInfInstall */
547 static HRESULT DoInfInstallW(const SETUPCOMMAND_PARAMSW *setup)
548 {
549     ADVInfo info;
550     HRESULT hr;
551 
552     TRACE("(%p)\n", setup);
553 
554     ZeroMemory(&info, sizeof(ADVInfo));
555 
556     hr = install_init(setup->inf_name, setup->section_name, setup->dir, 0, &info);
557     if (hr != S_OK)
558         goto done;
559 
560     hr = spapi_install(&info);
561     if (hr != S_OK)
562         goto done;
563 
564     hr = adv_install(&info);
565 
566 done:
567     install_release(&info);
568 
569     return S_OK;
570 }
571 
572 /***********************************************************************
573  *      DoInfInstall  (ADVPACK.@)
574  *
575  * Install an INF section.
576  *
577  * PARAMS
578  *  setup [I] Structure containing install information.
579  *
580  * RETURNS
581  *   S_OK                                Everything OK
582  *   HRESULT_FROM_WIN32(GetLastError())  Some other error
583  */
584 HRESULT WINAPI DoInfInstall(const SETUPCOMMAND_PARAMS *setup)
585 {
586     UNICODE_STRING title, inf, section, dir;
587     SETUPCOMMAND_PARAMSW params;
588     HRESULT hr;
589 
590     if (!setup)
591         return E_INVALIDARG;
592 
593     RtlCreateUnicodeStringFromAsciiz(&title, setup->title);
594     RtlCreateUnicodeStringFromAsciiz(&inf, setup->inf_name);
595     RtlCreateUnicodeStringFromAsciiz(&section, setup->section_name);
596     RtlCreateUnicodeStringFromAsciiz(&dir, setup->dir);
597 
598     params.title = title.Buffer;
599     params.inf_name = inf.Buffer;
600     params.section_name = section.Buffer;
601     params.dir = dir.Buffer;
602     params.hwnd = setup->hwnd;
603 
604     hr = DoInfInstallW(&params);
605 
606     RtlFreeUnicodeString(&title);
607     RtlFreeUnicodeString(&inf);
608     RtlFreeUnicodeString(&section);
609     RtlFreeUnicodeString(&dir);
610 
611     return hr;
612 }
613 
614 /***********************************************************************
615  *             ExecuteCabA    (ADVPACK.@)
616  *
617  * See ExecuteCabW.
618  */
619 HRESULT WINAPI ExecuteCabA(HWND hwnd, CABINFOA* pCab, LPVOID pReserved)
620 {
621     UNICODE_STRING cab, inf, section;
622     CABINFOW cabinfo;
623     HRESULT hr;
624 
625     TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
626 
627     if (!pCab)
628         return E_INVALIDARG;
629 
630     if (pCab->pszCab)
631     {
632         RtlCreateUnicodeStringFromAsciiz(&cab, pCab->pszCab);
633         cabinfo.pszCab = cab.Buffer;
634     }
635     else
636         cabinfo.pszCab = NULL;
637 
638     RtlCreateUnicodeStringFromAsciiz(&inf, pCab->pszInf);
639     RtlCreateUnicodeStringFromAsciiz(&section, pCab->pszSection);
640 
641     MultiByteToWideChar(CP_ACP, 0, pCab->szSrcPath, -1, cabinfo.szSrcPath, ARRAY_SIZE(cabinfo.szSrcPath));
642 
643     cabinfo.pszInf = inf.Buffer;
644     cabinfo.pszSection = section.Buffer;
645     cabinfo.dwFlags = pCab->dwFlags;
646 
647     hr = ExecuteCabW(hwnd, &cabinfo, pReserved);
648 
649     if (pCab->pszCab)
650         RtlFreeUnicodeString(&cab);
651 
652     RtlFreeUnicodeString(&inf);
653     RtlFreeUnicodeString(&section);
654 
655     return hr;
656 }
657 
658 /***********************************************************************
659  *             ExecuteCabW    (ADVPACK.@)
660  *
661  * Installs the INF file extracted from a specified cabinet file.
662  *
663  * PARAMS
664  *   hwnd      [I] Handle to the window used for the display.
665  *   pCab      [I] Information about the cabinet file.
666  *   pReserved [I] Reserved.  Must be NULL.
667  *
668  * RETURNS
669  *   Success: S_OK.
670  *   Failure: E_FAIL.
671  */
672 HRESULT WINAPI ExecuteCabW(HWND hwnd, CABINFOW* pCab, LPVOID pReserved)
673 {
674     ADVInfo info;
675     HRESULT hr;
676 
677     TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
678 
679     ZeroMemory(&info, sizeof(ADVInfo));
680 
681     if (pCab->pszCab && *pCab->pszCab)
682         FIXME("Cab archive not extracted!\n");
683 
684     hr = install_init(pCab->pszInf, pCab->pszSection, pCab->szSrcPath, pCab->dwFlags, &info);
685     if (hr != S_OK)
686         goto done;
687 
688     hr = spapi_install(&info);
689     if (hr != S_OK)
690         goto done;
691 
692     hr = adv_install(&info);
693 
694 done:
695     install_release(&info);
696 
697     return hr;
698 }
699 
700 /***********************************************************************
701  *      LaunchINFSectionA   (ADVPACK.@)
702  *
703  * See LaunchINFSectionW.
704  */
705 INT WINAPI LaunchINFSectionA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
706 {
707     UNICODE_STRING cmd;
708     HRESULT hr;
709 
710     TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
711 
712     if (!cmdline)
713         return ADV_FAILURE;
714 
715     RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
716 
717     hr = LaunchINFSectionW(hWnd, hInst, cmd.Buffer, show);
718 
719     RtlFreeUnicodeString(&cmd);
720 
721     return hr;
722 }
723 
724 /***********************************************************************
725  *      LaunchINFSectionW   (ADVPACK.@)
726  *
727  * Installs an INF section without BACKUP/ROLLBACK capabilities.
728  *
729  * PARAMS
730  *   hWnd    [I] Handle to parent window, NULL for desktop.
731  *   hInst   [I] Instance of the process.
732  *   cmdline [I] Contains parameters in the order INF,section,flags,reboot.
733  *   show    [I] How the window should be shown.
734  *
735  * RETURNS
736  *  Success: ADV_SUCCESS.
737  *  Failure: ADV_FAILURE.
738  *
739  * NOTES
740  *  INF - Filename of the INF to launch.
741  *  section - INF section to install.
742  *  flags - see advpub.h.
743  *  reboot - smart reboot behavior
744  *    'A' Always reboot.
745  *    'I' Reboot if needed (default).
746  *    'N' No reboot.
747  */
748 INT WINAPI LaunchINFSectionW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
749 {
750     ADVInfo info;
751     LPWSTR cmdline_copy, cmdline_ptr;
752     LPWSTR inf_filename, install_sec;
753     LPWSTR str_flags;
754     DWORD flags = 0;
755     HRESULT hr = S_OK;
756 
757     TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
758 
759     if (!cmdline)
760         return ADV_FAILURE;
761 
762     cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
763     cmdline_ptr = cmdline_copy;
764     lstrcpyW(cmdline_copy, cmdline);
765 
766     inf_filename = get_parameter(&cmdline_ptr, ',', TRUE);
767     install_sec = get_parameter(&cmdline_ptr, ',', TRUE);
768 
769     str_flags = get_parameter(&cmdline_ptr, ',', TRUE);
770     if (str_flags)
771     {
772         DWORD inf_flags = atolW(str_flags);
773         if (inf_flags & LIS_QUIET) flags |= RSC_FLAG_QUIET;
774         if (inf_flags & LIS_NOGRPCONV) flags |= RSC_FLAG_NGCONV;
775     }
776 
777     ZeroMemory(&info, sizeof(ADVInfo));
778 
779     hr = install_init(inf_filename, install_sec, NULL, flags, &info);
780     if (hr != S_OK)
781         goto done;
782 
783     hr = spapi_install(&info);
784     if (hr != S_OK)
785         goto done;
786 
787     hr = adv_install(&info);
788 
789 done:
790     install_release(&info);
791     HeapFree(GetProcessHeap(), 0, cmdline_copy);
792 
793     return SUCCEEDED(hr) ? ADV_SUCCESS : ADV_FAILURE;
794 }
795 
796 /***********************************************************************
797  *      LaunchINFSectionExA (ADVPACK.@)
798  *
799  * See LaunchINFSectionExW.
800  */
801 HRESULT WINAPI LaunchINFSectionExA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
802 {
803     UNICODE_STRING cmd;
804     HRESULT hr;
805 
806     TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
807 
808     if (!cmdline)
809         return ADV_FAILURE;
810 
811     RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
812 
813     hr = LaunchINFSectionExW(hWnd, hInst, cmd.Buffer, show);
814 
815     RtlFreeUnicodeString(&cmd);
816 
817     return hr;
818 }
819 
820 /***********************************************************************
821  *      LaunchINFSectionExW (ADVPACK.@)
822  *
823  * Installs an INF section with BACKUP/ROLLBACK capabilities.
824  *
825  * PARAMS
826  *   hWnd    [I] Handle to parent window, NULL for desktop.
827  *   hInst   [I] Instance of the process.
828  *   cmdline [I] Contains parameters in the order INF,section,CAB,flags,reboot.
829  *   show    [I] How the window should be shown.
830  *
831  * RETURNS
832  *  Success: ADV_SUCCESS.
833  *  Failure: ADV_FAILURE.
834  *
835  * NOTES
836  *  INF - Filename of the INF to launch.
837  *  section - INF section to install.
838  *  flags - see advpub.h.
839  *  reboot - smart reboot behavior
840  *    'A' Always reboot.
841  *    'I' Reboot if needed (default).
842  *    'N' No reboot.
843  *
844  * BUGS
845  *  Doesn't handle the reboot flag.
846  */
847 HRESULT WINAPI LaunchINFSectionExW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
848 {
849     LPWSTR cmdline_copy, cmdline_ptr;
850     LPWSTR flags, ptr;
851     CABINFOW cabinfo;
852     HRESULT hr;
853 
854     TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
855 
856     if (!cmdline)
857         return ADV_FAILURE;
858 
859     cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
860     cmdline_ptr = cmdline_copy;
861     lstrcpyW(cmdline_copy, cmdline);
862 
863     cabinfo.pszInf = get_parameter(&cmdline_ptr, ',', TRUE);
864     cabinfo.pszSection = get_parameter(&cmdline_ptr, ',', TRUE);
865     cabinfo.pszCab = get_parameter(&cmdline_ptr, ',', TRUE);
866     *cabinfo.szSrcPath = '\0';
867 
868     flags = get_parameter(&cmdline_ptr, ',', TRUE);
869     if (flags)
870         cabinfo.dwFlags = atolW(flags);
871 
872     if (!is_full_path(cabinfo.pszCab) && !is_full_path(cabinfo.pszInf))
873     {
874         HeapFree(GetProcessHeap(), 0, cmdline_copy);
875         return E_INVALIDARG;
876     }
877 
878     /* get the source path from the cab filename */
879     if (cabinfo.pszCab && *cabinfo.pszCab)
880     {
881         if (!is_full_path(cabinfo.pszCab))
882             lstrcpyW(cabinfo.szSrcPath, cabinfo.pszInf);
883         else
884             lstrcpyW(cabinfo.szSrcPath, cabinfo.pszCab);
885 
886         ptr = strrchrW(cabinfo.szSrcPath, '\\');
887         *(++ptr) = '\0';
888     }
889 
890     hr = ExecuteCabW(hWnd, &cabinfo, NULL);
891     HeapFree(GetProcessHeap(), 0, cmdline_copy);
892     return SUCCEEDED(hr) ? ADV_SUCCESS : ADV_FAILURE;
893 }
894 
895 HRESULT launch_exe(LPCWSTR cmd, LPCWSTR dir, HANDLE *phEXE)
896 {
897     STARTUPINFOW si;
898     PROCESS_INFORMATION pi;
899 
900     if (phEXE) *phEXE = NULL;
901 
902     ZeroMemory(&pi, sizeof(pi));
903     ZeroMemory(&si, sizeof(si));
904     si.cb = sizeof(si);
905 
906     if (!CreateProcessW(NULL, (LPWSTR)cmd, NULL, NULL, FALSE,
907                         CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP,
908                         NULL, dir, &si, &pi))
909     {
910         return HRESULT_FROM_WIN32(GetLastError());
911     }
912 
913     CloseHandle(pi.hThread);
914 
915     if (phEXE)
916     {
917         *phEXE = pi.hProcess;
918         return S_ASYNCHRONOUS;
919     }
920 
921     /* wait for the child process to finish */
922     WaitForSingleObject(pi.hProcess, INFINITE);
923     CloseHandle(pi.hProcess);
924 
925     return S_OK;
926 }
927 
928 /***********************************************************************
929  *      RunSetupCommandA  (ADVPACK.@)
930  *
931  * See RunSetupCommandW.
932  */
933 HRESULT WINAPI RunSetupCommandA(HWND hWnd, LPCSTR szCmdName,
934                                 LPCSTR szInfSection, LPCSTR szDir,
935                                 LPCSTR lpszTitle, HANDLE *phEXE,
936                                 DWORD dwFlags, LPVOID pvReserved)
937 {
938     UNICODE_STRING cmdname, infsec;
939     UNICODE_STRING dir, title;
940     HRESULT hr;
941 
942     TRACE("(%p, %s, %s, %s, %s, %p, %d, %p)\n",
943           hWnd, debugstr_a(szCmdName), debugstr_a(szInfSection),
944           debugstr_a(szDir), debugstr_a(lpszTitle),
945           phEXE, dwFlags, pvReserved);
946 
947     if (!szCmdName || !szDir)
948         return E_INVALIDARG;
949 
950     RtlCreateUnicodeStringFromAsciiz(&cmdname, szCmdName);
951     RtlCreateUnicodeStringFromAsciiz(&infsec, szInfSection);
952     RtlCreateUnicodeStringFromAsciiz(&dir, szDir);
953     RtlCreateUnicodeStringFromAsciiz(&title, lpszTitle);
954 
955     hr = RunSetupCommandW(hWnd, cmdname.Buffer, infsec.Buffer, dir.Buffer,
956                           title.Buffer, phEXE, dwFlags, pvReserved);
957 
958     RtlFreeUnicodeString(&cmdname);
959     RtlFreeUnicodeString(&infsec);
960     RtlFreeUnicodeString(&dir);
961     RtlFreeUnicodeString(&title);
962 
963     return hr;
964 }
965 
966 /***********************************************************************
967  *      RunSetupCommandW  (ADVPACK.@)
968  *
969  * Executes an install section in an INF file or a program.
970  *
971  * PARAMS
972  *   hWnd          [I] Handle to parent window, NULL for quiet mode
973  *   szCmdName     [I] Inf or EXE filename to execute
974  *   szInfSection  [I] Inf section to install, NULL for DefaultInstall
975  *   szDir         [I] Path to extracted files
976  *   szTitle       [I] Title of all dialogs
977  *   phEXE         [O] Handle of EXE to wait for
978  *   dwFlags       [I] Flags; see include/advpub.h
979  *   pvReserved    [I] Reserved
980  *
981  * RETURNS
982  *   S_OK                                 Everything OK
983  *   S_ASYNCHRONOUS                       OK, required to wait on phEXE
984  *   ERROR_SUCCESS_REBOOT_REQUIRED        Reboot required
985  *   E_INVALIDARG                         Invalid argument given
986  *   HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION)
987  *                                        Not supported on this Windows version
988  *   E_UNEXPECTED                         Unexpected error
989  *   HRESULT_FROM_WIN32(GetLastError())   Some other error
990  */
991 HRESULT WINAPI RunSetupCommandW(HWND hWnd, LPCWSTR szCmdName,
992                                 LPCWSTR szInfSection, LPCWSTR szDir,
993                                 LPCWSTR lpszTitle, HANDLE *phEXE,
994                                 DWORD dwFlags, LPVOID pvReserved)
995 {
996     ADVInfo info;
997     HRESULT hr;
998 
999     TRACE("(%p, %s, %s, %s, %s, %p, %d, %p)\n",
1000           hWnd, debugstr_w(szCmdName), debugstr_w(szInfSection),
1001           debugstr_w(szDir), debugstr_w(lpszTitle),
1002           phEXE, dwFlags, pvReserved);
1003 
1004     if (dwFlags & RSC_FLAG_UPDHLPDLLS)
1005         FIXME("Unhandled flag: RSC_FLAG_UPDHLPDLLS\n");
1006 
1007     if (!szCmdName || !szDir)
1008         return E_INVALIDARG;
1009 
1010     if (!(dwFlags & RSC_FLAG_INF))
1011         return launch_exe(szCmdName, szDir, phEXE);
1012 
1013     ZeroMemory(&info, sizeof(ADVInfo));
1014 
1015     hr = install_init(szCmdName, szInfSection, szDir, dwFlags, &info);
1016     if (hr != S_OK)
1017         goto done;
1018 
1019     hr = spapi_install(&info);
1020     if (hr != S_OK)
1021         goto done;
1022 
1023     hr = adv_install(&info);
1024 
1025 done:
1026     install_release(&info);
1027 
1028     return hr;
1029 }
1030