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