ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

reg.c
Go to the documentation of this file.
00001 /*
00002  * Advpack registry functions
00003  *
00004  * Copyright 2004 Huw D M Davies
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
00019  */
00020 
00021 #include <stdarg.h>
00022 #include "windef.h"
00023 #include "winbase.h"
00024 #include "winreg.h"
00025 #include "winerror.h"
00026 #include "winuser.h"
00027 #include "winternl.h"
00028 #include "advpub.h"
00029 #include "wine/unicode.h"
00030 #include "wine/debug.h"
00031 
00032 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
00033 
00034 static const WCHAR REGINST[] = {'R','E','G','I','N','S','T',0};
00035 static const WCHAR Strings[] = {'S','t','r','i','n','g','s',0};
00036 static const WCHAR MOD_PATH[] = {'_','M','O','D','_','P','A','T','H',0};
00037 static const WCHAR SYS_MOD_PATH[] = {'_','S','Y','S','_','M','O','D','_','P','A','T','H',0};
00038 static const WCHAR SystemRoot[] = {'S','y','s','t','e','m','R','o','o','t',0};
00039 static const WCHAR escaped_SystemRoot[] = {'%','S','y','s','t','e','m','R','o','o','t','%',0};
00040 static const WCHAR quote[] = {'\"',0};
00041 
00042 static BOOL get_temp_ini_path(LPWSTR name)
00043 {
00044     WCHAR tmp_dir[MAX_PATH];
00045     WCHAR prefix[] = {'a','v','p',0};
00046 
00047     if(!GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir))
00048        return FALSE;
00049 
00050     if(!GetTempFileNameW(tmp_dir, prefix, 0, name))
00051         return FALSE;
00052     return TRUE;
00053 }
00054 
00055 static BOOL create_tmp_ini_file(HMODULE hm, WCHAR *ini_file)
00056 {
00057     HRSRC hrsrc;
00058     HGLOBAL hmem = NULL;
00059     DWORD rsrc_size, bytes_written;
00060     VOID *rsrc_data;
00061     HANDLE hf = INVALID_HANDLE_VALUE;
00062 
00063     if(!get_temp_ini_path(ini_file)) {
00064         ERR("Can't get temp ini file path\n");
00065         goto error;
00066     }
00067 
00068     if(!(hrsrc = FindResourceW(hm, REGINST, REGINST))) {
00069         ERR("Can't find REGINST resource\n");
00070         goto error;
00071     }
00072 
00073     rsrc_size = SizeofResource(hm, hrsrc);
00074     hmem = LoadResource(hm, hrsrc);
00075     rsrc_data = LockResource(hmem);
00076 
00077     if(!rsrc_data || !rsrc_size) {
00078         ERR("Can't load REGINST resource\n");
00079         goto error;
00080     }       
00081 
00082     if((hf = CreateFileW(ini_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
00083                          FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
00084         ERR("Unable to create temp ini file\n");
00085         goto error;
00086     }
00087     if(!WriteFile(hf, rsrc_data, rsrc_size, &bytes_written, NULL) || rsrc_size != bytes_written) {
00088         ERR("Write failed\n");
00089         goto error;
00090     }
00091     FreeResource(hmem);
00092     CloseHandle(hf);
00093     return TRUE;
00094 error:
00095     if(hmem) FreeResource(hmem);
00096     if(hf != INVALID_HANDLE_VALUE) CloseHandle(hf);
00097     return FALSE;
00098 }
00099 
00100 static void strentry_atow(const STRENTRYA *aentry, STRENTRYW *wentry)
00101 {
00102     DWORD name_len, val_len;
00103 
00104     name_len = MultiByteToWideChar(CP_ACP, 0, aentry->pszName, -1, NULL, 0);
00105     val_len = MultiByteToWideChar(CP_ACP, 0, aentry->pszValue, -1, NULL, 0);
00106 
00107     wentry->pszName = HeapAlloc(GetProcessHeap(), 0, name_len * sizeof(WCHAR));
00108     wentry->pszValue = HeapAlloc(GetProcessHeap(), 0, val_len * sizeof(WCHAR));
00109 
00110     MultiByteToWideChar(CP_ACP, 0, aentry->pszName, -1, wentry->pszName, name_len);
00111     MultiByteToWideChar(CP_ACP, 0, aentry->pszValue, -1, wentry->pszValue, val_len);
00112 }
00113 
00114 static STRTABLEW *strtable_atow(const STRTABLEA *atable)
00115 {
00116     STRTABLEW *wtable;
00117     DWORD j;
00118 
00119     wtable = HeapAlloc(GetProcessHeap(), 0, sizeof(STRTABLEW));
00120     wtable->pse = HeapAlloc(GetProcessHeap(), 0, atable->cEntries * sizeof(STRENTRYW));
00121     wtable->cEntries = atable->cEntries;
00122 
00123     for (j = 0; j < wtable->cEntries; j++)
00124         strentry_atow(&atable->pse[j], &wtable->pse[j]);
00125 
00126     return wtable;
00127 }
00128 
00129 static void free_strtable(STRTABLEW *wtable)
00130 {
00131     DWORD j;
00132 
00133     for (j = 0; j < wtable->cEntries; j++)
00134     {
00135         HeapFree(GetProcessHeap(), 0, wtable->pse[j].pszName);
00136         HeapFree(GetProcessHeap(), 0, wtable->pse[j].pszValue);
00137     }
00138 
00139     HeapFree(GetProcessHeap(), 0, wtable->pse);
00140     HeapFree(GetProcessHeap(), 0, wtable);
00141 }
00142 
00143 /***********************************************************************
00144  *          RegInstallA (advpack.@)
00145  *
00146  * See RegInstallW.
00147  */
00148 HRESULT WINAPI RegInstallA(HMODULE hm, LPCSTR pszSection, const STRTABLEA* pstTable)
00149 {
00150     UNICODE_STRING section;
00151     STRTABLEW *wtable;
00152     HRESULT hr;
00153 
00154     TRACE("(%p, %s, %p)\n", hm, debugstr_a(pszSection), pstTable);
00155 
00156     if (pstTable)
00157         wtable = strtable_atow(pstTable);
00158     else
00159         wtable = NULL;
00160 
00161     RtlCreateUnicodeStringFromAsciiz(&section, pszSection);
00162 
00163     hr = RegInstallW(hm, section.Buffer, wtable);
00164 
00165     if (pstTable)
00166         free_strtable(wtable);
00167 
00168     RtlFreeUnicodeString(&section);
00169 
00170     return hr;
00171 }
00172 
00173 static HRESULT write_predefined_strings(HMODULE hm, LPCWSTR ini_path)
00174 {
00175     WCHAR mod_path[MAX_PATH + 2];
00176     WCHAR sys_mod_path[MAX_PATH + 2];
00177     WCHAR sys_root[MAX_PATH];
00178 
00179     *mod_path = '\"';
00180     if (!GetModuleFileNameW(hm, mod_path + 1, sizeof(mod_path) / sizeof(WCHAR) - 2))
00181         return E_FAIL;
00182 
00183     lstrcatW(mod_path, quote);
00184     WritePrivateProfileStringW(Strings, MOD_PATH, mod_path, ini_path);
00185 
00186     *sys_root = '\0';
00187     GetEnvironmentVariableW(SystemRoot, sys_root, sizeof(sys_root) / sizeof(WCHAR));
00188 
00189     if(!strncmpiW(sys_root, mod_path + 1, strlenW(sys_root)))
00190     {
00191         *sys_mod_path = '\"';
00192         strcpyW(sys_mod_path + 1, escaped_SystemRoot);
00193         strcatW(sys_mod_path, mod_path + 1 + strlenW(sys_root));
00194     }
00195     else
00196     {
00197         FIXME("SYS_MOD_PATH needs more work\n");
00198         strcpyW(sys_mod_path, mod_path);
00199     }
00200 
00201     WritePrivateProfileStringW(Strings, SYS_MOD_PATH, sys_mod_path, ini_path);
00202 
00203     return S_OK;
00204 }
00205 
00206 /***********************************************************************
00207  *          RegInstallW (advpack.@)
00208  *
00209  * Loads an INF from a string resource, adds entries to the string
00210  * substitution table, and executes the INF.
00211  *
00212  * PARAMS
00213  *   hm         [I] Module that contains the REGINST resource.
00214  *   pszSection [I] The INF section to execute.
00215  *   pstTable   [I] Table of string substitutions.
00216  * 
00217  * RETURNS
00218  *   Success: S_OK.
00219  *   Failure: E_FAIL.
00220  */
00221 HRESULT WINAPI RegInstallW(HMODULE hm, LPCWSTR pszSection, const STRTABLEW* pstTable)
00222 {
00223     unsigned int i;
00224     CABINFOW cabinfo;
00225     WCHAR tmp_ini_path[MAX_PATH];
00226     HRESULT hr = E_FAIL;
00227 
00228     TRACE("(%p, %s, %p)\n", hm, debugstr_w(pszSection), pstTable);
00229 
00230     if(!create_tmp_ini_file(hm, tmp_ini_path))
00231         return E_FAIL;
00232 
00233     if (write_predefined_strings(hm, tmp_ini_path) != S_OK)
00234         goto done;
00235 
00236     /* Write the additional string table */
00237     if (pstTable)
00238     {
00239         for(i = 0; i < pstTable->cEntries; i++)
00240         {
00241             WCHAR tmp_value[MAX_PATH + 2];
00242     
00243             tmp_value[0] = '\"';
00244             lstrcpyW(tmp_value + 1, pstTable->pse[i].pszValue);
00245             lstrcatW(tmp_value, quote);
00246     
00247             WritePrivateProfileStringW(Strings, pstTable->pse[i].pszName, tmp_value, tmp_ini_path);
00248         }
00249     }
00250 
00251     /* flush cache */
00252     WritePrivateProfileStringW(NULL, NULL, NULL, tmp_ini_path);
00253 
00254     /* FIXME: read AdvOptions val for dwFlags */
00255     ZeroMemory(&cabinfo, sizeof(CABINFOW));
00256     cabinfo.pszInf = tmp_ini_path;
00257     cabinfo.pszSection = (LPWSTR)pszSection;
00258     cabinfo.dwFlags = 0;
00259 
00260     hr = ExecuteCabW(NULL, &cabinfo, NULL);
00261 
00262 done:
00263 
00264     DeleteFileW(tmp_ini_path);
00265 
00266     return hr;
00267 }
00268 
00269 /***********************************************************************
00270  *          RegRestoreAllA (advpack.@)
00271  *
00272  * See RegRestoreAllW.
00273  */
00274 HRESULT WINAPI RegRestoreAllA(HWND hWnd, LPSTR pszTitleString, HKEY hkBackupKey)
00275 {
00276     UNICODE_STRING title;
00277     HRESULT hr;
00278 
00279     TRACE("(%p, %s, %p)\n", hWnd, debugstr_a(pszTitleString), hkBackupKey);
00280 
00281     RtlCreateUnicodeStringFromAsciiz(&title, pszTitleString);
00282 
00283     hr = RegRestoreAllW(hWnd, title.Buffer, hkBackupKey);
00284 
00285     RtlFreeUnicodeString(&title);
00286 
00287     return hr;
00288 }
00289 
00290 /***********************************************************************
00291  *          RegRestoreAllW (advpack.@)
00292  *
00293  * Restores all saved registry entries.
00294  *
00295  * PARAMS
00296  *   hWnd           [I] Handle to the window used for the display.
00297  *   pszTitleString [I] Title of the window.
00298  *   hkBackupKey    [I] Handle to the backup key.
00299  *
00300  * RETURNS
00301  *   Success: S_OK.
00302  *   Failure: E_FAIL.
00303  *
00304  * BUGS
00305  *   Unimplemented.
00306  */
00307 HRESULT WINAPI RegRestoreAllW(HWND hWnd, LPWSTR pszTitleString, HKEY hkBackupKey)
00308 {
00309     FIXME("(%p, %s, %p) stub\n", hWnd, debugstr_w(pszTitleString), hkBackupKey);
00310     
00311     return E_FAIL;   
00312 }
00313 
00314 /***********************************************************************
00315  *          RegSaveRestoreA (advpack.@)
00316  *
00317  * See RegSaveRestoreW.
00318  */
00319 HRESULT WINAPI RegSaveRestoreA(HWND hWnd, LPCSTR pszTitleString, HKEY hkBackupKey,
00320                                LPCSTR pcszRootKey, LPCSTR pcszSubKey,
00321                                LPCSTR pcszValueName, DWORD dwFlags)
00322 {
00323     UNICODE_STRING title, root, subkey, value;
00324     HRESULT hr;
00325 
00326     TRACE("(%p, %s, %p, %s, %s, %s, %d)\n", hWnd, debugstr_a(pszTitleString),
00327           hkBackupKey, debugstr_a(pcszRootKey), debugstr_a(pcszSubKey),
00328           debugstr_a(pcszValueName), dwFlags);
00329 
00330     RtlCreateUnicodeStringFromAsciiz(&title, pszTitleString);
00331     RtlCreateUnicodeStringFromAsciiz(&root, pcszRootKey);
00332     RtlCreateUnicodeStringFromAsciiz(&subkey, pcszSubKey);
00333     RtlCreateUnicodeStringFromAsciiz(&value, pcszValueName);
00334 
00335     hr = RegSaveRestoreW(hWnd, title.Buffer, hkBackupKey, root.Buffer,
00336                          subkey.Buffer, value.Buffer, dwFlags);
00337 
00338     RtlFreeUnicodeString(&title);
00339     RtlFreeUnicodeString(&root);
00340     RtlFreeUnicodeString(&subkey);
00341     RtlFreeUnicodeString(&value);
00342 
00343     return hr;
00344 }
00345 
00346 /***********************************************************************
00347  *          RegSaveRestoreW (advpack.@)
00348  *
00349  * Saves or restores the specified registry value.
00350  *
00351  * PARAMS
00352  *   hWnd           [I] Handle to the window used for the display.
00353  *   pszTitleString [I] Title of the window.
00354  *   hkBackupKey    [I] Key used to store the backup data.
00355  *   pcszRootKey    [I] Root key of the registry value
00356  *   pcszSubKey     [I] Sub key of the registry value.
00357  *   pcszValueName  [I] Value to save or restore. 
00358  *   dwFlags        [I] See advpub.h.
00359  * 
00360  * RETURNS
00361  *   Success: S_OK.
00362  *   Failure: E_FAIL.
00363  *
00364  * BUGS
00365  *   Unimplemented.
00366  */
00367 HRESULT WINAPI RegSaveRestoreW(HWND hWnd, LPCWSTR pszTitleString, HKEY hkBackupKey,
00368                                LPCWSTR pcszRootKey, LPCWSTR pcszSubKey,
00369                                LPCWSTR pcszValueName, DWORD dwFlags)
00370 {
00371     FIXME("(%p, %s, %p, %s, %s, %s, %d): stub\n", hWnd, debugstr_w(pszTitleString),
00372           hkBackupKey, debugstr_w(pcszRootKey), debugstr_w(pcszSubKey),
00373           debugstr_w(pcszValueName), dwFlags);
00374 
00375     return E_FAIL;   
00376 }
00377 
00378 /***********************************************************************
00379  *          RegSaveRestoreOnINFA (advpack.@)
00380  *
00381  * See RegSaveRestoreOnINFW.
00382  */
00383 HRESULT WINAPI RegSaveRestoreOnINFA(HWND hWnd, LPCSTR pszTitle, LPCSTR pszINF,
00384                                     LPCSTR pszSection, HKEY hHKLMBackKey,
00385                                     HKEY hHKCUBackKey, DWORD dwFlags)
00386 {
00387     UNICODE_STRING title, inf, section;
00388     HRESULT hr;
00389 
00390     TRACE("(%p, %s, %s, %s, %p, %p, %d)\n", hWnd, debugstr_a(pszTitle),
00391           debugstr_a(pszINF), debugstr_a(pszSection),
00392           hHKLMBackKey, hHKCUBackKey, dwFlags);
00393 
00394     RtlCreateUnicodeStringFromAsciiz(&title, pszTitle);
00395     RtlCreateUnicodeStringFromAsciiz(&inf, pszINF);
00396     RtlCreateUnicodeStringFromAsciiz(&section, pszSection);
00397 
00398     hr = RegSaveRestoreOnINFW(hWnd, title.Buffer, inf.Buffer, section.Buffer,
00399                               hHKLMBackKey, hHKCUBackKey, dwFlags);
00400 
00401     RtlFreeUnicodeString(&title);
00402     RtlFreeUnicodeString(&inf);
00403     RtlFreeUnicodeString(&section);
00404 
00405     return hr;
00406 }
00407 
00408 /***********************************************************************
00409  *          RegSaveRestoreOnINFW (advpack.@)
00410  *
00411  * Saves or restores the specified INF Reg section.
00412  *
00413  * PARAMS
00414  *   hWnd         [I] Handle to the window used for the display.
00415  *   pszTitle     [I] Title of the window.
00416  *   pszINF       [I] Filename of the INF.
00417  *   pszSection   [I] Section to save or restore.
00418  *   hHKLMBackKey [I] Opened key in HKLM to store data.
00419  *   hHKCUBackKey [I] Opened key in HKCU to store data.
00420  *   dwFlags      [I] See advpub.h
00421  *
00422  * RETURNS
00423  *   Success: S_OK.
00424  *   Failure: E_FAIL.
00425  *
00426  * BUGS
00427  *   Unimplemented.
00428  */
00429 HRESULT WINAPI RegSaveRestoreOnINFW(HWND hWnd, LPCWSTR pszTitle, LPCWSTR pszINF,
00430                                     LPCWSTR pszSection, HKEY hHKLMBackKey,
00431                                     HKEY hHKCUBackKey, DWORD dwFlags)
00432 {
00433     FIXME("(%p, %s, %s, %s, %p, %p, %d): stub\n", hWnd, debugstr_w(pszTitle),
00434           debugstr_w(pszINF), debugstr_w(pszSection),
00435           hHKLMBackKey, hHKCUBackKey, dwFlags);
00436 
00437     return E_FAIL;   
00438 }

Generated on Fri May 25 2012 04:14:54 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.