2 * Advpack registry functions
4 * Copyright 2004 Huw D M Davies
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
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
35 static const WCHAR REGINST[] = {'R','E','G','I','N','S','T',0};
36 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
37 static const WCHAR MOD_PATH[] = {'_','M','O','D','_','P','A','T','H',0};
38 static const WCHAR SYS_MOD_PATH[] = {'_','S','Y','S','_','M','O','D','_','P','A','T','H',0};
39 static const WCHAR SystemRoot[] = {'S','y','s','t','e','m','R','o','o','t',0};
40 static const WCHAR escaped_SystemRoot[] = {'%','S','y','s','t','e','m','R','o','o','t','%',0};
42 static BOOL get_temp_ini_path(LPWSTR name)
44 WCHAR tmp_dir[MAX_PATH];
45 WCHAR prefix[] = {'a','v','p',0};
47 if(!GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir))
50 if(!GetTempFileNameW(tmp_dir, prefix, 0, name))
55 static BOOL create_tmp_ini_file(HMODULE hm, WCHAR *ini_file)
59 DWORD rsrc_size, bytes_written;
61 HANDLE hf = INVALID_HANDLE_VALUE;
63 if(!get_temp_ini_path(ini_file)) {
64 ERR("Can't get temp ini file path\n");
68 if(!(hrsrc = FindResourceW(hm, REGINST, REGINST))) {
69 ERR("Can't find REGINST resource\n");
73 rsrc_size = SizeofResource(hm, hrsrc);
74 hmem = LoadResource(hm, hrsrc);
75 rsrc_data = LockResource(hmem);
77 if(!rsrc_data || !rsrc_size) {
78 ERR("Can't load REGINST resource\n");
82 if((hf = CreateFileW(ini_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
83 FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
84 ERR("Unable to create temp ini file\n");
87 if(!WriteFile(hf, rsrc_data, rsrc_size, &bytes_written, NULL) || rsrc_size != bytes_written) {
88 ERR("Write failed\n");
95 if(hmem) FreeResource(hmem);
96 if(hf != INVALID_HANDLE_VALUE) CloseHandle(hf);
100 /***********************************************************************
101 * RegInstall (advpack.@)
103 * Loads an INF from a string resource, adds entries to the string
104 * substitution table, and executes the INF.
107 * hm [I] Module that contains the REGINST resouce.
108 * pszSection [I] The INF section to execute.
109 * pstTable [I] Table of string substitutions.
115 HRESULT WINAPI RegInstall(HMODULE hm, LPCSTR pszSection, LPCSTRTABLE pstTable)
118 WCHAR tmp_ini_path[MAX_PATH];
119 WCHAR mod_path[MAX_PATH + 2], sys_mod_path[MAX_PATH + 2], sys_root[MAX_PATH];
121 WCHAR quote[] = {'\"',0};
122 UNICODE_STRING section;
124 TRACE("(%p %s %p)\n", hm, pszSection, pstTable);
126 if (pstTable) for(i = 0; i < pstTable->cEntries; i++)
127 TRACE("%d: %s -> %s\n", i, pstTable->pse[i].pszName,
128 pstTable->pse[i].pszValue);
130 if(!create_tmp_ini_file(hm, tmp_ini_path))
133 /* Write a couple of pre-defined strings */
135 GetModuleFileNameW(hm, mod_path + 1, sizeof(mod_path)/sizeof(WCHAR) - 2);
136 strcatW(mod_path, quote);
137 WritePrivateProfileStringW(Strings, MOD_PATH, mod_path, tmp_ini_path);
140 GetEnvironmentVariableW(SystemRoot, sys_root, sizeof(sys_root)/sizeof(WCHAR));
141 if(!strncmpiW(sys_root, mod_path + 1, strlenW(sys_root))) {
142 sys_mod_path[0] = '\"';
143 strcpyW(sys_mod_path + 1, escaped_SystemRoot);
144 strcatW(sys_mod_path, mod_path + 1 + strlenW(sys_root));
146 FIXME("SYS_MOD_PATH needs more work\n");
147 strcpyW(sys_mod_path, mod_path);
149 WritePrivateProfileStringW(Strings, SYS_MOD_PATH, sys_mod_path, tmp_ini_path);
151 /* Write the additional string table */
152 if (pstTable) for(i = 0; i < pstTable->cEntries; i++) {
153 char tmp_value[MAX_PATH + 2];
154 UNICODE_STRING name, value;
156 strcpy(tmp_value + 1, pstTable->pse[i].pszValue);
157 strcat(tmp_value, "\"");
158 RtlCreateUnicodeStringFromAsciiz(&name, pstTable->pse[i].pszName);
159 RtlCreateUnicodeStringFromAsciiz(&value, tmp_value);
160 WritePrivateProfileStringW(Strings, name.Buffer, value.Buffer, tmp_ini_path);
161 RtlFreeUnicodeString(&name);
162 RtlFreeUnicodeString(&value);
165 WritePrivateProfileStringW(NULL, NULL, NULL, tmp_ini_path);
168 if((hinf = SetupOpenInfFileW(tmp_ini_path, NULL, INF_STYLE_WIN4, NULL)) ==
169 INVALID_HANDLE_VALUE) {
170 ERR("Setupapi can't open inf\n");
174 /* append any layout files */
175 SetupOpenAppendInfFileW(NULL, hinf, NULL);
177 /* Need to do a lot more here */
178 RtlCreateUnicodeStringFromAsciiz(§ion, pszSection);
179 SetupInstallFromInfSectionW(NULL, hinf, section.Buffer,
180 SPINST_INIFILES | SPINST_REGISTRY | SPINST_PROFILEITEMS,
181 HKEY_LOCAL_MACHINE, NULL, 0, NULL, NULL, NULL, NULL);
182 RtlFreeUnicodeString(§ion);
185 SetupCloseInfFile(hinf);
186 DeleteFileW(tmp_ini_path);
191 /***********************************************************************
192 * RegRestoreAll (advpack.@)
194 * Restores all saved registry entries.
197 * hWnd [I] Handle to the window used for the display.
198 * pszTitleString [I] Title of the window.
199 * hkBackupKey [I] Handle to the backup key.
208 HRESULT WINAPI RegRestoreAll(HWND hWnd, PSTR pszTitleString, HKEY hkBackupKey)
210 FIXME("(%p, %p, %p) stub\n", hWnd, pszTitleString, hkBackupKey);
215 /***********************************************************************
216 * RegSaveRestore (advpack.@)
218 * Saves or restores the specified registry value.
221 * hWnd [I] Handle to the window used for the display.
222 * pszTitleString [I] Title of the window.
223 * hkBackupKey [I] Key used to store the backup data.
224 * pcszRootKey [I] Root key of the registry value
225 * pcszSubKey [I] Sub key of the registry value.
226 * pcszValueName [I] Value to save or restore.
227 * dwFlags [I] See advpub.h.
236 HRESULT WINAPI RegSaveRestore(HWND hWnd, PCSTR pszTitleString, HKEY hkBackupKey,
237 PCSTR pcszRootKey, PCSTR pcszSubKey,
238 PCSTR pcszValueName, DWORD dwFlags)
240 FIXME("(%p, %p, %p, %p, %p, %p, %ld) stub\n", hWnd, pszTitleString,
241 hkBackupKey, pcszRootKey, pcszSubKey, pcszValueName, dwFlags);
246 /***********************************************************************
247 * RegSaveRestoreOnINF (advpack.@)
249 * Saves or restores the specified INF Reg section.
252 * hWnd [I] Handle to the window used for the display.
253 * pszTitle [I] Title of the window.
254 * pszINF [I] Filename of the INF.
255 * pszSection [I] Section to save or restore.
256 * hHKLMBackKey [I] Opened key in HKLM to store data.
257 * hHKCUBackKey [I] Opened key in HKCU to store data.
258 * dwFlags [I] See advpub.h
267 HRESULT WINAPI RegSaveRestoreOnINF(HWND hWnd, PCSTR pszTitle, PCSTR pszINF,
268 PCSTR pszSection, HKEY hHKLMBackKey,
269 HKEY hHKCUBackKey, DWORD dwFlags)
271 FIXME("(%p, %p, %p, %p, %p, %p, %ld) stub\n", hWnd, pszTitle, pszINF,
272 pszSection, hHKLMBackKey, hHKCUBackKey, dwFlags);