wineps: Handle EMR_INVERTRGN record in spool files.
[wine.git] / dlls / advpack / install.c
blobdeaa82d6d98f67e305481184f74bc5cb1c00eb2b
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 "advpack_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
38 #define SPAPI_ERROR 0xE0000000L
39 #define SPAPI_PREFIX 0x800F0000L
40 #define SPAPI_MASK 0xFFFFL
41 #define HRESULT_FROM_SPAPI(x) ((HRESULT)((x & SPAPI_MASK) | SPAPI_PREFIX))
43 #define ADV_HRESULT(x) ((x & SPAPI_ERROR) ? HRESULT_FROM_SPAPI(x) : HRESULT_FROM_WIN32(x))
45 #define ADV_SUCCESS 0
46 #define ADV_FAILURE 1
48 /* contains information about a specific install instance */
49 typedef struct _ADVInfo
51 HINF hinf;
52 LPWSTR inf_path;
53 LPWSTR inf_filename;
54 LPWSTR install_sec;
55 LPWSTR working_dir;
56 DWORD flags;
57 BOOL need_reboot;
58 } ADVInfo;
60 typedef HRESULT (*iterate_fields_func)(HINF hinf, PCWSTR field, const void *arg);
62 /* Advanced INF callbacks */
63 static HRESULT del_dirs_callback(HINF hinf, PCWSTR field, const void *arg)
65 INFCONTEXT context;
66 HRESULT hr = S_OK;
67 DWORD size;
69 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
71 for (; ok; ok = SetupFindNextLine(&context, &context))
73 WCHAR directory[MAX_INF_STRING_LENGTH];
75 if (!SetupGetLineTextW(&context, NULL, NULL, NULL, directory,
76 MAX_INF_STRING_LENGTH, &size))
77 continue;
79 if (DelNodeW(directory, ADN_DEL_IF_EMPTY) != S_OK)
80 hr = E_FAIL;
83 return hr;
86 static HRESULT per_user_install_callback(HINF hinf, PCWSTR field, const void *arg)
88 PERUSERSECTIONW per_user;
89 INFCONTEXT context;
90 DWORD size;
92 per_user.bRollback = FALSE;
93 per_user.dwIsInstalled = 0;
95 SetupGetLineTextW(NULL, hinf, field, L"DisplayName", per_user.szDispName, ARRAY_SIZE(per_user.szDispName), &size);
97 SetupGetLineTextW(NULL, hinf, field, L"Version", per_user.szVersion, ARRAY_SIZE(per_user.szVersion), &size);
99 if (SetupFindFirstLineW(hinf, field, L"IsInstalled", &context))
101 SetupGetIntField(&context, 1, (PINT)&per_user.dwIsInstalled);
104 SetupGetLineTextW(NULL, hinf, field, L"ComponentID", per_user.szCompID, ARRAY_SIZE(per_user.szCompID), &size);
106 SetupGetLineTextW(NULL, hinf, field, L"GUID", per_user.szGUID, ARRAY_SIZE(per_user.szGUID), &size);
108 SetupGetLineTextW(NULL, hinf, field, L"Locale", per_user.szLocale, ARRAY_SIZE(per_user.szLocale), &size);
110 SetupGetLineTextW(NULL, hinf, field, L"StubPath", per_user.szStub, ARRAY_SIZE(per_user.szStub), &size);
112 return SetPerUserSecValuesW(&per_user);
115 static HRESULT register_ocxs_callback(HINF hinf, PCWSTR field, const void *arg)
117 HMODULE hm;
118 INFCONTEXT context;
119 HRESULT hr = S_OK;
121 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
123 for (; ok; ok = SetupFindNextLine(&context, &context))
125 WCHAR buffer[MAX_INF_STRING_LENGTH];
127 /* get OCX filename */
128 if (!SetupGetStringFieldW(&context, 1, buffer, ARRAY_SIZE(buffer), NULL))
129 continue;
131 hm = LoadLibraryExW(buffer, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
132 if (hm)
134 if (do_ocx_reg(hm, TRUE, NULL, NULL) != S_OK)
135 hr = E_FAIL;
137 FreeLibrary(hm);
139 else
140 hr = E_FAIL;
142 if (FAILED(hr))
144 /* FIXME: display a message box */
145 break;
149 return hr;
152 static HRESULT run_setup_commands_callback(HINF hinf, PCWSTR field, const void *arg)
154 const ADVInfo *info = (const ADVInfo *)arg;
155 INFCONTEXT context;
156 HRESULT hr = S_OK;
157 DWORD size;
159 BOOL ok = SetupFindFirstLineW(hinf, field, NULL, &context);
161 for (; ok; ok = SetupFindNextLine(&context, &context))
163 WCHAR buffer[MAX_INF_STRING_LENGTH];
165 if (!SetupGetLineTextW(&context, NULL, NULL, NULL, buffer,
166 MAX_INF_STRING_LENGTH, &size))
167 continue;
169 if (launch_exe(buffer, info->working_dir, NULL) != S_OK)
170 hr = E_FAIL;
173 return hr;
176 /* sequentially returns pointers to parameters in a parameter list
177 * returns NULL if the parameter is empty, e.g. one,,three */
178 LPWSTR get_parameter(LPWSTR *params, WCHAR separator, BOOL quoted)
180 LPWSTR token = *params;
182 if (!*params)
183 return NULL;
185 if (quoted && *token == '"')
187 WCHAR *end = wcschr(token + 1, '"');
188 if (end)
190 *end = 0;
191 *params = end + 1;
192 token = token + 1;
196 *params = wcschr(*params, separator);
197 if (*params)
198 *(*params)++ = '\0';
200 if (!*token)
201 return NULL;
203 return token;
206 static BOOL is_full_path(LPCWSTR path)
208 const int MIN_PATH_LEN = 3;
210 if (!path || lstrlenW(path) < MIN_PATH_LEN)
211 return FALSE;
213 if ((path[1] == ':' && path[2] == '\\') || (path[0] == '\\' && path[1] == '\\'))
214 return TRUE;
216 return FALSE;
219 /* retrieves the contents of a field, dynamically growing the buffer if necessary */
220 static WCHAR *get_field_string(INFCONTEXT *context, DWORD index, WCHAR *buffer,
221 const WCHAR *static_buffer, DWORD *size)
223 DWORD required;
225 if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
227 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
229 /* now grow the buffer */
230 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
231 if (!(buffer = HeapAlloc(GetProcessHeap(), 0, required*sizeof(WCHAR)))) return NULL;
232 *size = required;
233 if (SetupGetStringFieldW(context, index, buffer, *size, &required)) return buffer;
236 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
237 return NULL;
240 /* iterates over all fields of a certain key of a certain section */
241 static HRESULT iterate_section_fields(HINF hinf, PCWSTR section, PCWSTR key,
242 iterate_fields_func callback, void *arg)
244 WCHAR static_buffer[200];
245 WCHAR *buffer = static_buffer;
246 DWORD size = ARRAY_SIZE(static_buffer);
247 INFCONTEXT context;
248 HRESULT hr = E_FAIL;
250 BOOL ok = SetupFindFirstLineW(hinf, section, key, &context);
251 while (ok)
253 UINT i, count = SetupGetFieldCount(&context);
255 for (i = 1; i <= count; i++)
257 if (!(buffer = get_field_string(&context, i, buffer, static_buffer, &size)))
258 goto done;
260 if ((hr = callback(hinf, buffer, arg)) != S_OK)
261 goto done;
264 ok = SetupFindNextMatchLineW(&context, key, &context);
267 hr = S_OK;
269 done:
270 if (buffer != static_buffer) HeapFree(GetProcessHeap(), 0, buffer);
271 return hr;
274 static HRESULT check_admin_rights(const ADVInfo *info)
276 INT check;
277 INFCONTEXT context;
278 HRESULT hr = S_OK;
280 if (!SetupFindFirstLineW(info->hinf, info->install_sec, L"CheckAdminRights", &context))
281 return S_OK;
283 if (!SetupGetIntField(&context, 1, &check))
284 return S_OK;
286 if (check == 1)
287 hr = IsNTAdmin(0, NULL) ? S_OK : E_FAIL;
289 return hr;
292 /* performs a setupapi-level install of the INF file */
293 static HRESULT spapi_install(const ADVInfo *info)
295 BOOL ret;
296 HRESULT res;
297 PVOID context;
299 context = SetupInitDefaultQueueCallbackEx(NULL, INVALID_HANDLE_VALUE, 0, 0, NULL);
300 if (!context)
301 return ADV_HRESULT(GetLastError());
303 ret = SetupInstallFromInfSectionW(NULL, info->hinf, info->install_sec,
304 SPINST_FILES, NULL, info->working_dir,
305 SP_COPY_NEWER, SetupDefaultQueueCallbackW,
306 context, NULL, NULL);
307 if (!ret)
309 res = ADV_HRESULT(GetLastError());
310 SetupTermDefaultQueueCallback(context);
312 return res;
315 SetupTermDefaultQueueCallback(context);
317 ret = SetupInstallFromInfSectionW(NULL, info->hinf, info->install_sec,
318 SPINST_INIFILES | SPINST_REGISTRY | SPINST_REGSVR,
319 HKEY_LOCAL_MACHINE, NULL, 0,
320 NULL, NULL, NULL, NULL);
321 if (!ret)
322 return ADV_HRESULT(GetLastError());
324 return S_OK;
327 /* processes the Advanced INF commands */
328 static HRESULT adv_install(ADVInfo *info)
330 HRESULT hr;
332 hr = check_admin_rights(info);
333 if (hr != S_OK)
334 return hr;
336 hr = iterate_section_fields(info->hinf, info->install_sec, L"RunPreSetupCommands",
337 run_setup_commands_callback, info);
338 if (hr != S_OK)
339 return hr;
341 OleInitialize(NULL);
342 hr = iterate_section_fields(info->hinf, info->install_sec,
343 L"RegisterOCXs", register_ocxs_callback, NULL);
344 OleUninitialize();
345 if (hr != S_OK)
346 return hr;
348 hr = iterate_section_fields(info->hinf, info->install_sec,
349 L"PerUserInstall", per_user_install_callback, info);
350 if (hr != S_OK)
351 return hr;
353 hr = iterate_section_fields(info->hinf, info->install_sec, L"RunPostSetupCommands",
354 run_setup_commands_callback, info);
355 if (hr != S_OK)
356 return hr;
358 hr = iterate_section_fields(info->hinf, info->install_sec, L"DelDirs", del_dirs_callback, info);
359 if (hr != S_OK)
360 return hr;
362 return hr;
365 /* determines the proper working directory for the INF file */
366 static HRESULT get_working_dir(ADVInfo *info, LPCWSTR inf_filename, LPCWSTR working_dir)
368 WCHAR path[MAX_PATH];
369 LPCWSTR ptr;
370 DWORD len;
372 if ((ptr = wcsrchr(inf_filename, '\\')))
374 len = ptr - inf_filename + 1;
375 ptr = inf_filename;
377 else if (working_dir && *working_dir)
379 len = lstrlenW(working_dir) + 1;
380 ptr = working_dir;
382 else
384 GetCurrentDirectoryW(MAX_PATH, path);
385 lstrcatW(path, L"\\");
386 lstrcatW(path, inf_filename);
388 /* check if the INF file is in the current directory */
389 if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
391 GetCurrentDirectoryW(MAX_PATH, path);
393 else
395 /* default to the windows\inf directory if all else fails */
396 GetWindowsDirectoryW(path, MAX_PATH);
397 lstrcatW(path, L"\\INF");
400 len = lstrlenW(path) + 1;
401 ptr = path;
404 info->working_dir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
405 if (!info->working_dir)
406 return E_OUTOFMEMORY;
408 lstrcpynW(info->working_dir, ptr, len);
410 return S_OK;
413 /* loads the INF file and performs checks on it */
414 static HRESULT install_init(LPCWSTR inf_filename, LPCWSTR install_sec,
415 LPCWSTR working_dir, DWORD flags, ADVInfo *info)
417 DWORD len;
418 HRESULT hr;
419 LPCWSTR ptr, path;
421 if (!(ptr = wcsrchr(inf_filename, '\\')))
422 ptr = inf_filename;
424 len = lstrlenW(ptr);
426 info->inf_filename = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
427 if (!info->inf_filename)
428 return E_OUTOFMEMORY;
430 lstrcpyW(info->inf_filename, ptr);
432 /* FIXME: determine the proper platform to install (NTx86, etc) */
433 if (!install_sec || !*install_sec)
435 len = ARRAY_SIZE(L"DefaultInstall");
436 ptr = L"DefaultInstall";
438 else
440 len = lstrlenW(install_sec) + 1;
441 ptr = install_sec;
444 info->install_sec = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
445 if (!info->install_sec)
446 return E_OUTOFMEMORY;
448 lstrcpyW(info->install_sec, ptr);
450 hr = get_working_dir(info, inf_filename, working_dir);
451 if (FAILED(hr))
452 return hr;
454 len = lstrlenW(info->working_dir) + lstrlenW(info->inf_filename) + 2;
455 info->inf_path = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
456 if (!info->inf_path)
457 return E_OUTOFMEMORY;
459 lstrcpyW(info->inf_path, info->working_dir);
460 lstrcatW(info->inf_path, L"\\");
461 lstrcatW(info->inf_path, info->inf_filename);
463 /* RunSetupCommand opens unmodified filename parameter */
464 if (flags & RSC_FLAG_INF)
465 path = inf_filename;
466 else
467 path = info->inf_path;
469 info->hinf = SetupOpenInfFileW(path, NULL, INF_STYLE_WIN4, NULL);
470 if (info->hinf == INVALID_HANDLE_VALUE)
471 return ADV_HRESULT(GetLastError());
473 set_ldids(info->hinf, info->install_sec, info->working_dir);
475 /* FIXME: check that the INF is advanced */
477 info->flags = flags;
478 info->need_reboot = FALSE;
480 return S_OK;
483 /* release the install instance information */
484 static void install_release(const ADVInfo *info)
486 SetupCloseInfFile(info->hinf);
488 HeapFree(GetProcessHeap(), 0, info->inf_path);
489 HeapFree(GetProcessHeap(), 0, info->inf_filename);
490 HeapFree(GetProcessHeap(), 0, info->install_sec);
491 HeapFree(GetProcessHeap(), 0, info->working_dir);
494 /* this structure very closely resembles parameters of RunSetupCommand() */
495 typedef struct
497 HWND hwnd;
498 LPCSTR title;
499 LPCSTR inf_name;
500 LPCSTR dir;
501 LPCSTR section_name;
502 } SETUPCOMMAND_PARAMS;
504 typedef struct
506 HWND hwnd;
507 LPCWSTR title;
508 LPCWSTR inf_name;
509 LPCWSTR dir;
510 LPCWSTR section_name;
511 } SETUPCOMMAND_PARAMSW;
513 /* internal: see DoInfInstall */
514 static HRESULT DoInfInstallW(const SETUPCOMMAND_PARAMSW *setup)
516 ADVInfo info;
517 HRESULT hr;
519 TRACE("(%p)\n", setup);
521 ZeroMemory(&info, sizeof(ADVInfo));
523 hr = install_init(setup->inf_name, setup->section_name, setup->dir, 0, &info);
524 if (hr != S_OK)
525 goto done;
527 hr = spapi_install(&info);
528 if (hr != S_OK)
529 goto done;
531 hr = adv_install(&info);
533 done:
534 install_release(&info);
536 return S_OK;
539 /***********************************************************************
540 * DoInfInstall (ADVPACK.@)
542 * Install an INF section.
544 * PARAMS
545 * setup [I] Structure containing install information.
547 * RETURNS
548 * S_OK Everything OK
549 * HRESULT_FROM_WIN32(GetLastError()) Some other error
551 HRESULT WINAPI DoInfInstall(const SETUPCOMMAND_PARAMS *setup)
553 UNICODE_STRING title, inf, section, dir;
554 SETUPCOMMAND_PARAMSW params;
555 HRESULT hr;
557 if (!setup)
558 return E_INVALIDARG;
560 RtlCreateUnicodeStringFromAsciiz(&title, setup->title);
561 RtlCreateUnicodeStringFromAsciiz(&inf, setup->inf_name);
562 RtlCreateUnicodeStringFromAsciiz(&section, setup->section_name);
563 RtlCreateUnicodeStringFromAsciiz(&dir, setup->dir);
565 params.title = title.Buffer;
566 params.inf_name = inf.Buffer;
567 params.section_name = section.Buffer;
568 params.dir = dir.Buffer;
569 params.hwnd = setup->hwnd;
571 hr = DoInfInstallW(&params);
573 RtlFreeUnicodeString(&title);
574 RtlFreeUnicodeString(&inf);
575 RtlFreeUnicodeString(&section);
576 RtlFreeUnicodeString(&dir);
578 return hr;
581 /***********************************************************************
582 * ExecuteCabA (ADVPACK.@)
584 * See ExecuteCabW.
586 HRESULT WINAPI ExecuteCabA(HWND hwnd, CABINFOA* pCab, LPVOID pReserved)
588 UNICODE_STRING cab, inf, section;
589 CABINFOW cabinfo;
590 HRESULT hr;
592 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
594 if (!pCab)
595 return E_INVALIDARG;
597 if (pCab->pszCab)
599 RtlCreateUnicodeStringFromAsciiz(&cab, pCab->pszCab);
600 cabinfo.pszCab = cab.Buffer;
602 else
603 cabinfo.pszCab = NULL;
605 RtlCreateUnicodeStringFromAsciiz(&inf, pCab->pszInf);
606 RtlCreateUnicodeStringFromAsciiz(&section, pCab->pszSection);
608 MultiByteToWideChar(CP_ACP, 0, pCab->szSrcPath, -1, cabinfo.szSrcPath, ARRAY_SIZE(cabinfo.szSrcPath));
610 cabinfo.pszInf = inf.Buffer;
611 cabinfo.pszSection = section.Buffer;
612 cabinfo.dwFlags = pCab->dwFlags;
614 hr = ExecuteCabW(hwnd, &cabinfo, pReserved);
616 if (pCab->pszCab)
617 RtlFreeUnicodeString(&cab);
619 RtlFreeUnicodeString(&inf);
620 RtlFreeUnicodeString(&section);
622 return hr;
625 /***********************************************************************
626 * ExecuteCabW (ADVPACK.@)
628 * Installs the INF file extracted from a specified cabinet file.
630 * PARAMS
631 * hwnd [I] Handle to the window used for the display.
632 * pCab [I] Information about the cabinet file.
633 * pReserved [I] Reserved. Must be NULL.
635 * RETURNS
636 * Success: S_OK.
637 * Failure: E_FAIL.
639 HRESULT WINAPI ExecuteCabW(HWND hwnd, CABINFOW* pCab, LPVOID pReserved)
641 ADVInfo info;
642 HRESULT hr;
644 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
646 ZeroMemory(&info, sizeof(ADVInfo));
648 if ((pCab->pszCab && *pCab->pszCab) && (pCab->pszInf && *pCab->pszInf) && *pCab->szSrcPath)
650 TRACE("pszCab: %s, pszInf: %s, szSrcPath: %s\n", debugstr_w(pCab->pszCab), debugstr_w(pCab->pszInf),
651 debugstr_w(pCab->szSrcPath));
653 hr = ExtractFilesW(pCab->pszCab, pCab->szSrcPath, 0, pCab->pszInf, NULL, 0);
654 if (FAILED(hr))
655 ERR("Failed to extract .inf file!\n");
658 hr = install_init(pCab->pszInf, pCab->pszSection, pCab->szSrcPath, pCab->dwFlags, &info);
659 if (hr != S_OK)
660 goto done;
662 hr = spapi_install(&info);
663 if (hr != S_OK)
664 goto done;
666 hr = adv_install(&info);
668 done:
669 install_release(&info);
671 return hr;
674 /***********************************************************************
675 * LaunchINFSectionA (ADVPACK.@)
677 * See LaunchINFSectionW.
679 INT WINAPI LaunchINFSectionA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
681 UNICODE_STRING cmd;
682 HRESULT hr;
684 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
686 if (!cmdline)
687 return ADV_FAILURE;
689 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
691 hr = LaunchINFSectionW(hWnd, hInst, cmd.Buffer, show);
693 RtlFreeUnicodeString(&cmd);
695 return hr;
698 /***********************************************************************
699 * LaunchINFSectionW (ADVPACK.@)
701 * Installs an INF section without BACKUP/ROLLBACK capabilities.
703 * PARAMS
704 * hWnd [I] Handle to parent window, NULL for desktop.
705 * hInst [I] Instance of the process.
706 * cmdline [I] Contains parameters in the order INF,section,flags,reboot.
707 * show [I] How the window should be shown.
709 * RETURNS
710 * Success: ADV_SUCCESS.
711 * Failure: ADV_FAILURE.
713 * NOTES
714 * INF - Filename of the INF to launch.
715 * section - INF section to install.
716 * flags - see advpub.h.
717 * reboot - smart reboot behavior
718 * 'A' Always reboot.
719 * 'I' Reboot if needed (default).
720 * 'N' No reboot.
722 INT WINAPI LaunchINFSectionW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
724 ADVInfo info;
725 LPWSTR cmdline_copy, cmdline_ptr;
726 LPWSTR inf_filename, install_sec;
727 LPWSTR str_flags;
728 DWORD flags = 0;
729 HRESULT hr = S_OK;
731 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
733 if (!cmdline)
734 return ADV_FAILURE;
736 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
737 cmdline_ptr = cmdline_copy;
738 lstrcpyW(cmdline_copy, cmdline);
740 inf_filename = get_parameter(&cmdline_ptr, ',', TRUE);
741 install_sec = get_parameter(&cmdline_ptr, ',', TRUE);
743 str_flags = get_parameter(&cmdline_ptr, ',', TRUE);
744 if (str_flags)
746 DWORD inf_flags = wcstol(str_flags, NULL, 10);
747 if (inf_flags & LIS_QUIET) flags |= RSC_FLAG_QUIET;
748 if (inf_flags & LIS_NOGRPCONV) flags |= RSC_FLAG_NGCONV;
751 ZeroMemory(&info, sizeof(ADVInfo));
753 hr = install_init(inf_filename, install_sec, NULL, flags, &info);
754 if (hr != S_OK)
755 goto done;
757 hr = spapi_install(&info);
758 if (hr != S_OK)
759 goto done;
761 hr = adv_install(&info);
763 done:
764 install_release(&info);
765 HeapFree(GetProcessHeap(), 0, cmdline_copy);
767 return SUCCEEDED(hr) ? ADV_SUCCESS : ADV_FAILURE;
770 /***********************************************************************
771 * LaunchINFSectionExA (ADVPACK.@)
773 * See LaunchINFSectionExW.
775 HRESULT WINAPI LaunchINFSectionExA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
777 UNICODE_STRING cmd;
778 HRESULT hr;
780 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
782 if (!cmdline)
783 return ADV_FAILURE;
785 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
787 hr = LaunchINFSectionExW(hWnd, hInst, cmd.Buffer, show);
789 RtlFreeUnicodeString(&cmd);
791 return hr;
794 /***********************************************************************
795 * LaunchINFSectionExW (ADVPACK.@)
797 * Installs an INF section with BACKUP/ROLLBACK capabilities.
799 * PARAMS
800 * hWnd [I] Handle to parent window, NULL for desktop.
801 * hInst [I] Instance of the process.
802 * cmdline [I] Contains parameters in the order INF,section,CAB,flags,reboot.
803 * show [I] How the window should be shown.
805 * RETURNS
806 * Success: ADV_SUCCESS.
807 * Failure: ADV_FAILURE.
809 * NOTES
810 * INF - Filename of the INF to launch.
811 * section - INF section to install.
812 * flags - see advpub.h.
813 * reboot - smart reboot behavior
814 * 'A' Always reboot.
815 * 'I' Reboot if needed (default).
816 * 'N' No reboot.
818 * BUGS
819 * Doesn't handle the reboot flag.
821 HRESULT WINAPI LaunchINFSectionExW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
823 LPWSTR cmdline_copy, cmdline_ptr;
824 LPWSTR flags, ptr;
825 CABINFOW cabinfo;
826 HRESULT hr;
828 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
830 if (!cmdline)
831 return ADV_FAILURE;
833 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
834 cmdline_ptr = cmdline_copy;
835 lstrcpyW(cmdline_copy, cmdline);
837 cabinfo.pszInf = get_parameter(&cmdline_ptr, ',', TRUE);
838 cabinfo.pszSection = get_parameter(&cmdline_ptr, ',', TRUE);
839 cabinfo.pszCab = get_parameter(&cmdline_ptr, ',', TRUE);
840 *cabinfo.szSrcPath = '\0';
842 flags = get_parameter(&cmdline_ptr, ',', TRUE);
843 if (flags)
844 cabinfo.dwFlags = wcstol(flags, NULL, 10);
846 if (!is_full_path(cabinfo.pszCab) && !is_full_path(cabinfo.pszInf))
848 HeapFree(GetProcessHeap(), 0, cmdline_copy);
849 return E_INVALIDARG;
852 /* get the source path from the cab filename */
853 if (cabinfo.pszCab && *cabinfo.pszCab)
855 if (!is_full_path(cabinfo.pszCab))
856 lstrcpyW(cabinfo.szSrcPath, cabinfo.pszInf);
857 else
858 lstrcpyW(cabinfo.szSrcPath, cabinfo.pszCab);
860 ptr = wcsrchr(cabinfo.szSrcPath, '\\');
861 *(++ptr) = '\0';
864 hr = ExecuteCabW(hWnd, &cabinfo, NULL);
865 HeapFree(GetProcessHeap(), 0, cmdline_copy);
866 return SUCCEEDED(hr) ? ADV_SUCCESS : ADV_FAILURE;
869 HRESULT launch_exe(LPCWSTR cmd, LPCWSTR dir, HANDLE *phEXE)
871 STARTUPINFOW si;
872 PROCESS_INFORMATION pi;
874 if (phEXE) *phEXE = NULL;
876 ZeroMemory(&pi, sizeof(pi));
877 ZeroMemory(&si, sizeof(si));
878 si.cb = sizeof(si);
880 if (!CreateProcessW(NULL, (LPWSTR)cmd, NULL, NULL, FALSE,
881 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP,
882 NULL, dir, &si, &pi))
884 return HRESULT_FROM_WIN32(GetLastError());
887 CloseHandle(pi.hThread);
889 if (phEXE)
891 *phEXE = pi.hProcess;
892 return S_ASYNCHRONOUS;
895 /* wait for the child process to finish */
896 WaitForSingleObject(pi.hProcess, INFINITE);
897 CloseHandle(pi.hProcess);
899 return S_OK;
902 /***********************************************************************
903 * RunSetupCommandA (ADVPACK.@)
905 * See RunSetupCommandW.
907 HRESULT WINAPI RunSetupCommandA(HWND hWnd, LPCSTR szCmdName,
908 LPCSTR szInfSection, LPCSTR szDir,
909 LPCSTR lpszTitle, HANDLE *phEXE,
910 DWORD dwFlags, LPVOID pvReserved)
912 UNICODE_STRING cmdname, infsec;
913 UNICODE_STRING dir, title;
914 HRESULT hr;
916 TRACE("(%p, %s, %s, %s, %s, %p, %ld, %p)\n",
917 hWnd, debugstr_a(szCmdName), debugstr_a(szInfSection),
918 debugstr_a(szDir), debugstr_a(lpszTitle),
919 phEXE, dwFlags, pvReserved);
921 if (!szCmdName || !szDir)
922 return E_INVALIDARG;
924 RtlCreateUnicodeStringFromAsciiz(&cmdname, szCmdName);
925 RtlCreateUnicodeStringFromAsciiz(&infsec, szInfSection);
926 RtlCreateUnicodeStringFromAsciiz(&dir, szDir);
927 RtlCreateUnicodeStringFromAsciiz(&title, lpszTitle);
929 hr = RunSetupCommandW(hWnd, cmdname.Buffer, infsec.Buffer, dir.Buffer,
930 title.Buffer, phEXE, dwFlags, pvReserved);
932 RtlFreeUnicodeString(&cmdname);
933 RtlFreeUnicodeString(&infsec);
934 RtlFreeUnicodeString(&dir);
935 RtlFreeUnicodeString(&title);
937 return hr;
940 /***********************************************************************
941 * RunSetupCommandW (ADVPACK.@)
943 * Executes an install section in an INF file or a program.
945 * PARAMS
946 * hWnd [I] Handle to parent window, NULL for quiet mode
947 * szCmdName [I] Inf or EXE filename to execute
948 * szInfSection [I] Inf section to install, NULL for DefaultInstall
949 * szDir [I] Path to extracted files
950 * szTitle [I] Title of all dialogs
951 * phEXE [O] Handle of EXE to wait for
952 * dwFlags [I] Flags; see include/advpub.h
953 * pvReserved [I] Reserved
955 * RETURNS
956 * S_OK Everything OK
957 * S_ASYNCHRONOUS OK, required to wait on phEXE
958 * ERROR_SUCCESS_REBOOT_REQUIRED Reboot required
959 * E_INVALIDARG Invalid argument given
960 * HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION)
961 * Not supported on this Windows version
962 * E_UNEXPECTED Unexpected error
963 * HRESULT_FROM_WIN32(GetLastError()) Some other error
965 HRESULT WINAPI RunSetupCommandW(HWND hWnd, LPCWSTR szCmdName,
966 LPCWSTR szInfSection, LPCWSTR szDir,
967 LPCWSTR lpszTitle, HANDLE *phEXE,
968 DWORD dwFlags, LPVOID pvReserved)
970 ADVInfo info;
971 HRESULT hr;
973 TRACE("(%p, %s, %s, %s, %s, %p, %ld, %p)\n",
974 hWnd, debugstr_w(szCmdName), debugstr_w(szInfSection),
975 debugstr_w(szDir), debugstr_w(lpszTitle),
976 phEXE, dwFlags, pvReserved);
978 if (dwFlags & RSC_FLAG_UPDHLPDLLS)
979 FIXME("Unhandled flag: RSC_FLAG_UPDHLPDLLS\n");
981 if (!szCmdName || !szDir)
982 return E_INVALIDARG;
984 if (!(dwFlags & RSC_FLAG_INF))
985 return launch_exe(szCmdName, szDir, phEXE);
987 ZeroMemory(&info, sizeof(ADVInfo));
989 hr = install_init(szCmdName, szInfSection, szDir, dwFlags, &info);
990 if (hr != S_OK)
991 goto done;
993 hr = spapi_install(&info);
994 if (hr != S_OK)
995 goto done;
997 hr = adv_install(&info);
999 done:
1000 install_release(&info);
1002 return hr;