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