dcd1dd7b97599c7e9a865962304f1e082fe75c61
[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 <stdarg.h>
22 #include <stdlib.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "winnls.h"
30 #include "setupapi.h"
31 #include "advpub.h"
32 #include "ole2.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35 #include "advpack_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
38
39 #define SPAPI_ERROR 0xE0000000L
40 #define SPAPI_PREFIX 0x800F0000L
41 #define SPAPI_MASK 0xFFFFL
42 #define HRESULT_FROM_SPAPI(x) ((HRESULT)((x & SPAPI_MASK) | SPAPI_PREFIX))
43
44 #define ADV_HRESULT(x) ((x & SPAPI_ERROR) ? HRESULT_FROM_SPAPI(x) : HRESULT_FROM_WIN32(x))
45
46 #define ADV_SUCCESS 0
47 #define ADV_FAILURE 1
48
49 /* contains information about a specific install instance */
50 typedef struct _ADVInfo
51 {
52 HINF hinf;
53 LPWSTR inf_path;
54 LPWSTR inf_filename;
55 LPWSTR install_sec;
56 LPWSTR working_dir;
57 DWORD flags;
58 BOOL need_reboot;
59 } ADVInfo;
60
61 typedef HRESULT (*iterate_fields_func)(HINF hinf, PCWSTR field, const void *arg);
62
63 /* Advanced INF commands */
64 static const WCHAR CheckAdminRights[] = {
65 'C','h','e','c','k','A','d','m','i','n','R','i','g','h','t','s',0
66 };
67 static const WCHAR DelDirs[] = {'D','e','l','D','i','r','s',0};
68 static const WCHAR PerUserInstall[] = {'P','e','r','U','s','e','r','I','n','s','t','a','l','l',0};
69 static const WCHAR RegisterOCXs[] = {'R','e','g','i','s','t','e','r','O','C','X','s',0};
70 static const WCHAR RunPreSetupCommands[] = {
71 'R','u','n','P','r','e','S','e','t','u','p','C','o','m','m','a','n','d','s',0
72 };
73 static const WCHAR RunPostSetupCommands[] = {
74 'R','u','n','P','o','s','t','S','e','t','u','p','C','o','m','m','a','n','d','s',0
75 };
76
77 /* Advanced INF callbacks */
78 static HRESULT del_dirs_callback(HINF hinf, PCWSTR field, const void *arg)
79 {
80 INFCONTEXT context;
81 HRESULT hr = S_OK;
82 DWORD size;
83
84 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
85
86 for (; ok; ok = SetupFindNextLine(&context, &context))
87 {
88 WCHAR directory[MAX_INF_STRING_LENGTH];
89
90 if (!SetupGetLineTextW(&context, NULL, NULL, NULL, directory,
91 MAX_INF_STRING_LENGTH, &size))
92 continue;
93
94 if (DelNodeW(directory, ADN_DEL_IF_EMPTY) != S_OK)
95 hr = E_FAIL;
96 }
97
98 return hr;
99 }
100
101 static HRESULT per_user_install_callback(HINF hinf, PCWSTR field, const void *arg)
102 {
103 PERUSERSECTIONW per_user;
104 INFCONTEXT context;
105 DWORD size;
106
107 static const WCHAR disp_name[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
108 static const WCHAR version[] = {'V','e','r','s','i','o','n',0};
109 static const WCHAR is_installed[] = {'I','s','I','n','s','t','a','l','l','e','d',0};
110 static const WCHAR comp_id[] = {'C','o','m','p','o','n','e','n','t','I','D',0};
111 static const WCHAR guid[] = {'G','U','I','D',0};
112 static const WCHAR locale[] = {'L','o','c','a','l','e',0};
113 static const WCHAR stub_path[] = {'S','t','u','b','P','a','t','h',0};
114
115 per_user.bRollback = FALSE;
116 per_user.dwIsInstalled = 0;
117
118 SetupGetLineTextW(NULL, hinf, field, disp_name, per_user.szDispName,
119 sizeof(per_user.szDispName) / sizeof(WCHAR), &size);
120
121 SetupGetLineTextW(NULL, hinf, field, version, per_user.szVersion,
122 sizeof(per_user.szVersion) / sizeof(WCHAR), &size);
123
124 if (SetupFindFirstLineW(hinf, field, is_installed, &context))
125 {
126 SetupGetIntField(&context, 1, (PINT)&per_user.dwIsInstalled);
127 }
128
129 SetupGetLineTextW(NULL, hinf, field, comp_id, per_user.szCompID,
130 sizeof(per_user.szCompID) / sizeof(WCHAR), &size);
131
132 SetupGetLineTextW(NULL, hinf, field, guid, per_user.szGUID,
133 sizeof(per_user.szGUID) / sizeof(WCHAR), &size);
134
135 SetupGetLineTextW(NULL, hinf, field, locale, per_user.szLocale,
136 sizeof(per_user.szLocale) / sizeof(WCHAR), &size);
137
138 SetupGetLineTextW(NULL, hinf, field, stub_path, per_user.szStub,
139 sizeof(per_user.szStub) / sizeof(WCHAR), &size);
140
141 return SetPerUserSecValuesW(&per_user);
142 }
143
144 static HRESULT register_ocxs_callback(HINF hinf, PCWSTR field, const void *arg)
145 {
146 HMODULE hm;
147 INFCONTEXT context;
148 HRESULT hr = S_OK;
149
150 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
151
152 for (; ok; ok = SetupFindNextLine(&context, &context))
153 {
154 WCHAR buffer[MAX_INF_STRING_LENGTH];
155
156 /* get OCX filename */
157 if (!SetupGetStringFieldW(&context, 1, buffer,
158 sizeof(buffer) / sizeof(WCHAR), NULL))
159 continue;
160
161 hm = LoadLibraryExW(buffer, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
162 if (hm)
163 {
164 if (do_ocx_reg(hm, TRUE, NULL, NULL) != S_OK)
165 hr = E_FAIL;
166
167 FreeLibrary(hm);
168 }
169 else
170 hr = E_FAIL;
171
172 if (FAILED(hr))
173 {
174 /* FIXME: display a message box */
175 break;
176 }
177 }
178
179 return hr;
180 }
181
182 static HRESULT run_setup_commands_callback(HINF hinf, PCWSTR field, const void *arg)
183 {
184 const ADVInfo *info = (const ADVInfo *)arg;
185 INFCONTEXT context;
186 HRESULT hr = S_OK;
187 DWORD size;
188
189 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
190
191 for (; ok; ok = SetupFindNextLine(&context, &context))
192 {
193 WCHAR buffer[MAX_INF_STRING_LENGTH];
194
195 if (!SetupGetLineTextW(&context, NULL, NULL, NULL, buffer,
196 MAX_INF_STRING_LENGTH, &size))
197 continue;
198
199 if (launch_exe(buffer, info->working_dir, NULL) != S_OK)
200 hr = E_FAIL;
201 }
202
203 return hr;
204 }
205
206 /* sequentially returns pointers to parameters in a parameter list
207 * returns NULL if the parameter is empty, e.g. one,,three */
208 LPWSTR get_parameter(LPWSTR *params, WCHAR separator, BOOL quoted)
209 {
210 LPWSTR token = *params;
211
212 if (!*params)
213 return NULL;
214
215 if (quoted && *token == '"')
216 {
217 WCHAR *end = strchrW(token + 1, '"');
218 if (end)
219 {
220 *end = 0;
221 *params = end + 1;
222 token = token + 1;
223 }
224 }
225
226 *params = strchrW(*params, separator);
227 if (*params)
228 *(*params)++ = '\0';
229
230 if (!*token)
231 return NULL;
232
233 return token;
234 }
235
236 static BOOL is_full_path(LPCWSTR path)
237 {
238 const int MIN_PATH_LEN = 3;
239
240 if (!path || lstrlenW(path) < MIN_PATH_LEN)
241 return FALSE;
242
243 if ((path[1] == ':' && path[2] == '\\') || (path[0] == '\\' && path[1] == '\\'))
244 return TRUE;
245
246 return FALSE;
247 }
248
249 /* retrieves the contents of a field, dynamically growing the buffer if necessary */
250 static WCHAR *get_field_string(INFCONTEXT *context, DWORD index, WCHAR *buffer,
251 const WCHAR *static_buffer, DWORD *size)
252 {
253 DWORD required;
254
255 if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
256
257 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
258 {
259 /* now grow the buffer */
260 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
261 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, required*sizeof(WCHAR)))) return NULL;
262 *size = required;
263 if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
264 }
265
266 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
267 return NULL;
268 }
269
270 /* iterates over all fields of a certain key of a certain section */
271 static HRESULT iterate_section_fields(HINF hinf, PCWSTR section, PCWSTR key,
272 iterate_fields_func callback, void *arg)
273 {
274 WCHAR static_buffer[200];
275 WCHAR *buffer = static_buffer;
276 DWORD size = sizeof(static_buffer) / sizeof(WCHAR);
277 INFCONTEXT context;
278 HRESULT hr = E_FAIL;
279
280 BOOL ok = SetupFindFirstLineW(hinf, section, key, &context);
281 while (ok)
282 {
283 UINT i, count = SetupGetFieldCount(&context);
284
285 for (i = 1; i <= count; i++)
286 {
287 if (!(buffer = get_field_string(&context, i, buffer, static_buffer, &size)))
288 goto done;
289
290 if ((hr = callback(hinf, buffer, arg)) != S_OK)
291 goto done;
292 }
293
294 ok = SetupFindNextMatchLineW(&context, key, &context);
295 }
296
297 hr = S_OK;
298
299 done:
300 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
301 return hr;
302 }
303
304 static HRESULT check_admin_rights(const ADVInfo *info)
305 {
306 INT check;
307 INFCONTEXT context;
308 HRESULT hr = S_OK;
309
310 if (!SetupFindFirstLineW(info->hinf, info->install_sec,
311 CheckAdminRights, &context))
312 return S_OK;
313
314 if (!SetupGetIntField(&context, 1, &check))
315 return S_OK;
316
317 if (check == 1)
318 hr = IsNTAdmin(0, NULL) ? S_OK : E_FAIL;
319
320 return hr;
321 }
322
323 /* performs a setupapi-level install of the INF file */
324 static HRESULT spapi_install(const ADVInfo *info)
325 {
326 BOOL ret;
327 HRESULT res;
328 PVOID context;
329
330 context = SetupInitDefaultQueueCallbackEx(NULL, INVALID_HANDLE_VALUE, 0, 0, NULL);
331 if (!context)
332 return ADV_HRESULT(GetLastError());
333
334 ret = SetupInstallFromInfSectionW(NULL, info->hinf, info->install_sec,
335 SPINST_FILES, NULL, info->working_dir,
336 SP_COPY_NEWER, SetupDefaultQueueCallbackW,
337 context, NULL, NULL);
338 if (!ret)
339 {
340 res = ADV_HRESULT(GetLastError());
341 SetupTermDefaultQueueCallback(context);
342
343 return res;
344 }
345
346 SetupTermDefaultQueueCallback(context);
347
348 ret = SetupInstallFromInfSectionW(NULL, info->hinf, info->install_sec,
349 SPINST_INIFILES | SPINST_REGISTRY | SPINST_REGSVR,
350 HKEY_LOCAL_MACHINE, NULL, 0,
351 NULL, NULL, NULL, NULL);
352 if (!ret)
353 return ADV_HRESULT(GetLastError());
354
355 return S_OK;
356 }
357
358 /* processes the Advanced INF commands */
359 static HRESULT adv_install(ADVInfo *info)
360 {
361 HRESULT hr;
362
363 hr = check_admin_rights(info);
364 if (hr != S_OK)
365 return hr;
366
367 hr = iterate_section_fields(info->hinf, info->install_sec, RunPreSetupCommands,
368 run_setup_commands_callback, info);
369 if (hr != S_OK)
370 return hr;
371
372 OleInitialize(NULL);
373 hr = iterate_section_fields(info->hinf, info->install_sec,
374 RegisterOCXs, register_ocxs_callback, NULL);
375 OleUninitialize();
376 if (hr != S_OK)
377 return hr;
378
379 hr = iterate_section_fields(info->hinf, info->install_sec,
380 PerUserInstall, per_user_install_callback, info);
381 if (hr != S_OK)
382 return hr;
383
384 hr = iterate_section_fields(info->hinf, info->install_sec, RunPostSetupCommands,
385 run_setup_commands_callback, info);
386 if (hr != S_OK)
387 return hr;
388
389 hr = iterate_section_fields(info->hinf, info->install_sec,
390 DelDirs, del_dirs_callback, info);
391 if (hr != S_OK)
392 return hr;
393
394 return hr;
395 }
396
397 /* determines the proper working directory for the INF file */
398 static HRESULT get_working_dir(ADVInfo *info, LPCWSTR inf_filename, LPCWSTR working_dir)
399 {
400 WCHAR path[MAX_PATH];
401 LPCWSTR ptr;
402 DWORD len;
403
404 static const WCHAR backslash[] = {'\\',0};
405 static const WCHAR inf_dir[] = {'\\','I','N','F',0};
406
407 if ((ptr = strrchrW(inf_filename, '\\')))
408 {
409 len = ptr - inf_filename + 1;
410 ptr = inf_filename;
411 }
412 else if (working_dir && *working_dir)
413 {
414 len = lstrlenW(working_dir) + 1;
415 ptr = working_dir;
416 }
417 else
418 {
419 GetCurrentDirectoryW(MAX_PATH, path);
420 lstrcatW(path, backslash);
421 lstrcatW(path, inf_filename);
422
423 /* check if the INF file is in the current directory */
424 if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
425 {
426 GetCurrentDirectoryW(MAX_PATH, path);
427 }
428 else
429 {
430 /* default to the windows\inf directory if all else fails */
431 GetWindowsDirectoryW(path, MAX_PATH);
432 lstrcatW(path, inf_dir);
433 }
434
435 len = lstrlenW(path) + 1;
436 ptr = path;
437 }
438
439 info->working_dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
440 if (!info->working_dir)
441 return E_OUTOFMEMORY;
442
443 lstrcpynW(info->working_dir, ptr, len);
444
445 return S_OK;
446 }
447
448 /* loads the INF file and performs checks on it */
449 static HRESULT install_init(LPCWSTR inf_filename, LPCWSTR install_sec,
450 LPCWSTR working_dir, DWORD flags, ADVInfo *info)
451 {
452 DWORD len;
453 HRESULT hr;
454 LPCWSTR ptr, path;
455
456 static const WCHAR backslash[] = {'\\',0};
457 static const WCHAR default_install[] = {
458 'D','e','f','a','u','l','t','I','n','s','t','a','l','l',0
459 };
460
461 if (!(ptr = strrchrW(inf_filename, '\\')))
462 ptr = inf_filename;
463
464 len = lstrlenW(ptr);
465
466 info->inf_filename = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
467 if (!info->inf_filename)
468 return E_OUTOFMEMORY;
469
470 lstrcpyW(info->inf_filename, ptr);
471
472 /* FIXME: determine the proper platform to install (NTx86, etc) */
473 if (!install_sec || !*install_sec)
474 {
475 len = sizeof(default_install) - 1;
476 ptr = default_install;
477 }
478 else
479 {
480 len = lstrlenW(install_sec);
481 ptr = install_sec;
482 }
483
484 info->install_sec = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
485 if (!info->install_sec)
486 return E_OUTOFMEMORY;
487
488 lstrcpyW(info->install_sec, ptr);
489
490 hr = get_working_dir(info, inf_filename, working_dir);
491 if (FAILED(hr))
492 return hr;
493
494 len = lstrlenW(info->working_dir) + lstrlenW(info->inf_filename) + 2;
495 info->inf_path = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
496 if (!info->inf_path)
497 return E_OUTOFMEMORY;
498
499 lstrcpyW(info->inf_path, info->working_dir);
500 lstrcatW(info->inf_path, backslash);
501 lstrcatW(info->inf_path, info->inf_filename);
502
503 /* RunSetupCommand opens unmodifed filename parameter */
504 if (flags & RSC_FLAG_INF)
505 path = inf_filename;
506 else
507 path = info->inf_path;
508
509 info->hinf = SetupOpenInfFileW(path, NULL, INF_STYLE_WIN4, NULL);
510 if (info->hinf == INVALID_HANDLE_VALUE)
511 return ADV_HRESULT(GetLastError());
512
513 set_ldids(info->hinf, info->install_sec, info->working_dir);
514
515 /* FIXME: check that the INF is advanced */
516
517 info->flags = flags;
518 info->need_reboot = FALSE;
519
520 return S_OK;
521 }
522
523 /* release the install instance information */
524 static void install_release(const ADVInfo *info)
525 {
526 SetupCloseInfFile(info->hinf);
527
528 HeapFree(GetProcessHeap(), 0, info->inf_path);
529 HeapFree(GetProcessHeap(), 0, info->inf_filename);
530 HeapFree(GetProcessHeap(), 0, info->install_sec);
531 HeapFree(GetProcessHeap(), 0, info->working_dir);
532 }
533
534 /* this structure very closely resembles parameters of RunSetupCommand() */
535 typedef struct
536 {
537 HWND hwnd;
538 LPCSTR title;
539 LPCSTR inf_name;
540 LPCSTR dir;
541 LPCSTR section_name;
542 } SETUPCOMMAND_PARAMS;
543
544 typedef struct
545 {
546 HWND hwnd;
547 LPCWSTR title;
548 LPCWSTR inf_name;
549 LPCWSTR dir;
550 LPCWSTR section_name;
551 } SETUPCOMMAND_PARAMSW;
552
553 /* internal: see DoInfInstall */
554 static HRESULT DoInfInstallW(const SETUPCOMMAND_PARAMSW *setup)
555 {
556 ADVInfo info;
557 HRESULT hr;
558
559 TRACE("(%p)\n", setup);
560
561 ZeroMemory(&info, sizeof(ADVInfo));
562
563 hr = install_init(setup->inf_name, setup->section_name, setup->dir, 0, &info);
564 if (hr != S_OK)
565 goto done;
566
567 hr = spapi_install(&info);
568 if (hr != S_OK)
569 goto done;
570
571 hr = adv_install(&info);
572
573 done:
574 install_release(&info);
575
576 return S_OK;
577 }
578
579 /***********************************************************************
580 * DoInfInstall (ADVPACK.@)
581 *
582 * Install an INF section.
583 *
584 * PARAMS
585 * setup [I] Structure containing install information.
586 *
587 * RETURNS
588 * S_OK Everything OK
589 * HRESULT_FROM_WIN32(GetLastError()) Some other error
590 */
591 HRESULT WINAPI DoInfInstall(const SETUPCOMMAND_PARAMS *setup)
592 {
593 UNICODE_STRING title, inf, section, dir;
594 SETUPCOMMAND_PARAMSW params;
595 HRESULT hr;
596
597 if (!setup)
598 return E_INVALIDARG;
599
600 RtlCreateUnicodeStringFromAsciiz(&title, setup->title);
601 RtlCreateUnicodeStringFromAsciiz(&inf, setup->inf_name);
602 RtlCreateUnicodeStringFromAsciiz(&section, setup->section_name);
603 RtlCreateUnicodeStringFromAsciiz(&dir, setup->dir);
604
605 params.title = title.Buffer;
606 params.inf_name = inf.Buffer;
607 params.section_name = section.Buffer;
608 params.dir = dir.Buffer;
609 params.hwnd = setup->hwnd;
610
611 hr = DoInfInstallW(&params);
612
613 RtlFreeUnicodeString(&title);
614 RtlFreeUnicodeString(&inf);
615 RtlFreeUnicodeString(&section);
616 RtlFreeUnicodeString(&dir);
617
618 return hr;
619 }
620
621 /***********************************************************************
622 * ExecuteCabA (ADVPACK.@)
623 *
624 * See ExecuteCabW.
625 */
626 HRESULT WINAPI ExecuteCabA(HWND hwnd, CABINFOA* pCab, LPVOID pReserved)
627 {
628 UNICODE_STRING cab, inf, section;
629 CABINFOW cabinfo;
630 HRESULT hr;
631
632 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
633
634 if (!pCab)
635 return E_INVALIDARG;
636
637 if (pCab->pszCab)
638 {
639 RtlCreateUnicodeStringFromAsciiz(&cab, pCab->pszCab);
640 cabinfo.pszCab = cab.Buffer;
641 }
642 else
643 cabinfo.pszCab = NULL;
644
645 RtlCreateUnicodeStringFromAsciiz(&inf, pCab->pszInf);
646 RtlCreateUnicodeStringFromAsciiz(&section, pCab->pszSection);
647
648 MultiByteToWideChar(CP_ACP, 0, pCab->szSrcPath, -1, cabinfo.szSrcPath,
649 sizeof(cabinfo.szSrcPath) / sizeof(WCHAR));
650
651 cabinfo.pszInf = inf.Buffer;
652 cabinfo.pszSection = section.Buffer;
653 cabinfo.dwFlags = pCab->dwFlags;
654
655 hr = ExecuteCabW(hwnd, &cabinfo, pReserved);
656
657 if (pCab->pszCab)
658 RtlFreeUnicodeString(&cab);
659
660 RtlFreeUnicodeString(&inf);
661 RtlFreeUnicodeString(&section);
662
663 return hr;
664 }
665
666 /***********************************************************************
667 * ExecuteCabW (ADVPACK.@)
668 *
669 * Installs the INF file extracted from a specified cabinet file.
670 *
671 * PARAMS
672 * hwnd [I] Handle to the window used for the display.
673 * pCab [I] Information about the cabinet file.
674 * pReserved [I] Reserved. Must be NULL.
675 *
676 * RETURNS
677 * Success: S_OK.
678 * Failure: E_FAIL.
679 */
680 HRESULT WINAPI ExecuteCabW(HWND hwnd, CABINFOW* pCab, LPVOID pReserved)
681 {
682 ADVInfo info;
683 HRESULT hr;
684
685 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
686
687 ZeroMemory(&info, sizeof(ADVInfo));
688
689 if (pCab->pszCab && *pCab->pszCab)
690 FIXME("Cab archive not extracted!\n");
691
692 hr = install_init(pCab->pszInf, pCab->pszSection, pCab->szSrcPath, pCab->dwFlags, &info);
693 if (hr != S_OK)
694 goto done;
695
696 hr = spapi_install(&info);
697 if (hr != S_OK)
698 goto done;
699
700 hr = adv_install(&info);
701
702 done:
703 install_release(&info);
704
705 return hr;
706 }
707
708 /***********************************************************************
709 * LaunchINFSectionA (ADVPACK.@)
710 *
711 * See LaunchINFSectionW.
712 */
713 INT WINAPI LaunchINFSectionA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
714 {
715 UNICODE_STRING cmd;
716 HRESULT hr;
717
718 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
719
720 if (!cmdline)
721 return ADV_FAILURE;
722
723 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
724
725 hr = LaunchINFSectionW(hWnd, hInst, cmd.Buffer, show);
726
727 RtlFreeUnicodeString(&cmd);
728
729 return hr;
730 }
731
732 /***********************************************************************
733 * LaunchINFSectionW (ADVPACK.@)
734 *
735 * Installs an INF section without BACKUP/ROLLBACK capabilities.
736 *
737 * PARAMS
738 * hWnd [I] Handle to parent window, NULL for desktop.
739 * hInst [I] Instance of the process.
740 * cmdline [I] Contains parameters in the order INF,section,flags,reboot.
741 * show [I] How the window should be shown.
742 *
743 * RETURNS
744 * Success: ADV_SUCCESS.
745 * Failure: ADV_FAILURE.
746 *
747 * NOTES
748 * INF - Filename of the INF to launch.
749 * section - INF section to install.
750 * flags - see advpub.h.
751 * reboot - smart reboot behavior
752 * 'A' Always reboot.
753 * 'I' Reboot if needed (default).
754 * 'N' No reboot.
755 */
756 INT WINAPI LaunchINFSectionW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
757 {
758 ADVInfo info;
759 LPWSTR cmdline_copy, cmdline_ptr;
760 LPWSTR inf_filename, install_sec;
761 LPWSTR str_flags;
762 DWORD flags = 0;
763 HRESULT hr = S_OK;
764
765 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
766
767 if (!cmdline)
768 return ADV_FAILURE;
769
770 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
771 cmdline_ptr = cmdline_copy;
772 lstrcpyW(cmdline_copy, cmdline);
773
774 inf_filename = get_parameter(&cmdline_ptr, ',', TRUE);
775 install_sec = get_parameter(&cmdline_ptr, ',', TRUE);
776
777 str_flags = get_parameter(&cmdline_ptr, ',', TRUE);
778 if (str_flags)
779 {
780 DWORD inf_flags = atolW(str_flags);
781 if (inf_flags & LIS_QUIET) flags |= RSC_FLAG_QUIET;
782 if (inf_flags & LIS_NOGRPCONV) flags |= RSC_FLAG_NGCONV;
783 }
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 }