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