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