dxgi: Return actual swap effect for D3D11 swapchains.
[wine.git] / dlls / advpack / install.c
blobc3caf3ae7eb6680afa457adc81e4946a782a7bd1
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 "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"
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) ((HRESULT)((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, const 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, const 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) != S_OK)
95 hr = E_FAIL;
98 return hr;
101 static HRESULT per_user_install_callback(HINF hinf, PCWSTR field, const 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, ARRAY_SIZE(per_user.szDispName), &size);
120 SetupGetLineTextW(NULL, hinf, field, version, per_user.szVersion, ARRAY_SIZE(per_user.szVersion), &size);
122 if (SetupFindFirstLineW(hinf, field, is_installed, &context))
124 SetupGetIntField(&context, 1, (PINT)&per_user.dwIsInstalled);
127 SetupGetLineTextW(NULL, hinf, field, comp_id, per_user.szCompID, ARRAY_SIZE(per_user.szCompID), &size);
129 SetupGetLineTextW(NULL, hinf, field, guid, per_user.szGUID, ARRAY_SIZE(per_user.szGUID), &size);
131 SetupGetLineTextW(NULL, hinf, field, locale, per_user.szLocale, ARRAY_SIZE(per_user.szLocale), &size);
133 SetupGetLineTextW(NULL, hinf, field, stub_path, per_user.szStub, ARRAY_SIZE(per_user.szStub), &size);
135 return SetPerUserSecValuesW(&per_user);
138 static HRESULT register_ocxs_callback(HINF hinf, PCWSTR field, const void *arg)
140 HMODULE hm;
141 INFCONTEXT context;
142 HRESULT hr = S_OK;
144 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
146 for (; ok; ok = SetupFindNextLine(&context, &context))
148 WCHAR buffer[MAX_INF_STRING_LENGTH];
150 /* get OCX filename */
151 if (!SetupGetStringFieldW(&context, 1, buffer, ARRAY_SIZE(buffer), NULL))
152 continue;
154 hm = LoadLibraryExW(buffer, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
155 if (hm)
157 if (do_ocx_reg(hm, TRUE, NULL, NULL) != S_OK)
158 hr = E_FAIL;
160 FreeLibrary(hm);
162 else
163 hr = E_FAIL;
165 if (FAILED(hr))
167 /* FIXME: display a message box */
168 break;
172 return hr;
175 static HRESULT run_setup_commands_callback(HINF hinf, PCWSTR field, const void *arg)
177 const ADVInfo *info = (const ADVInfo *)arg;
178 INFCONTEXT context;
179 HRESULT hr = S_OK;
180 DWORD size;
182 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
184 for (; ok; ok = SetupFindNextLine(&context, &context))
186 WCHAR buffer[MAX_INF_STRING_LENGTH];
188 if (!SetupGetLineTextW(&context, NULL, NULL, NULL, buffer,
189 MAX_INF_STRING_LENGTH, &size))
190 continue;
192 if (launch_exe(buffer, info->working_dir, NULL) != S_OK)
193 hr = E_FAIL;
196 return hr;
199 /* sequentially returns pointers to parameters in a parameter list
200 * returns NULL if the parameter is empty, e.g. one,,three */
201 LPWSTR get_parameter(LPWSTR *params, WCHAR separator, BOOL quoted)
203 LPWSTR token = *params;
205 if (!*params)
206 return NULL;
208 if (quoted && *token == '"')
210 WCHAR *end = strchrW(token + 1, '"');
211 if (end)
213 *end = 0;
214 *params = end + 1;
215 token = token + 1;
219 *params = strchrW(*params, separator);
220 if (*params)
221 *(*params)++ = '\0';
223 if (!*token)
224 return NULL;
226 return token;
229 static BOOL is_full_path(LPCWSTR path)
231 const int MIN_PATH_LEN = 3;
233 if (!path || lstrlenW(path) < MIN_PATH_LEN)
234 return FALSE;
236 if ((path[1] == ':' && path[2] == '\\') || (path[0] == '\\' && path[1] == '\\'))
237 return TRUE;
239 return FALSE;
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)
246 DWORD required;
248 if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
250 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
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;
259 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
260 return NULL;
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)
267 WCHAR static_buffer[200];
268 WCHAR *buffer = static_buffer;
269 DWORD size = ARRAY_SIZE(static_buffer);
270 INFCONTEXT context;
271 HRESULT hr = E_FAIL;
273 BOOL ok = SetupFindFirstLineW(hinf, section, key, &context);
274 while (ok)
276 UINT i, count = SetupGetFieldCount(&context);
278 for (i = 1; i <= count; i++)
280 if (!(buffer = get_field_string(&context, i, buffer, static_buffer, &size)))
281 goto done;
283 if ((hr = callback(hinf, buffer, arg)) != S_OK)
284 goto done;
287 ok = SetupFindNextMatchLineW(&context, key, &context);
290 hr = S_OK;
292 done:
293 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
294 return hr;
297 static HRESULT check_admin_rights(const ADVInfo *info)
299 INT check;
300 INFCONTEXT context;
301 HRESULT hr = S_OK;
303 if (!SetupFindFirstLineW(info->hinf, info->install_sec,
304 CheckAdminRights, &context))
305 return S_OK;
307 if (!SetupGetIntField(&context, 1, &check))
308 return S_OK;
310 if (check == 1)
311 hr = IsNTAdmin(0, NULL) ? S_OK : E_FAIL;
313 return hr;
316 /* performs a setupapi-level install of the INF file */
317 static HRESULT spapi_install(const ADVInfo *info)
319 BOOL ret;
320 HRESULT res;
321 PVOID context;
323 context = SetupInitDefaultQueueCallbackEx(NULL, INVALID_HANDLE_VALUE, 0, 0, NULL);
324 if (!context)
325 return ADV_HRESULT(GetLastError());
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)
333 res = ADV_HRESULT(GetLastError());
334 SetupTermDefaultQueueCallback(context);
336 return res;
339 SetupTermDefaultQueueCallback(context);
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());
348 return S_OK;
351 /* processes the Advanced INF commands */
352 static HRESULT adv_install(ADVInfo *info)
354 HRESULT hr;
356 hr = check_admin_rights(info);
357 if (hr != S_OK)
358 return hr;
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;
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;
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;
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;
382 hr = iterate_section_fields(info->hinf, info->install_sec,
383 DelDirs, del_dirs_callback, info);
384 if (hr != S_OK)
385 return hr;
387 return hr;
390 /* determines the proper working directory for the INF file */
391 static HRESULT get_working_dir(ADVInfo *info, LPCWSTR inf_filename, LPCWSTR working_dir)
393 WCHAR path[MAX_PATH];
394 LPCWSTR ptr;
395 DWORD len;
397 static const WCHAR backslash[] = {'\\',0};
398 static const WCHAR inf_dir[] = {'\\','I','N','F',0};
400 if ((ptr = strrchrW(inf_filename, '\\')))
402 len = ptr - inf_filename + 1;
403 ptr = inf_filename;
405 else if (working_dir && *working_dir)
407 len = lstrlenW(working_dir) + 1;
408 ptr = working_dir;
410 else
412 GetCurrentDirectoryW(MAX_PATH, path);
413 lstrcatW(path, backslash);
414 lstrcatW(path, inf_filename);
416 /* check if the INF file is in the current directory */
417 if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
419 GetCurrentDirectoryW(MAX_PATH, path);
421 else
423 /* default to the windows\inf directory if all else fails */
424 GetWindowsDirectoryW(path, MAX_PATH);
425 lstrcatW(path, inf_dir);
428 len = lstrlenW(path) + 1;
429 ptr = path;
432 info->working_dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
433 if (!info->working_dir)
434 return E_OUTOFMEMORY;
436 lstrcpynW(info->working_dir, ptr, len);
438 return S_OK;
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)
445 DWORD len;
446 HRESULT hr;
447 LPCWSTR ptr, path;
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
454 if (!(ptr = strrchrW(inf_filename, '\\')))
455 ptr = inf_filename;
457 len = lstrlenW(ptr);
459 info->inf_filename = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
460 if (!info->inf_filename)
461 return E_OUTOFMEMORY;
463 lstrcpyW(info->inf_filename, ptr);
465 /* FIXME: determine the proper platform to install (NTx86, etc) */
466 if (!install_sec || !*install_sec)
468 len = sizeof(default_install) - 1;
469 ptr = default_install;
471 else
473 len = lstrlenW(install_sec);
474 ptr = install_sec;
477 info->install_sec = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
478 if (!info->install_sec)
479 return E_OUTOFMEMORY;
481 lstrcpyW(info->install_sec, ptr);
483 hr = get_working_dir(info, inf_filename, working_dir);
484 if (FAILED(hr))
485 return hr;
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;
492 lstrcpyW(info->inf_path, info->working_dir);
493 lstrcatW(info->inf_path, backslash);
494 lstrcatW(info->inf_path, info->inf_filename);
496 /* RunSetupCommand opens unmodifed filename parameter */
497 if (flags & RSC_FLAG_INF)
498 path = inf_filename;
499 else
500 path = info->inf_path;
502 info->hinf = SetupOpenInfFileW(path, NULL, INF_STYLE_WIN4, NULL);
503 if (info->hinf == INVALID_HANDLE_VALUE)
504 return ADV_HRESULT(GetLastError());
506 set_ldids(info->hinf, info->install_sec, info->working_dir);
508 /* FIXME: check that the INF is advanced */
510 info->flags = flags;
511 info->need_reboot = FALSE;
513 return S_OK;
516 /* release the install instance information */
517 static void install_release(const ADVInfo *info)
519 SetupCloseInfFile(info->hinf);
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);
527 /* this structure very closely resembles parameters of RunSetupCommand() */
528 typedef struct
530 HWND hwnd;
531 LPCSTR title;
532 LPCSTR inf_name;
533 LPCSTR dir;
534 LPCSTR section_name;
535 } SETUPCOMMAND_PARAMS;
537 typedef struct
539 HWND hwnd;
540 LPCWSTR title;
541 LPCWSTR inf_name;
542 LPCWSTR dir;
543 LPCWSTR section_name;
544 } SETUPCOMMAND_PARAMSW;
546 /* internal: see DoInfInstall */
547 static HRESULT DoInfInstallW(const SETUPCOMMAND_PARAMSW *setup)
549 ADVInfo info;
550 HRESULT hr;
552 TRACE("(%p)\n", setup);
554 ZeroMemory(&info, sizeof(ADVInfo));
556 hr = install_init(setup->inf_name, setup->section_name, setup->dir, 0, &info);
557 if (hr != S_OK)
558 goto done;
560 hr = spapi_install(&info);
561 if (hr != S_OK)
562 goto done;
564 hr = adv_install(&info);
566 done:
567 install_release(&info);
569 return S_OK;
572 /***********************************************************************
573 * DoInfInstall (ADVPACK.@)
575 * Install an INF section.
577 * PARAMS
578 * setup [I] Structure containing install information.
580 * RETURNS
581 * S_OK Everything OK
582 * HRESULT_FROM_WIN32(GetLastError()) Some other error
584 HRESULT WINAPI DoInfInstall(const SETUPCOMMAND_PARAMS *setup)
586 UNICODE_STRING title, inf, section, dir;
587 SETUPCOMMAND_PARAMSW params;
588 HRESULT hr;
590 if (!setup)
591 return E_INVALIDARG;
593 RtlCreateUnicodeStringFromAsciiz(&title, setup->title);
594 RtlCreateUnicodeStringFromAsciiz(&inf, setup->inf_name);
595 RtlCreateUnicodeStringFromAsciiz(&section, setup->section_name);
596 RtlCreateUnicodeStringFromAsciiz(&dir, setup->dir);
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;
604 hr = DoInfInstallW(&params);
606 RtlFreeUnicodeString(&title);
607 RtlFreeUnicodeString(&inf);
608 RtlFreeUnicodeString(&section);
609 RtlFreeUnicodeString(&dir);
611 return hr;
614 /***********************************************************************
615 * ExecuteCabA (ADVPACK.@)
617 * See ExecuteCabW.
619 HRESULT WINAPI ExecuteCabA(HWND hwnd, CABINFOA* pCab, LPVOID pReserved)
621 UNICODE_STRING cab, inf, section;
622 CABINFOW cabinfo;
623 HRESULT hr;
625 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
627 if (!pCab)
628 return E_INVALIDARG;
630 if (pCab->pszCab)
632 RtlCreateUnicodeStringFromAsciiz(&cab, pCab->pszCab);
633 cabinfo.pszCab = cab.Buffer;
635 else
636 cabinfo.pszCab = NULL;
638 RtlCreateUnicodeStringFromAsciiz(&inf, pCab->pszInf);
639 RtlCreateUnicodeStringFromAsciiz(&section, pCab->pszSection);
641 MultiByteToWideChar(CP_ACP, 0, pCab->szSrcPath, -1, cabinfo.szSrcPath, ARRAY_SIZE(cabinfo.szSrcPath));
643 cabinfo.pszInf = inf.Buffer;
644 cabinfo.pszSection = section.Buffer;
645 cabinfo.dwFlags = pCab->dwFlags;
647 hr = ExecuteCabW(hwnd, &cabinfo, pReserved);
649 if (pCab->pszCab)
650 RtlFreeUnicodeString(&cab);
652 RtlFreeUnicodeString(&inf);
653 RtlFreeUnicodeString(&section);
655 return hr;
658 /***********************************************************************
659 * ExecuteCabW (ADVPACK.@)
661 * Installs the INF file extracted from a specified cabinet file.
663 * PARAMS
664 * hwnd [I] Handle to the window used for the display.
665 * pCab [I] Information about the cabinet file.
666 * pReserved [I] Reserved. Must be NULL.
668 * RETURNS
669 * Success: S_OK.
670 * Failure: E_FAIL.
672 HRESULT WINAPI ExecuteCabW(HWND hwnd, CABINFOW* pCab, LPVOID pReserved)
674 ADVInfo info;
675 HRESULT hr;
677 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
679 ZeroMemory(&info, sizeof(ADVInfo));
681 if (pCab->pszCab && *pCab->pszCab)
682 FIXME("Cab archive not extracted!\n");
684 hr = install_init(pCab->pszInf, pCab->pszSection, pCab->szSrcPath, pCab->dwFlags, &info);
685 if (hr != S_OK)
686 goto done;
688 hr = spapi_install(&info);
689 if (hr != S_OK)
690 goto done;
692 hr = adv_install(&info);
694 done:
695 install_release(&info);
697 return hr;
700 /***********************************************************************
701 * LaunchINFSectionA (ADVPACK.@)
703 * See LaunchINFSectionW.
705 INT WINAPI LaunchINFSectionA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
707 UNICODE_STRING cmd;
708 HRESULT hr;
710 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
712 if (!cmdline)
713 return ADV_FAILURE;
715 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
717 hr = LaunchINFSectionW(hWnd, hInst, cmd.Buffer, show);
719 RtlFreeUnicodeString(&cmd);
721 return hr;
724 /***********************************************************************
725 * LaunchINFSectionW (ADVPACK.@)
727 * Installs an INF section without BACKUP/ROLLBACK capabilities.
729 * PARAMS
730 * hWnd [I] Handle to parent window, NULL for desktop.
731 * hInst [I] Instance of the process.
732 * cmdline [I] Contains parameters in the order INF,section,flags,reboot.
733 * show [I] How the window should be shown.
735 * RETURNS
736 * Success: ADV_SUCCESS.
737 * Failure: ADV_FAILURE.
739 * NOTES
740 * INF - Filename of the INF to launch.
741 * section - INF section to install.
742 * flags - see advpub.h.
743 * reboot - smart reboot behavior
744 * 'A' Always reboot.
745 * 'I' Reboot if needed (default).
746 * 'N' No reboot.
748 INT WINAPI LaunchINFSectionW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
750 ADVInfo info;
751 LPWSTR cmdline_copy, cmdline_ptr;
752 LPWSTR inf_filename, install_sec;
753 LPWSTR str_flags;
754 DWORD flags = 0;
755 HRESULT hr = S_OK;
757 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
759 if (!cmdline)
760 return ADV_FAILURE;
762 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
763 cmdline_ptr = cmdline_copy;
764 lstrcpyW(cmdline_copy, cmdline);
766 inf_filename = get_parameter(&cmdline_ptr, ',', TRUE);
767 install_sec = get_parameter(&cmdline_ptr, ',', TRUE);
769 str_flags = get_parameter(&cmdline_ptr, ',', TRUE);
770 if (str_flags)
772 DWORD inf_flags = atolW(str_flags);
773 if (inf_flags & LIS_QUIET) flags |= RSC_FLAG_QUIET;
774 if (inf_flags & LIS_NOGRPCONV) flags |= RSC_FLAG_NGCONV;
777 ZeroMemory(&info, sizeof(ADVInfo));
779 hr = install_init(inf_filename, install_sec, NULL, flags, &info);
780 if (hr != S_OK)
781 goto done;
783 hr = spapi_install(&info);
784 if (hr != S_OK)
785 goto done;
787 hr = adv_install(&info);
789 done:
790 install_release(&info);
791 HeapFree(GetProcessHeap(), 0, cmdline_copy);
793 return SUCCEEDED(hr) ? ADV_SUCCESS : ADV_FAILURE;
796 /***********************************************************************
797 * LaunchINFSectionExA (ADVPACK.@)
799 * See LaunchINFSectionExW.
801 HRESULT WINAPI LaunchINFSectionExA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
803 UNICODE_STRING cmd;
804 HRESULT hr;
806 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
808 if (!cmdline)
809 return ADV_FAILURE;
811 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
813 hr = LaunchINFSectionExW(hWnd, hInst, cmd.Buffer, show);
815 RtlFreeUnicodeString(&cmd);
817 return hr;
820 /***********************************************************************
821 * LaunchINFSectionExW (ADVPACK.@)
823 * Installs an INF section with BACKUP/ROLLBACK capabilities.
825 * PARAMS
826 * hWnd [I] Handle to parent window, NULL for desktop.
827 * hInst [I] Instance of the process.
828 * cmdline [I] Contains parameters in the order INF,section,CAB,flags,reboot.
829 * show [I] How the window should be shown.
831 * RETURNS
832 * Success: ADV_SUCCESS.
833 * Failure: ADV_FAILURE.
835 * NOTES
836 * INF - Filename of the INF to launch.
837 * section - INF section to install.
838 * flags - see advpub.h.
839 * reboot - smart reboot behavior
840 * 'A' Always reboot.
841 * 'I' Reboot if needed (default).
842 * 'N' No reboot.
844 * BUGS
845 * Doesn't handle the reboot flag.
847 HRESULT WINAPI LaunchINFSectionExW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
849 LPWSTR cmdline_copy, cmdline_ptr;
850 LPWSTR flags, ptr;
851 CABINFOW cabinfo;
852 HRESULT hr;
854 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
856 if (!cmdline)
857 return ADV_FAILURE;
859 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
860 cmdline_ptr = cmdline_copy;
861 lstrcpyW(cmdline_copy, cmdline);
863 cabinfo.pszInf = get_parameter(&cmdline_ptr, ',', TRUE);
864 cabinfo.pszSection = get_parameter(&cmdline_ptr, ',', TRUE);
865 cabinfo.pszCab = get_parameter(&cmdline_ptr, ',', TRUE);
866 *cabinfo.szSrcPath = '\0';
868 flags = get_parameter(&cmdline_ptr, ',', TRUE);
869 if (flags)
870 cabinfo.dwFlags = atolW(flags);
872 if (!is_full_path(cabinfo.pszCab) && !is_full_path(cabinfo.pszInf))
874 HeapFree(GetProcessHeap(), 0, cmdline_copy);
875 return E_INVALIDARG;
878 /* get the source path from the cab filename */
879 if (cabinfo.pszCab && *cabinfo.pszCab)
881 if (!is_full_path(cabinfo.pszCab))
882 lstrcpyW(cabinfo.szSrcPath, cabinfo.pszInf);
883 else
884 lstrcpyW(cabinfo.szSrcPath, cabinfo.pszCab);
886 ptr = strrchrW(cabinfo.szSrcPath, '\\');
887 *(++ptr) = '\0';
890 hr = ExecuteCabW(hWnd, &cabinfo, NULL);
891 HeapFree(GetProcessHeap(), 0, cmdline_copy);
892 return SUCCEEDED(hr) ? ADV_SUCCESS : ADV_FAILURE;
895 HRESULT launch_exe(LPCWSTR cmd, LPCWSTR dir, HANDLE *phEXE)
897 STARTUPINFOW si;
898 PROCESS_INFORMATION pi;
900 if (phEXE) *phEXE = NULL;
902 ZeroMemory(&pi, sizeof(pi));
903 ZeroMemory(&si, sizeof(si));
904 si.cb = sizeof(si);
906 if (!CreateProcessW(NULL, (LPWSTR)cmd, NULL, NULL, FALSE,
907 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP,
908 NULL, dir, &si, &pi))
910 return HRESULT_FROM_WIN32(GetLastError());
913 CloseHandle(pi.hThread);
915 if (phEXE)
917 *phEXE = pi.hProcess;
918 return S_ASYNCHRONOUS;
921 /* wait for the child process to finish */
922 WaitForSingleObject(pi.hProcess, INFINITE);
923 CloseHandle(pi.hProcess);
925 return S_OK;
928 /***********************************************************************
929 * RunSetupCommandA (ADVPACK.@)
931 * See RunSetupCommandW.
933 HRESULT WINAPI RunSetupCommandA(HWND hWnd, LPCSTR szCmdName,
934 LPCSTR szInfSection, LPCSTR szDir,
935 LPCSTR lpszTitle, HANDLE *phEXE,
936 DWORD dwFlags, LPVOID pvReserved)
938 UNICODE_STRING cmdname, infsec;
939 UNICODE_STRING dir, title;
940 HRESULT hr;
942 TRACE("(%p, %s, %s, %s, %s, %p, %d, %p)\n",
943 hWnd, debugstr_a(szCmdName), debugstr_a(szInfSection),
944 debugstr_a(szDir), debugstr_a(lpszTitle),
945 phEXE, dwFlags, pvReserved);
947 if (!szCmdName || !szDir)
948 return E_INVALIDARG;
950 RtlCreateUnicodeStringFromAsciiz(&cmdname, szCmdName);
951 RtlCreateUnicodeStringFromAsciiz(&infsec, szInfSection);
952 RtlCreateUnicodeStringFromAsciiz(&dir, szDir);
953 RtlCreateUnicodeStringFromAsciiz(&title, lpszTitle);
955 hr = RunSetupCommandW(hWnd, cmdname.Buffer, infsec.Buffer, dir.Buffer,
956 title.Buffer, phEXE, dwFlags, pvReserved);
958 RtlFreeUnicodeString(&cmdname);
959 RtlFreeUnicodeString(&infsec);
960 RtlFreeUnicodeString(&dir);
961 RtlFreeUnicodeString(&title);
963 return hr;
966 /***********************************************************************
967 * RunSetupCommandW (ADVPACK.@)
969 * Executes an install section in an INF file or a program.
971 * PARAMS
972 * hWnd [I] Handle to parent window, NULL for quiet mode
973 * szCmdName [I] Inf or EXE filename to execute
974 * szInfSection [I] Inf section to install, NULL for DefaultInstall
975 * szDir [I] Path to extracted files
976 * szTitle [I] Title of all dialogs
977 * phEXE [O] Handle of EXE to wait for
978 * dwFlags [I] Flags; see include/advpub.h
979 * pvReserved [I] Reserved
981 * RETURNS
982 * S_OK Everything OK
983 * S_ASYNCHRONOUS OK, required to wait on phEXE
984 * ERROR_SUCCESS_REBOOT_REQUIRED Reboot required
985 * E_INVALIDARG Invalid argument given
986 * HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION)
987 * Not supported on this Windows version
988 * E_UNEXPECTED Unexpected error
989 * HRESULT_FROM_WIN32(GetLastError()) Some other error
991 HRESULT WINAPI RunSetupCommandW(HWND hWnd, LPCWSTR szCmdName,
992 LPCWSTR szInfSection, LPCWSTR szDir,
993 LPCWSTR lpszTitle, HANDLE *phEXE,
994 DWORD dwFlags, LPVOID pvReserved)
996 ADVInfo info;
997 HRESULT hr;
999 TRACE("(%p, %s, %s, %s, %s, %p, %d, %p)\n",
1000 hWnd, debugstr_w(szCmdName), debugstr_w(szInfSection),
1001 debugstr_w(szDir), debugstr_w(lpszTitle),
1002 phEXE, dwFlags, pvReserved);
1004 if (dwFlags & RSC_FLAG_UPDHLPDLLS)
1005 FIXME("Unhandled flag: RSC_FLAG_UPDHLPDLLS\n");
1007 if (!szCmdName || !szDir)
1008 return E_INVALIDARG;
1010 if (!(dwFlags & RSC_FLAG_INF))
1011 return launch_exe(szCmdName, szDir, phEXE);
1013 ZeroMemory(&info, sizeof(ADVInfo));
1015 hr = install_init(szCmdName, szInfSection, szDir, dwFlags, &info);
1016 if (hr != S_OK)
1017 goto done;
1019 hr = spapi_install(&info);
1020 if (hr != S_OK)
1021 goto done;
1023 hr = adv_install(&info);
1025 done:
1026 install_release(&info);
1028 return hr;