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