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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "wine/debug.h"
34 #include "wine/unicode.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) ((x & SPAPI_MASK) | SPAPI_PREFIX)
43 #define ADV_HRESULT(x) ((x & SPAPI_ERROR) ? HRESULT_FROM_SPAPI(x) : HRESULT_FROM_WIN32(x))
45 /* sequentially returns pointers to parameters in a parameter list
46 * returns NULL if the parameter is empty, e.g. one,,three */
47 LPWSTR get_parameter(LPWSTR *params, WCHAR separator)
49 LPWSTR token = *params;
54 *params = strchrW(*params, separator);
64 static BOOL is_full_path(LPWSTR path)
66 const int MIN_PATH_LEN = 3;
68 if (!path || lstrlenW(path) < MIN_PATH_LEN)
71 if (path[1] == ':' || (path[0] == '\\' && path[1] == '\\'))
77 /* performs a setupapi-level install of the INF file */
78 static HRESULT spapi_install(HINF hinf, LPCWSTR install_sec, LPCWSTR source_path)
84 context = SetupInitDefaultQueueCallbackEx(NULL, INVALID_HANDLE_VALUE, 0, 0, NULL);
86 return ADV_HRESULT(GetLastError());
88 ret = SetupInstallFromInfSectionW(NULL, hinf, install_sec, SPINST_FILES,
89 NULL, source_path, SP_COPY_NEWER,
90 SetupDefaultQueueCallbackW,
94 res = ADV_HRESULT(GetLastError());
95 SetupTermDefaultQueueCallback(context);
100 SetupTermDefaultQueueCallback(context);
102 ret = SetupInstallFromInfSectionW(NULL, hinf, install_sec,
103 SPINST_INIFILES | SPINST_REGISTRY,
104 HKEY_LOCAL_MACHINE, NULL, 0,
105 NULL, NULL, NULL, NULL);
107 return ADV_HRESULT(GetLastError());
112 /* this structure very closely resembles parameters of RunSetupCommand() */
120 } SETUPCOMMAND_PARAMS;
122 /***********************************************************************
123 * DoInfInstall (ADVPACK.@)
125 * Install an INF section.
128 * setup [I] Structure containing install information.
132 * HRESULT_FROM_WIN32(GetLastError()) Some other error
134 HRESULT WINAPI DoInfInstall(const SETUPCOMMAND_PARAMS *setup)
138 void *callback_context;
140 TRACE("%p, %s, %s, %s, %s\n", setup->hwnd, debugstr_a(setup->title),
141 debugstr_a(setup->inf_name), debugstr_a(setup->dir),
142 debugstr_a(setup->section_name));
144 hinf = SetupOpenInfFileA(setup->inf_name, NULL, INF_STYLE_WIN4, NULL);
145 if (hinf == INVALID_HANDLE_VALUE) return HRESULT_FROM_WIN32(GetLastError());
147 callback_context = SetupInitDefaultQueueCallback(setup->hwnd);
149 ret = SetupInstallFromInfSectionA(NULL, hinf, setup->section_name, SPINST_ALL,
150 NULL, NULL, 0, SetupDefaultQueueCallbackA,
151 callback_context, NULL, NULL);
152 SetupTermDefaultQueueCallback(callback_context);
153 SetupCloseInfFile(hinf);
155 return ret ? S_OK : HRESULT_FROM_WIN32(GetLastError());
158 /***********************************************************************
159 * ExecuteCabA (ADVPACK.@)
163 HRESULT WINAPI ExecuteCabA(HWND hwnd, CABINFOA* pCab, LPVOID pReserved)
165 UNICODE_STRING cab, inf, section;
169 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
176 RtlCreateUnicodeStringFromAsciiz(&cab, pCab->pszCab);
177 cabinfo.pszCab = cab.Buffer;
180 cabinfo.pszCab = NULL;
182 RtlCreateUnicodeStringFromAsciiz(&inf, pCab->pszInf);
183 RtlCreateUnicodeStringFromAsciiz(§ion, pCab->pszSection);
185 MultiByteToWideChar(CP_ACP, 0, pCab->szSrcPath, -1, cabinfo.szSrcPath,
186 sizeof(cabinfo.szSrcPath) / sizeof(WCHAR));
188 cabinfo.pszInf = inf.Buffer;
189 cabinfo.pszSection = section.Buffer;
190 cabinfo.dwFlags = pCab->dwFlags;
192 hr = ExecuteCabW(hwnd, &cabinfo, pReserved);
195 RtlFreeUnicodeString(&cab);
197 RtlFreeUnicodeString(&inf);
198 RtlFreeUnicodeString(§ion);
203 /***********************************************************************
204 * ExecuteCabW (ADVPACK.@)
206 * Installs the INF file extracted from a specified cabinet file.
209 * hwnd [I] Handle to the window used for the display.
210 * pCab [I] Information about the cabinet file.
211 * pReserved [I] Reserved. Must be NULL.
220 HRESULT WINAPI ExecuteCabW(HWND hwnd, CABINFOW* pCab, LPVOID pReserved)
222 FIXME("(%p, %p, %p): stub\n", hwnd, pCab, pReserved);
226 /***********************************************************************
227 * LaunchINFSectionA (ADVPACK.@)
229 * See LaunchINFSectionW.
231 INT WINAPI LaunchINFSectionA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
236 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
238 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
240 hr = LaunchINFSectionW(hWnd, hInst, cmd.Buffer, show);
242 RtlFreeUnicodeString(&cmd);
247 /***********************************************************************
248 * LaunchINFSectionW (ADVPACK.@)
250 * Installs an INF section without BACKUP/ROLLBACK capabilities.
253 * hWnd [I] Handle to parent window, NULL for desktop.
254 * hInst [I] Instance of the process.
255 * cmdline [I] Contains parameters in the order INF,section,flags,reboot.
256 * show [I] How the window should be shown.
263 * INF - Filename of the INF to launch.
264 * section - INF section to install.
265 * flags - see advpub.h.
266 * reboot - smart reboot behavior
268 * 'I' Reboot if needed (default).
274 INT WINAPI LaunchINFSectionW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
276 FIXME("(%p, %p, %s, %i): stub\n", hWnd, hInst, debugstr_w(cmdline), show);
280 /***********************************************************************
281 * LaunchINFSectionExA (ADVPACK.@)
283 * See LaunchINFSectionExW.
285 HRESULT WINAPI LaunchINFSectionExA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
290 TRACE("(%p, %p, %s, %i): stub\n", hWnd, hInst, debugstr_a(cmdline), show);
292 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
294 hr = LaunchINFSectionExW(hWnd, hInst, cmd.Buffer, show);
296 RtlFreeUnicodeString(&cmd);
301 /***********************************************************************
302 * LaunchINFSectionExW (ADVPACK.@)
304 * Installs an INF section with BACKUP/ROLLBACK capabilities.
307 * hWnd [I] Handle to parent window, NULL for desktop.
308 * hInst [I] Instance of the process.
309 * cmdline [I] Contains parameters in the order INF,section,CAB,flags,reboot.
310 * show [I] How the window should be shown.
317 * INF - Filename of the INF to launch.
318 * section - INF section to install.
319 * flags - see advpub.h.
320 * reboot - smart reboot behavior
322 * 'I' Reboot if needed (default).
326 * Doesn't handle the reboot flag.
328 HRESULT WINAPI LaunchINFSectionExW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
330 LPWSTR cmdline_copy, cmdline_ptr;
335 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
340 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
341 cmdline_ptr = cmdline_copy;
342 lstrcpyW(cmdline_copy, cmdline);
344 cabinfo.pszInf = get_parameter(&cmdline_ptr, ',');
345 cabinfo.pszSection = get_parameter(&cmdline_ptr, ',');
346 cabinfo.pszCab = get_parameter(&cmdline_ptr, ',');
348 flags = get_parameter(&cmdline_ptr, ',');
350 cabinfo.dwFlags = atolW(flags);
352 /* get the source path from the cab filename */
353 if (cabinfo.pszCab && *cabinfo.pszCab)
355 if (!is_full_path(cabinfo.pszCab))
358 lstrcpyW(cabinfo.szSrcPath, cabinfo.pszCab);
359 ptr = strrchrW(cabinfo.szSrcPath, '\\');
363 hr = ExecuteCabW(hWnd, &cabinfo, NULL);
366 HeapFree(GetProcessHeap(), 0, cmdline_copy);
371 HRESULT launch_exe(LPCWSTR cmd, LPCWSTR dir, HANDLE *phEXE)
374 PROCESS_INFORMATION pi;
376 if (phEXE) *phEXE = NULL;
378 ZeroMemory(&pi, sizeof(pi));
379 ZeroMemory(&si, sizeof(si));
382 if (!CreateProcessW(NULL, (LPWSTR)cmd, NULL, NULL, FALSE,
383 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP,
384 NULL, dir, &si, &pi))
386 return HRESULT_FROM_WIN32(GetLastError());
389 CloseHandle(pi.hThread);
393 *phEXE = pi.hProcess;
394 return S_ASYNCHRONOUS;
397 /* wait for the child process to finish */
398 WaitForSingleObject(pi.hProcess, INFINITE);
399 CloseHandle(pi.hProcess);
404 /***********************************************************************
405 * RunSetupCommandA (ADVPACK.@)
407 * See RunSetupCommandW.
409 HRESULT WINAPI RunSetupCommandA(HWND hWnd, LPCSTR szCmdName,
410 LPCSTR szInfSection, LPCSTR szDir,
411 LPCSTR lpszTitle, HANDLE *phEXE,
412 DWORD dwFlags, LPVOID pvReserved)
414 UNICODE_STRING cmdname, infsec;
415 UNICODE_STRING dir, title;
418 TRACE("(%p, %s, %s, %s, %s, %p, %ld, %p)\n",
419 hWnd, debugstr_a(szCmdName), debugstr_a(szInfSection),
420 debugstr_a(szDir), debugstr_a(lpszTitle),
421 phEXE, dwFlags, pvReserved);
423 if (!szCmdName || !szDir)
426 RtlCreateUnicodeStringFromAsciiz(&cmdname, szCmdName);
427 RtlCreateUnicodeStringFromAsciiz(&infsec, szInfSection);
428 RtlCreateUnicodeStringFromAsciiz(&dir, szDir);
429 RtlCreateUnicodeStringFromAsciiz(&title, lpszTitle);
431 hr = RunSetupCommandW(hWnd, cmdname.Buffer, infsec.Buffer, dir.Buffer,
432 title.Buffer, phEXE, dwFlags, pvReserved);
434 RtlFreeUnicodeString(&cmdname);
435 RtlFreeUnicodeString(&infsec);
436 RtlFreeUnicodeString(&dir);
437 RtlFreeUnicodeString(&title);
442 /***********************************************************************
443 * RunSetupCommandW (ADVPACK.@)
445 * Executes an install section in an INF file or a program.
448 * hWnd [I] Handle to parent window, NULL for quiet mode
449 * szCmdName [I] Inf or EXE filename to execute
450 * szInfSection [I] Inf section to install, NULL for DefaultInstall
451 * szDir [I] Path to extracted files
452 * szTitle [I] Title of all dialogs
453 * phEXE [O] Handle of EXE to wait for
454 * dwFlags [I] Flags; see include/advpub.h
455 * pvReserved [I] Reserved
459 * S_ASYNCHRONOUS OK, required to wait on phEXE
460 * ERROR_SUCCESS_REBOOT_REQUIRED Reboot required
461 * E_INVALIDARG Invalid argument given
462 * HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION)
463 * Not supported on this Windows version
464 * E_UNEXPECTED Unexpected error
465 * HRESULT_FROM_WIN32(GetLastError()) Some other error
468 * INF install unimplemented.
470 HRESULT WINAPI RunSetupCommandW(HWND hWnd, LPCWSTR szCmdName,
471 LPCWSTR szInfSection, LPCWSTR szDir,
472 LPCWSTR lpszTitle, HANDLE *phEXE,
473 DWORD dwFlags, LPVOID pvReserved)
478 TRACE("(%p, %s, %s, %s, %s, %p, %ld, %p)\n",
479 hWnd, debugstr_w(szCmdName), debugstr_w(szInfSection),
480 debugstr_w(szDir), debugstr_w(lpszTitle),
481 phEXE, dwFlags, pvReserved);
484 FIXME("Unhandled flags: 0x%08lx\n", dwFlags);
486 if (!szCmdName || !szDir)
489 if (!(dwFlags & RSC_FLAG_INF))
490 return launch_exe(szCmdName, szDir, phEXE);
492 hinf = SetupOpenInfFileW(szCmdName, NULL, INF_STYLE_WIN4, NULL);
493 if (hinf == INVALID_HANDLE_VALUE)
494 return ADV_HRESULT(GetLastError());
496 hr = spapi_install(hinf, szInfSection, szDir);
498 SetupCloseInfFile(hinf);