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 NULL, context, NULL, NULL);
93 res = ADV_HRESULT(GetLastError());
94 SetupTermDefaultQueueCallback(context);
99 SetupTermDefaultQueueCallback(context);
101 ret = SetupInstallFromInfSectionW(NULL, hinf, install_sec,
102 SPINST_INIFILES | SPINST_REGISTRY,
103 HKEY_LOCAL_MACHINE, NULL, 0,
104 NULL, NULL, NULL, NULL);
106 return ADV_HRESULT(GetLastError());
111 /* this structure very closely resembles parameters of RunSetupCommand() */
119 } SETUPCOMMAND_PARAMS;
121 /***********************************************************************
122 * DoInfInstall (ADVPACK.@)
124 * Install an INF section.
127 * setup [I] Structure containing install information.
131 * HRESULT_FROM_WIN32(GetLastError()) Some other error
133 HRESULT WINAPI DoInfInstall(const SETUPCOMMAND_PARAMS *setup)
137 void *callback_context;
139 TRACE("%p, %s, %s, %s, %s\n", setup->hwnd, debugstr_a(setup->title),
140 debugstr_a(setup->inf_name), debugstr_a(setup->dir),
141 debugstr_a(setup->section_name));
143 hinf = SetupOpenInfFileA(setup->inf_name, NULL, INF_STYLE_WIN4, NULL);
144 if (hinf == INVALID_HANDLE_VALUE) return HRESULT_FROM_WIN32(GetLastError());
146 callback_context = SetupInitDefaultQueueCallback(setup->hwnd);
148 ret = SetupInstallFromInfSectionA(NULL, hinf, setup->section_name, SPINST_ALL,
149 NULL, NULL, 0, SetupDefaultQueueCallbackA,
150 callback_context, NULL, NULL);
151 SetupTermDefaultQueueCallback(callback_context);
152 SetupCloseInfFile(hinf);
154 return ret ? S_OK : HRESULT_FROM_WIN32(GetLastError());
157 /***********************************************************************
158 * ExecuteCabA (ADVPACK.@)
162 HRESULT WINAPI ExecuteCabA(HWND hwnd, CABINFOA* pCab, LPVOID pReserved)
164 UNICODE_STRING cab, inf, section;
168 TRACE("(%p, %p, %p)\n", hwnd, pCab, pReserved);
175 RtlCreateUnicodeStringFromAsciiz(&cab, pCab->pszCab);
176 cabinfo.pszCab = cab.Buffer;
179 cabinfo.pszCab = NULL;
181 RtlCreateUnicodeStringFromAsciiz(&inf, pCab->pszInf);
182 RtlCreateUnicodeStringFromAsciiz(§ion, pCab->pszSection);
184 MultiByteToWideChar(CP_ACP, 0, pCab->szSrcPath, -1, cabinfo.szSrcPath,
185 sizeof(cabinfo.szSrcPath) / sizeof(WCHAR));
187 cabinfo.pszInf = inf.Buffer;
188 cabinfo.pszSection = section.Buffer;
189 cabinfo.dwFlags = pCab->dwFlags;
191 hr = ExecuteCabW(hwnd, &cabinfo, pReserved);
194 RtlFreeUnicodeString(&cab);
196 RtlFreeUnicodeString(&inf);
197 RtlFreeUnicodeString(§ion);
202 /***********************************************************************
203 * ExecuteCabW (ADVPACK.@)
205 * Installs the INF file extracted from a specified cabinet file.
208 * hwnd [I] Handle to the window used for the display.
209 * pCab [I] Information about the cabinet file.
210 * pReserved [I] Reserved. Must be NULL.
219 HRESULT WINAPI ExecuteCabW(HWND hwnd, CABINFOW* pCab, LPVOID pReserved)
221 FIXME("(%p, %p, %p): stub\n", hwnd, pCab, pReserved);
225 /***********************************************************************
226 * LaunchINFSectionA (ADVPACK.@)
228 * See LaunchINFSectionW.
230 INT WINAPI LaunchINFSectionA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
235 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
237 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
239 hr = LaunchINFSectionW(hWnd, hInst, cmd.Buffer, show);
241 RtlFreeUnicodeString(&cmd);
246 /***********************************************************************
247 * LaunchINFSectionW (ADVPACK.@)
249 * Installs an INF section without BACKUP/ROLLBACK capabilities.
252 * hWnd [I] Handle to parent window, NULL for desktop.
253 * hInst [I] Instance of the process.
254 * cmdline [I] Contains parameters in the order INF,section,flags,reboot.
255 * show [I] How the window should be shown.
262 * INF - Filename of the INF to launch.
263 * section - INF section to install.
264 * flags - see advpub.h.
265 * reboot - smart reboot behavior
267 * 'I' Reboot if needed (default).
273 INT WINAPI LaunchINFSectionW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
275 FIXME("(%p, %p, %s, %i): stub\n", hWnd, hInst, debugstr_w(cmdline), show);
279 /***********************************************************************
280 * LaunchINFSectionExA (ADVPACK.@)
282 * See LaunchINFSectionExW.
284 HRESULT WINAPI LaunchINFSectionExA(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
289 TRACE("(%p, %p, %s, %i): stub\n", hWnd, hInst, debugstr_a(cmdline), show);
291 RtlCreateUnicodeStringFromAsciiz(&cmd, cmdline);
293 hr = LaunchINFSectionExW(hWnd, hInst, cmd.Buffer, show);
295 RtlFreeUnicodeString(&cmd);
300 /***********************************************************************
301 * LaunchINFSectionExW (ADVPACK.@)
303 * Installs an INF section with BACKUP/ROLLBACK capabilities.
306 * hWnd [I] Handle to parent window, NULL for desktop.
307 * hInst [I] Instance of the process.
308 * cmdline [I] Contains parameters in the order INF,section,CAB,flags,reboot.
309 * show [I] How the window should be shown.
316 * INF - Filename of the INF to launch.
317 * section - INF section to install.
318 * flags - see advpub.h.
319 * reboot - smart reboot behavior
321 * 'I' Reboot if needed (default).
325 * Doesn't handle the reboot flag.
327 HRESULT WINAPI LaunchINFSectionExW(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
329 LPWSTR cmdline_copy, cmdline_ptr;
334 TRACE("(%p, %p, %s, %d)\n", hWnd, hInst, debugstr_w(cmdline), show);
339 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
340 cmdline_ptr = cmdline_copy;
341 lstrcpyW(cmdline_copy, cmdline);
343 cabinfo.pszInf = get_parameter(&cmdline_ptr, ',');
344 cabinfo.pszSection = get_parameter(&cmdline_ptr, ',');
345 cabinfo.pszCab = get_parameter(&cmdline_ptr, ',');
347 flags = get_parameter(&cmdline_ptr, ',');
349 cabinfo.dwFlags = atolW(flags);
351 /* get the source path from the cab filename */
352 if (cabinfo.pszCab && *cabinfo.pszCab)
354 if (!is_full_path(cabinfo.pszCab))
357 lstrcpyW(cabinfo.szSrcPath, cabinfo.pszCab);
358 ptr = strrchrW(cabinfo.szSrcPath, '\\');
362 hr = ExecuteCabW(hWnd, &cabinfo, NULL);
365 HeapFree(GetProcessHeap(), 0, cmdline_copy);
370 HRESULT launch_exe(LPCWSTR cmd, LPCWSTR dir, HANDLE *phEXE)
373 PROCESS_INFORMATION pi;
375 if (phEXE) *phEXE = NULL;
377 ZeroMemory(&pi, sizeof(pi));
378 ZeroMemory(&si, sizeof(si));
381 if (!CreateProcessW(NULL, (LPWSTR)cmd, NULL, NULL, FALSE,
382 CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP,
383 NULL, dir, &si, &pi))
385 return HRESULT_FROM_WIN32(GetLastError());
388 CloseHandle(pi.hThread);
392 *phEXE = pi.hProcess;
393 return S_ASYNCHRONOUS;
396 /* wait for the child process to finish */
397 WaitForSingleObject(pi.hProcess, INFINITE);
398 CloseHandle(pi.hProcess);
403 /***********************************************************************
404 * RunSetupCommandA (ADVPACK.@)
406 * See RunSetupCommandW.
408 HRESULT WINAPI RunSetupCommandA(HWND hWnd, LPCSTR szCmdName,
409 LPCSTR szInfSection, LPCSTR szDir,
410 LPCSTR lpszTitle, HANDLE *phEXE,
411 DWORD dwFlags, LPVOID pvReserved)
413 UNICODE_STRING cmdname, infsec;
414 UNICODE_STRING dir, title;
417 TRACE("(%p, %s, %s, %s, %s, %p, %ld, %p)\n",
418 hWnd, debugstr_a(szCmdName), debugstr_a(szInfSection),
419 debugstr_a(szDir), debugstr_a(lpszTitle),
420 phEXE, dwFlags, pvReserved);
422 if (!szCmdName || !szDir)
425 RtlCreateUnicodeStringFromAsciiz(&cmdname, szCmdName);
426 RtlCreateUnicodeStringFromAsciiz(&infsec, szInfSection);
427 RtlCreateUnicodeStringFromAsciiz(&dir, szDir);
428 RtlCreateUnicodeStringFromAsciiz(&title, lpszTitle);
430 hr = RunSetupCommandW(hWnd, cmdname.Buffer, infsec.Buffer, dir.Buffer,
431 title.Buffer, phEXE, dwFlags, pvReserved);
433 RtlFreeUnicodeString(&cmdname);
434 RtlFreeUnicodeString(&infsec);
435 RtlFreeUnicodeString(&dir);
436 RtlFreeUnicodeString(&title);
441 /***********************************************************************
442 * RunSetupCommandW (ADVPACK.@)
444 * Executes an install section in an INF file or a program.
447 * hWnd [I] Handle to parent window, NULL for quiet mode
448 * szCmdName [I] Inf or EXE filename to execute
449 * szInfSection [I] Inf section to install, NULL for DefaultInstall
450 * szDir [I] Path to extracted files
451 * szTitle [I] Title of all dialogs
452 * phEXE [O] Handle of EXE to wait for
453 * dwFlags [I] Flags; see include/advpub.h
454 * pvReserved [I] Reserved
458 * S_ASYNCHRONOUS OK, required to wait on phEXE
459 * ERROR_SUCCESS_REBOOT_REQUIRED Reboot required
460 * E_INVALIDARG Invalid argument given
461 * HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION)
462 * Not supported on this Windows version
463 * E_UNEXPECTED Unexpected error
464 * HRESULT_FROM_WIN32(GetLastError()) Some other error
467 * INF install unimplemented.
469 HRESULT WINAPI RunSetupCommandW(HWND hWnd, LPCWSTR szCmdName,
470 LPCWSTR szInfSection, LPCWSTR szDir,
471 LPCWSTR lpszTitle, HANDLE *phEXE,
472 DWORD dwFlags, LPVOID pvReserved)
477 TRACE("(%p, %s, %s, %s, %s, %p, %ld, %p)\n",
478 hWnd, debugstr_w(szCmdName), debugstr_w(szInfSection),
479 debugstr_w(szDir), debugstr_w(lpszTitle),
480 phEXE, dwFlags, pvReserved);
483 FIXME("Unhandled flags: 0x%08lx\n", dwFlags);
485 if (!szCmdName || !szDir)
488 if (!(dwFlags & RSC_FLAG_INF))
489 return launch_exe(szCmdName, szDir, phEXE);
491 hinf = SetupOpenInfFileW(szCmdName, NULL, INF_STYLE_WIN4, NULL);
492 if (hinf == INVALID_HANDLE_VALUE)
493 return ADV_HRESULT(GetLastError());
495 hr = spapi_install(hinf, szInfSection, szDir);
497 SetupCloseInfFile(hinf);