winecoreaudio: Implement widOpen and widClose.
[wine/multimedia.git] / dlls / advpack / install.c
blob0c12d9db2f9a4c173a9f06f0ebea0c8e05f8dbaa
1 /*
2 * Advpack install functions
4 * Copyright 2006 James Hawkins
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.
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.
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
21 #include <stdarg.h>
22 #include <stdlib.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winver.h"
29 #include "winternl.h"
30 #include "winnls.h"
31 #include "setupapi.h"
32 #include "advpub.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35 #include "advpack_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
39 #define SPAPI_ERROR 0xE0000000L
40 #define SPAPI_PREFIX 0x800F0000L
41 #define SPAPI_MASK 0xFFFFL
42 #define HRESULT_FROM_SPAPI(x) ((x & SPAPI_MASK) | SPAPI_PREFIX)
44 #define ADV_HRESULT(x) ((x & SPAPI_ERROR) ? HRESULT_FROM_SPAPI(x) : HRESULT_FROM_WIN32(x))
46 #define ADV_SUCCESS 0
47 #define ADV_FAILURE 1
49 /* contains information about a specific install instance */
50 typedef struct _ADVInfo
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;
61 typedef HRESULT (*iterate_fields_func)(HINF hinf, PCWSTR field, void *arg);
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
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
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
77 /* Advanced INF callbacks */
78 static HRESULT del_dirs_callback(HINF hinf, PCWSTR field, void *arg)
80 INFCONTEXT context;
81 HRESULT hr = S_OK;
82 DWORD size;
84 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
86 for (; ok; ok = SetupFindNextLine(&context, &context))
88 WCHAR directory[MAX_INF_STRING_LENGTH];
90 if (!SetupGetLineTextW(&context, NULL, NULL, NULL, directory,
91 MAX_INF_STRING_LENGTH, &size))
92 continue;
94 if (DelNodeW(directory, ADN_DEL_IF_EMPTY))
95 hr = E_FAIL;
98 return hr;
101 static HRESULT per_user_install_callback(HINF hinf, PCWSTR field, void *arg)
103 PERUSERSECTIONW per_user;
104 INFCONTEXT context;
105 DWORD size;
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};
115 per_user.bRollback = FALSE;
116 per_user.dwIsInstalled = 0;
118 SetupGetLineTextW(NULL, hinf, field, disp_name, per_user.szDispName,
119 sizeof(per_user.szDispName) / sizeof(WCHAR), &size);
121 SetupGetLineTextW(NULL, hinf, field, version, per_user.szVersion,
122 sizeof(per_user.szVersion) / sizeof(WCHAR), &size);
124 if (SetupFindFirstLineW(hinf, field, is_installed, &context))
126 SetupGetIntField(&context, 1, (PINT)&per_user.dwIsInstalled);
129 SetupGetLineTextW(NULL, hinf, field, comp_id, per_user.szCompID,
130 sizeof(per_user.szCompID) / sizeof(WCHAR), &size);
132 SetupGetLineTextW(NULL, hinf, field, guid, per_user.szGUID,
133 sizeof(per_user.szGUID) / sizeof(WCHAR), &size);
135 SetupGetLineTextW(NULL, hinf, field, locale, per_user.szLocale,
136 sizeof(per_user.szLocale) / sizeof(WCHAR), &size);
138 SetupGetLineTextW(NULL, hinf, field, stub_path, per_user.szStub,
139 sizeof(per_user.szStub) / sizeof(WCHAR), &size);
141 return SetPerUserSecValuesW(&per_user);
144 static HRESULT register_ocxs_callback(HINF hinf, PCWSTR field, void *arg)
146 HMODULE hm;
147 INFCONTEXT context;
148 HRESULT hr = S_OK;
150 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
152 for (; ok; ok = SetupFindNextLine(&context, &context))
154 WCHAR buffer[MAX_INF_STRING_LENGTH];
156 /* get OCX filename */
157 if (!SetupGetStringFieldW(&context, 1, buffer,
158 sizeof(buffer) / sizeof(WCHAR), NULL))
159 continue;
161 hm = LoadLibraryExW(buffer, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
162 if (!hm)
163 continue;
165 if (do_ocx_reg(hm, TRUE))
166 hr = E_FAIL;
168 FreeLibrary(hm);
171 return hr;
174 static HRESULT run_setup_commands_callback(HINF hinf, PCWSTR field, void *arg)
176 ADVInfo *info = (ADVInfo *)arg;
177 INFCONTEXT context;
178 HRESULT hr = S_OK;
179 DWORD size;
181 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
183 for (; ok; ok = SetupFindNextLine(&context, &context))
185 WCHAR buffer[MAX_INF_STRING_LENGTH];
187 if (!SetupGetLineTextW(&context, NULL, NULL, NULL, buffer,
188 MAX_INF_STRING_LENGTH, &size))
189 continue;
191 if (launch_exe(buffer, info->working_dir, NULL))
192 hr = E_FAIL;
195 return hr;
198 /* sequentially returns pointers to parameters in a parameter list
199 * returns NULL if the parameter is empty, e.g. one,,three */
200 LPWSTR get_parameter(LPWSTR *params, WCHAR separator)
202 LPWSTR token = *params;
204 if (!*params)
205 return NULL;
207 *params = strchrW(*params, separator);
208 if (*params)
209 *(*params)++ = '\0';
211 if (!*token)
212 return NULL;
214 return token;
217 static BOOL is_full_path(LPWSTR path)
219 const int MIN_PATH_LEN = 3;
221 if (!path || lstrlenW(path) < MIN_PATH_LEN)
222 return FALSE;
224 if ((path[1] == ':' && path[2] == '\\') || (path[0] == '\\' && path[1] == '\\'))
225 return TRUE;
227 return FALSE;
230 /* retrieves the contents of a field, dynamically growing the buffer if necessary */
231 static WCHAR *get_field_string(INFCONTEXT *context, DWORD index, WCHAR *buffer,
232 WCHAR *static_buffer, DWORD *size)
234 DWORD required;
236 if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
238 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
240 /* now grow the buffer */
241 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
242 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, required*sizeof(WCHAR)))) return NULL;
243 *size = required;
244 if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
247 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
248 return NULL;
251 /* iterates over all fields of a certain key of a certain section */
252 static HRESULT iterate_section_fields(HINF hinf, PCWSTR section, PCWSTR key,
253 iterate_fields_func callback, void *arg)
255 WCHAR static_buffer[200];
256 WCHAR *buffer = static_buffer;
257 DWORD size = sizeof(static_buffer) / sizeof(WCHAR);
258 INFCONTEXT context;
259 HRESULT hr = E_FAIL;
261 BOOL ok = SetupFindFirstLineW(hinf, section, key, &context);
262 while (ok)
264 UINT i, count = SetupGetFieldCount(&context);
266 for (i = 1; i <= count; i++)
268 if (!(buffer = get_field_string(&context, i, buffer, static_buffer, &size)))
269 goto done;
271 if ((hr = callback(hinf, buffer, arg)) != S_OK)
272 goto done;
275 ok = SetupFindNextMatchLineW(&context, key, &context);
278 hr = S_OK;
280 done:
281 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
282 return hr;
285 static HRESULT check_admin_rights(ADVInfo *info)
287 INT check;
288 INFCONTEXT context;
289 HRESULT hr = S_OK;
291 if (!SetupFindFirstLineW(info->hinf, info->install_sec,
292 CheckAdminRights, &context))
293 return S_OK;
295 if (!SetupGetIntField(&context, 1, &check))
296 return S_OK;
298 if (check == 1)
299 hr = IsNTAdmin(0, NULL) ? S_OK : E_FAIL;
301 return hr;
304 /* performs a setupapi-level install of the INF file */
305 static HRESULT spapi_install(ADVInfo *info)
307 BOOL ret;
308 HRESULT res;
309 PVOID context;
311 context = SetupInitDefaultQueueCallbackEx(NULL, INVALID_HANDLE_VALUE, 0, 0, NULL);
312 if (!context)
313 return ADV_HRESULT(GetLastError());
315 ret = SetupInstallFromInfSectionW(NULL, info->hinf, info->install_sec,
316 SPINST_FILES, NULL, info->working_dir,
317 SP_COPY_NEWER, SetupDefaultQueueCallbackW,
318 context, NULL, NULL);
319 if (!ret)
321 res = ADV_HRESULT(GetLastError());
322 SetupTermDefaultQueueCallback(context);
324 return res;
327 SetupTermDefaultQueueCallback(context);
329 ret = SetupInstallFromInfSectionW(NULL, info->hinf, info->install_sec,
330 SPINST_INIFILES | SPINST_REGISTRY | SPINST_REGSVR,
331 HKEY_LOCAL_MACHINE, NULL, 0,
332 NULL, NULL, NULL, NULL);
333 if (!ret)
334 return ADV_HRESULT(GetLastError());
336 return S_OK;
339 /* processes the Advanced INF commands */
340 static HRESULT adv_install(ADVInfo *info)
342 HRESULT hr;
344 hr = check_admin_rights(info);
345 if (hr != S_OK)
346 return hr;
348 hr = iterate_section_fields(info->hinf, info->install_sec, RunPreSetupCommands,
349 run_setup_commands_callback, info);
350 if (hr != S_OK)
351 return hr;
353 hr = iterate_section_fields(info->hinf, info->install_sec,
354 RegisterOCXs, register_ocxs_callback, NULL);
355 if (hr != S_OK)
356 return hr;
358 hr = iterate_section_fields(info->hinf, info->install_sec,
359 PerUserInstall, per_user_install_callback, info);
360 if (hr != S_OK)
361 return hr;
363 hr = iterate_section_fields(info->hinf, info->install_sec, RunPostSetupCommands,
364 run_setup_commands_callback, info);
365 if (hr != S_OK)
366 return hr;
368 hr = iterate_section_fields(info->hinf, info->install_sec,
369 DelDirs, del_dirs_callback, info);
370 if (hr != S_OK)
371 return hr;
373 return hr;
376 /* determines the proper working directory for the INF file */
377 static HRESULT get_working_dir(ADVInfo *info, LPCWSTR inf_filename, LPCWSTR working_dir)
379 WCHAR path[MAX_PATH];
380 LPCWSTR ptr;
381 DWORD len;
383 static const WCHAR backslash[] = {'\\',0};
384 static const WCHAR inf_dir[] = {'\\','I','N','F',0};
386 if ((ptr = strrchrW(inf_filename, '\\')))
388 len = ptr - inf_filename + 1;
389 ptr = inf_filename;
391 else if (working_dir && *working_dir)
393 len = lstrlenW(working_dir) + 1;
394 ptr = working_dir;
396 else
398 GetCurrentDirectoryW(MAX_PATH, path);
399 lstrcatW(path, backslash);
400 lstrcatW(path, inf_filename);
402 /* check if the INF file is in the current directory */
403 if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
405 GetCurrentDirectoryW(MAX_PATH, path);
407 else
409 /* default to the windows\inf directory if all else fails */
410 GetWindowsDirectoryW(path, MAX_PATH);
411 lstrcatW(path, inf_dir);
414 len = lstrlenW(path) + 1;
415 ptr = path;
418 info->working_dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
419 if (!info->working_dir)
420 return E_OUTOFMEMORY;
422 lstrcpynW(info->working_dir, ptr, len);
424 return S_OK;
427 /* loads the INF file and performs checks on it */
428 static HRESULT install_init(LPCWSTR inf_filename, LPCWSTR install_sec,
429 LPCWSTR working_dir, DWORD flags, ADVInfo *info)
431 DWORD len;
432 HRESULT hr;
433 LPCWSTR ptr, path;
435 static const WCHAR backslash[] = {'\\',0};
436 static const WCHAR default_install[] = {
437 'D','e','f','a','u','l','t','I','n','s','t','a','l','l',0
440 if (!(ptr = strrchrW(inf_filename, '\\')))
441 ptr = inf_filename;
443 len = lstrlenW(ptr);
445 info->inf_filename = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
446 if (!info->inf_filename)
447 return E_OUTOFMEMORY;
449 lstrcpyW(info->inf_filename, ptr);
451 /* FIXME: determine the proper platform to install (NTx86, etc) */
452 if (!install_sec || !*install_sec)
454 len = sizeof(default_install) - 1;
455 ptr = default_install;
457 else
459 len = lstrlenW(install_sec);
460 ptr = install_sec;
463 info->install_sec = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
464 if (!info->install_sec)
465 return E_OUTOFMEMORY;
467 lstrcpyW(info->install_sec, ptr);
469 hr = get_working_dir(info, inf_filename, working_dir);
470 if (FAILED(hr))
471 return hr;
473 len = lstrlenW(info->working_dir) + lstrlenW(info->inf_filename) + 2;
474 info->inf_path = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
475 if (!info->inf_path)
476 return E_OUTOFMEMORY;
478 lstrcpyW(info->inf_path, info->working_dir);
479 lstrcatW(info->inf_path, backslash);
480 lstrcatW(info->inf_path, info->inf_filename);
482 /* RunSetupCommand opens unmodifed filename parameter */
483 if (flags & RSC_FLAG_INF)
484 path = inf_filename;
485 else
486 path = info->inf_path;
488 info->hinf = SetupOpenInfFileW(path, NULL, INF_STYLE_WIN4, NULL);
489 if (info->hinf == INVALID_HANDLE_VALUE)
490 return ADV_HRESULT(GetLastError());
492 set_ldids(info->hinf, info->install_sec, info->working_dir);
494 /* FIXME: check that the INF is advanced */
496 info->flags = flags;
497 info->need_reboot = FALSE;
499 return S_OK;
502 /* release the install instance information */
503 static void install_release(ADVInfo *info)
505 if (info->hinf && info->hinf != INVALID_HANDLE_VALUE)
506 SetupCloseInfFile(info->hinf);
508 HeapFree(GetProcessHeap(), 0, info->inf_path);
509 HeapFree(GetProcessHeap(), 0, info->inf_filename);
510 HeapFree(GetProcessHeap(), 0, info->install_sec);
511 HeapFree(GetProcessHeap(), 0, info->working_dir);
514 /* this structure very closely resembles parameters of RunSetupCommand() */
515 typedef struct
517 HWND hwnd;
518 LPCSTR title;
519 LPCSTR inf_name;
520 LPCSTR dir;
521 LPCSTR section_name;
522 } SETUPCOMMAND_PARAMS;
524 typedef struct
526 HWND hwnd;
527 LPCWSTR title;
528 LPCWSTR inf_name;
529 LPCWSTR dir;
530 LPCWSTR section_name;
531 } SETUPCOMMAND_PARAMSW;
533 /* internal: see DoInfInstall */
534 static HRESULT DoInfInstallW(const SETUPCOMMAND_PARAMSW *setup)
536 ADVInfo info;
537 HRESULT hr;
539 TRACE("(%p)\n", setup);
541 ZeroMemory(&info, sizeof(ADVInfo));
543 hr = install_init(setup->inf_name, setup->section_name, setup->dir, 0, &info);
544 if (hr != S_OK)
545 goto done;
547 hr = spapi_install(&info);
548 if (hr != S_OK)
549 goto done;
551 hr = adv_install(&info);
553 done:
554 install_release(&info);
556 return S_OK;
559 /***********************************************************************
560 * DoInfInstall (ADVPACK.@)
562 * Install an INF section.
564 * PARAMS
565 * setup [I] Structure containing install information.
567 * RETURNS
568 * S_OK Everything OK
569 * HRESULT_FROM_WIN32(GetLastError()) Some other error
571 HRESULT WINAPI DoInfInstall(const SETUPCOMMAND_PARAMS *setup)
573 UNICODE_STRING title, inf, section, dir;
574 SETUPCOMMAND_PARAMSW params;
575 HRESULT hr;
577 if (!setup)
578 return E_INVALIDARG;
580 RtlCreateUnicodeStringFromAsciiz(&title, setup->title);
581 RtlCreateUnicodeStringFromAsciiz(&inf, setup->inf_name);
582 RtlCreateUnicodeStringFromAsciiz(&section, setup->section_name);
583 RtlCreateUnicodeStringFromAsciiz(&dir, setup->dir);
585 params.title = title.Buffer;
586 params.inf_name = inf.Buffer;
587 params.section_name = section.Buffer;
588 params.dir = dir.Buffer;
589 params.hwnd = setup->hwnd;
591 hr = DoInfInstallW(&params);
593 RtlFreeUnicodeString(&title);
594 RtlFreeUnicodeString(&inf);
595 RtlFreeUnicodeString(&section);
596 RtlFreeUnicodeString(&dir);
598 return hr;
601 /***********************************************************************
602 * ExecuteCabA (ADVPACK.@)
604 * See ExecuteCabW.
606 HRESULT WINAPI ExecuteCabA(HWND hwnd, CABINFOA* pCab, LPVOID pReserved)
608 UNICODE_STRING cab, inf, section;
609 CABINFOW cabinfo;
610 HRESULT hr;
612 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
614 if (!pCab)
615 return E_INVALIDARG;
617 if (pCab->pszCab)
619 RtlCreateUnicodeStringFromAsciiz(&cab, pCab->pszCab);
620 cabinfo.pszCab = cab.Buffer;
622 else
623 cabinfo.pszCab = NULL;
625 RtlCreateUnicodeStringFromAsciiz(&inf, pCab->pszInf);
626 RtlCreateUnicodeStringFromAsciiz(&section, pCab->pszSection);
628 MultiByteToWideChar(CP_ACP, 0, pCab->szSrcPath, -1, cabinfo.szSrcPath,
629 sizeof(cabinfo.szSrcPath) / sizeof(WCHAR));
631 cabinfo.pszInf = inf.Buffer;
632 cabinfo.pszSection = section.Buffer;
633 cabinfo.dwFlags = pCab->dwFlags;
635 hr = ExecuteCabW(hwnd, &cabinfo, pReserved);
637 if (pCab->pszCab)
638 RtlFreeUnicodeString(&cab);
640 RtlFreeUnicodeString(&inf);
641 RtlFreeUnicodeString(&section);
643 return hr;
646 /***********************************************************************
647 * ExecuteCabW (ADVPACK.@)
649 * Installs the INF file extracted from a specified cabinet file.
651 * PARAMS
652 * hwnd [I] Handle to the window used for the display.
653 * pCab [I] Information about the cabinet file.
654 * pReserved [I] Reserved. Must be NULL.
656 * RETURNS
657 * Success: S_OK.
658 * Failure: E_FAIL.
660 HRESULT WINAPI ExecuteCabW(HWND hwnd, CABINFOW* pCab, LPVOID pReserved)
662 ADVInfo info;
663 HRESULT hr;
665 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
667 ZeroMemory(&info, sizeof(ADVInfo));
669 if (pCab->pszCab && *pCab->pszCab)
670 FIXME("Cab archive not extracted!\n");
672 hr = install_init(pCab->pszInf, pCab->pszSection, pCab->szSrcPath, pCab->dwFlags, &info);
673 if (hr != S_OK)
674 goto done;
676 hr = spapi_install(&info);
677 if (hr != S_OK)
678 goto done;
680 hr = adv_install(&info);
682 done:
683 install_release(&info);
685 return hr;
688 /***********************************************************************
689 * LaunchINFSectionA (ADVPACK.@)
691 * See LaunchINFSectionW.
693 INT WINAPI LaunchINFSectionA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
695 UNICODE_STRING cmd;
696 HRESULT hr;
698 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
700 if (!cmdline)
701 return ADV_FAILURE;
703 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
705 hr = LaunchINFSectionW(hWnd, hInst, cmd.Buffer, show);
707 RtlFreeUnicodeString(&cmd);
709 return hr;
712 /***********************************************************************
713 * LaunchINFSectionW (ADVPACK.@)
715 * Installs an INF section without BACKUP/ROLLBACK capabilities.
717 * PARAMS
718 * hWnd [I] Handle to parent window, NULL for desktop.
719 * hInst [I] Instance of the process.
720 * cmdline [I] Contains parameters in the order INF,section,flags,reboot.
721 * show [I] How the window should be shown.
723 * RETURNS
724 * Success: ADV_SUCCESS.
725 * Failure: ADV_FAILURE.
727 * NOTES
728 * INF - Filename of the INF to launch.
729 * section - INF section to install.
730 * flags - see advpub.h.
731 * reboot - smart reboot behavior
732 * 'A' Always reboot.
733 * 'I' Reboot if needed (default).
734 * 'N' No reboot.
736 INT WINAPI LaunchINFSectionW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
738 ADVInfo info;
739 LPWSTR cmdline_copy, cmdline_ptr;
740 LPWSTR inf_filename, install_sec;
741 LPWSTR str_flags;
742 DWORD flags = 0;
743 HRESULT hr = S_OK;
745 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
747 if (!cmdline)
748 return ADV_FAILURE;
750 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
751 cmdline_ptr = cmdline_copy;
752 lstrcpyW(cmdline_copy, cmdline);
754 inf_filename = get_parameter(&cmdline_ptr, ',');
755 install_sec = get_parameter(&cmdline_ptr, ',');
757 str_flags = get_parameter(&cmdline_ptr, ',');
758 if (str_flags)
759 flags = atolW(str_flags);
761 ZeroMemory(&info, sizeof(ADVInfo));
763 hr = install_init(inf_filename, install_sec, NULL, flags, &info);
764 if (hr != S_OK)
765 goto done;
767 hr = spapi_install(&info);
768 if (hr != S_OK)
769 goto done;
771 hr = adv_install(&info);
773 done:
774 install_release(&info);
775 HeapFree(GetProcessHeap(), 0, cmdline_copy);
777 return SUCCEEDED(hr) ? ADV_SUCCESS : ADV_FAILURE;
780 /***********************************************************************
781 * LaunchINFSectionExA (ADVPACK.@)
783 * See LaunchINFSectionExW.
785 HRESULT WINAPI LaunchINFSectionExA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
787 UNICODE_STRING cmd;
788 HRESULT hr;
790 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
792 if (!cmdline)
793 return ADV_FAILURE;
795 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
797 hr = LaunchINFSectionExW(hWnd, hInst, cmd.Buffer, show);
799 RtlFreeUnicodeString(&cmd);
801 return hr;
804 /***********************************************************************
805 * LaunchINFSectionExW (ADVPACK.@)
807 * Installs an INF section with BACKUP/ROLLBACK capabilities.
809 * PARAMS
810 * hWnd [I] Handle to parent window, NULL for desktop.
811 * hInst [I] Instance of the process.
812 * cmdline [I] Contains parameters in the order INF,section,CAB,flags,reboot.
813 * show [I] How the window should be shown.
815 * RETURNS
816 * Success: ADV_SUCCESS.
817 * Failure: ADV_FAILURE.
819 * NOTES
820 * INF - Filename of the INF to launch.
821 * section - INF section to install.
822 * flags - see advpub.h.
823 * reboot - smart reboot behavior
824 * 'A' Always reboot.
825 * 'I' Reboot if needed (default).
826 * 'N' No reboot.
828 * BUGS
829 * Doesn't handle the reboot flag.
831 HRESULT WINAPI LaunchINFSectionExW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
833 LPWSTR cmdline_copy, cmdline_ptr;
834 LPWSTR flags, ptr;
835 CABINFOW cabinfo;
836 HRESULT hr;
838 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
840 if (!cmdline)
841 return ADV_FAILURE;
843 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
844 cmdline_ptr = cmdline_copy;
845 lstrcpyW(cmdline_copy, cmdline);
847 cabinfo.pszInf = get_parameter(&cmdline_ptr, ',');
848 cabinfo.pszSection = get_parameter(&cmdline_ptr, ',');
849 cabinfo.pszCab = get_parameter(&cmdline_ptr, ',');
850 *cabinfo.szSrcPath = '\0';
852 flags = get_parameter(&cmdline_ptr, ',');
853 if (flags)
854 cabinfo.dwFlags = atolW(flags);
856 if (!is_full_path(cabinfo.pszCab) && !is_full_path(cabinfo.pszInf))
858 HeapFree(GetProcessHeap(), 0, cmdline_copy);
859 return E_INVALIDARG;
862 /* get the source path from the cab filename */
863 if (cabinfo.pszCab && *cabinfo.pszCab)
865 if (!is_full_path(cabinfo.pszCab))
866 lstrcpyW(cabinfo.szSrcPath, cabinfo.pszInf);
867 else
868 lstrcpyW(cabinfo.szSrcPath, cabinfo.pszCab);
870 ptr = strrchrW(cabinfo.szSrcPath, '\\');
871 *(++ptr) = '\0';
874 hr = ExecuteCabW(hWnd, &cabinfo, NULL);
875 HeapFree(GetProcessHeap(), 0, cmdline_copy);
876 return SUCCEEDED(hr) ? ADV_SUCCESS : ADV_FAILURE;
879 HRESULT launch_exe(LPCWSTR cmd, LPCWSTR dir, HANDLE *phEXE)
881 STARTUPINFOW si;
882 PROCESS_INFORMATION pi;
884 if (phEXE) *phEXE = NULL;
886 ZeroMemory(&pi, sizeof(pi));
887 ZeroMemory(&si, sizeof(si));
888 si.cb = sizeof(si);
890 if (!CreateProcessW(NULL, (LPWSTR)cmd, NULL, NULL, FALSE,
891 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP,
892 NULL, dir, &si, &pi))
894 return HRESULT_FROM_WIN32(GetLastError());
897 CloseHandle(pi.hThread);
899 if (phEXE)
901 *phEXE = pi.hProcess;
902 return S_ASYNCHRONOUS;
905 /* wait for the child process to finish */
906 WaitForSingleObject(pi.hProcess, INFINITE);
907 CloseHandle(pi.hProcess);
909 return S_OK;
912 /***********************************************************************
913 * RunSetupCommandA (ADVPACK.@)
915 * See RunSetupCommandW.
917 HRESULT WINAPI RunSetupCommandA(HWND hWnd, LPCSTR szCmdName,
918 LPCSTR szInfSection, LPCSTR szDir,
919 LPCSTR lpszTitle, HANDLE *phEXE,
920 DWORD dwFlags, LPVOID pvReserved)
922 UNICODE_STRING cmdname, infsec;
923 UNICODE_STRING dir, title;
924 HRESULT hr;
926 TRACE("(%p, %s, %s, %s, %s, %p, %d, %p)\n",
927 hWnd, debugstr_a(szCmdName), debugstr_a(szInfSection),
928 debugstr_a(szDir), debugstr_a(lpszTitle),
929 phEXE, dwFlags, pvReserved);
931 if (!szCmdName || !szDir)
932 return E_INVALIDARG;
934 RtlCreateUnicodeStringFromAsciiz(&cmdname, szCmdName);
935 RtlCreateUnicodeStringFromAsciiz(&infsec, szInfSection);
936 RtlCreateUnicodeStringFromAsciiz(&dir, szDir);
937 RtlCreateUnicodeStringFromAsciiz(&title, lpszTitle);
939 hr = RunSetupCommandW(hWnd, cmdname.Buffer, infsec.Buffer, dir.Buffer,
940 title.Buffer, phEXE, dwFlags, pvReserved);
942 RtlFreeUnicodeString(&cmdname);
943 RtlFreeUnicodeString(&infsec);
944 RtlFreeUnicodeString(&dir);
945 RtlFreeUnicodeString(&title);
947 return hr;
950 /***********************************************************************
951 * RunSetupCommandW (ADVPACK.@)
953 * Executes an install section in an INF file or a program.
955 * PARAMS
956 * hWnd [I] Handle to parent window, NULL for quiet mode
957 * szCmdName [I] Inf or EXE filename to execute
958 * szInfSection [I] Inf section to install, NULL for DefaultInstall
959 * szDir [I] Path to extracted files
960 * szTitle [I] Title of all dialogs
961 * phEXE [O] Handle of EXE to wait for
962 * dwFlags [I] Flags; see include/advpub.h
963 * pvReserved [I] Reserved
965 * RETURNS
966 * S_OK Everything OK
967 * S_ASYNCHRONOUS OK, required to wait on phEXE
968 * ERROR_SUCCESS_REBOOT_REQUIRED Reboot required
969 * E_INVALIDARG Invalid argument given
970 * HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION)
971 * Not supported on this Windows version
972 * E_UNEXPECTED Unexpected error
973 * HRESULT_FROM_WIN32(GetLastError()) Some other error
975 HRESULT WINAPI RunSetupCommandW(HWND hWnd, LPCWSTR szCmdName,
976 LPCWSTR szInfSection, LPCWSTR szDir,
977 LPCWSTR lpszTitle, HANDLE *phEXE,
978 DWORD dwFlags, LPVOID pvReserved)
980 ADVInfo info;
981 HRESULT hr;
983 TRACE("(%p, %s, %s, %s, %s, %p, %d, %p)\n",
984 hWnd, debugstr_w(szCmdName), debugstr_w(szInfSection),
985 debugstr_w(szDir), debugstr_w(lpszTitle),
986 phEXE, dwFlags, pvReserved);
988 if (dwFlags & RSC_FLAG_UPDHLPDLLS)
989 FIXME("Unhandled flag: RSC_FLAG_UPDHLPDLLS\n");
991 if (!szCmdName || !szDir)
992 return E_INVALIDARG;
994 if (!(dwFlags & RSC_FLAG_INF))
995 return launch_exe(szCmdName, szDir, phEXE);
997 ZeroMemory(&info, sizeof(ADVInfo));
999 hr = install_init(szCmdName, szInfSection, szDir, dwFlags, &info);
1000 if (hr != S_OK)
1001 goto done;
1003 hr = spapi_install(&info);
1004 if (hr != S_OK)
1005 goto done;
1007 hr = adv_install(&info);
1009 done:
1010 install_release(&info);
1012 return hr;