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