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