//----- (00464D32) -------------------------------------------------------- void __fastcall WriteWindowsRegistryInt(const char *pKey, int val) { const char *lpValueName; // [sp+4h] [bp-1Ch]@1 BYTE Data[4]; // [sp+8h] [bp-18h]@1 DWORD dwDisposition; // [sp+Ch] [bp-14h]@2 HKEY hKey; // [sp+10h] [bp-10h]@1 HKEY phkResult; // [sp+14h] [bp-Ch]@1 HKEY v7; // [sp+18h] [bp-8h]@1 HKEY v8; // [sp+1Ch] [bp-4h]@1 *(int *)Data = val; lpValueName = pKey; v8 = 0; v7 = 0; hKey = 0; phkResult = 0; if ( !RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE", 0, 0x2001Fu, &hKey) ) { if ( !RegCreateKeyExA(hKey, "New World Computing", 0, "", 0, 0xF003Fu, 0, &phkResult, &dwDisposition) ) { if ( !RegCreateKeyExA(phkResult, "Might and Magic VII", 0, "", 0, 0xF003Fu, 0, &v7, &dwDisposition) ) { if ( !RegCreateKeyExA(v7, "1.0", 0, "", 0, 0xF003Fu, 0, &v8, &dwDisposition) ) { RegSetValueExA(v8, lpValueName, 0, 4, Data, 4); RegCloseKey(v8); } RegCloseKey(v7); } RegCloseKey(phkResult); } RegCloseKey(hKey); } }
BOOL My_RegCreateKeyExA() { HKEY hKey=NULL; LPCSTR lpSubKey=NULL; DWORD Reserved=NULL; LPSTR lpClass=NULL; DWORD dwOptions=NULL; REGSAM samDesired=NULL; LPSECURITY_ATTRIBUTES lpSecurityAttributes=NULL; PHKEY phkResult=NULL; LPDWORD lpdwDisposition=NULL; LONG returnVal_Real = NULL; LONG returnVal_Intercepted = NULL; DWORD error_Real = 0; DWORD error_Intercepted = 0; __try{ disableInterception(); returnVal_Real = RegCreateKeyExA (hKey,lpSubKey,Reserved,lpClass,dwOptions,samDesired,lpSecurityAttributes,phkResult,lpdwDisposition); error_Real = GetLastError(); enableInterception(); returnVal_Intercepted = RegCreateKeyExA (hKey,lpSubKey,Reserved,lpClass,dwOptions,samDesired,lpSecurityAttributes,phkResult,lpdwDisposition); error_Intercepted = GetLastError(); }__except(puts("in filter"), 1){puts("exception caught");} return ((returnVal_Real == returnVal_Intercepted) && (error_Real == error_Intercepted)); }
BOOL RegisterServer() { DWORD dw; HKEY hKey; HKEY hSubKey; BOOL fRet; char achIMEKey[ARRAYSIZE(c_szInfoKeyPrefix) + CLSID_STRLEN]; TCHAR achFileName[MAX_PATH]; if (!CLSIDToStringA(c_clsidTextService, achIMEKey + ARRAYSIZE(c_szInfoKeyPrefix) - 1)) return FALSE; memcpy(achIMEKey, c_szInfoKeyPrefix, sizeof(c_szInfoKeyPrefix) - 1); if (fRet = RegCreateKeyExA(HKEY_CLASSES_ROOT, achIMEKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dw) == ERROR_SUCCESS) { fRet &= RegSetValueExA(hKey, NULL, 0, REG_SZ, (BYTE *) TEXTSERVICE_DESC_A, (strlen(TEXTSERVICE_DESC_A) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS; if (fRet &= RegCreateKeyExA(hKey, c_szInProcSvr32, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, &dw) == ERROR_SUCCESS) { dw = GetModuleFileName(g_hInst, achFileName, ARRAYSIZE(achFileName)); fRet &= RegSetValueEx(hSubKey, NULL, 0, REG_SZ, (BYTE *) achFileName, (lstrlen(achFileName) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS; fRet &= RegSetValueExA(hSubKey, c_szModelName, 0, REG_SZ, (BYTE *) TEXTSERVICE_MODEL, (strlen(TEXTSERVICE_MODEL) + 1)) == ERROR_SUCCESS; RegCloseKey(hSubKey); } RegCloseKey(hKey); } return fRet; }
static HKEY GetAppProfileKey() { if (!regapp || !regcomp) return NULL; HKEY hsoft = NULL; HKEY hcomp = NULL; HKEY happ = NULL; if (RegOpenKeyExA(HKEY_CURRENT_USER, "software", 0, KEY_WRITE|KEY_READ, &hsoft) == ERROR_SUCCESS) { DWORD dw; if (RegCreateKeyExA(hsoft, COMPANY, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hcomp, &dw) == ERROR_SUCCESS) { RegCreateKeyExA(hcomp, APPNAME, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &happ, &dw); } } if (hsoft != NULL) RegCloseKey(hsoft); if (hcomp != NULL) RegCloseKey(hcomp); return happ; }
static void test_delete(void) { HKEY hkey, hsubkey; LONG err; DWORD r; const DWORD deadbeef = 0xdeadbeef; run_reg_exe("reg delete", &r); ok(r == REG_EXIT_FAILURE, "got exit code %d, expected 1\n", r); run_reg_exe("reg delete /?", &r); ok(r == REG_EXIT_SUCCESS, "got exit code %d, expected 0\n", r); err = RegCreateKeyExA(HKEY_CURRENT_USER, KEY_BASE, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL); ok(err == ERROR_SUCCESS, "got %d\n", err); err = RegSetValueExA(hkey, "foo", 0, REG_DWORD, (LPBYTE)&deadbeef, sizeof(deadbeef)); ok(err == ERROR_SUCCESS, "got %d\n" ,err); err = RegSetValueExA(hkey, "bar", 0, REG_DWORD, (LPBYTE)&deadbeef, sizeof(deadbeef)); ok(err == ERROR_SUCCESS, "got %d\n" ,err); err = RegSetValueExA(hkey, "", 0, REG_DWORD, (LPBYTE)&deadbeef, sizeof(deadbeef)); ok(err == ERROR_SUCCESS, "got %d\n" ,err); err = RegCreateKeyExA(hkey, "subkey", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hsubkey, NULL); ok(err == ERROR_SUCCESS, "got %d\n" ,err); RegCloseKey(hsubkey); run_reg_exe("reg delete HKCU\\" KEY_BASE " /v bar /f", &r); ok(r == REG_EXIT_SUCCESS, "got exit code %d, expected 0\n", r); err = RegQueryValueExA(hkey, "bar", NULL, NULL, NULL, NULL); ok(err == ERROR_FILE_NOT_FOUND, "got %d\n", err); run_reg_exe("reg delete HKCU\\" KEY_BASE " /ve /f", &r); ok(r == REG_EXIT_SUCCESS, "got exit code %d, expected 0\n", r); err = RegQueryValueExA(hkey, "", NULL, NULL, NULL, NULL); ok(err == ERROR_FILE_NOT_FOUND, "got %d, expected 2\n", err); run_reg_exe("reg delete HKCU\\" KEY_BASE " /va /f", &r); ok(r == REG_EXIT_SUCCESS, "got exit code %d, expected 0\n", r); err = RegQueryValueExA(hkey, "foo", NULL, NULL, NULL, NULL); ok(err == ERROR_FILE_NOT_FOUND, "got %d\n", err); err = RegOpenKeyExA(hkey, "subkey", 0, KEY_READ, &hsubkey); ok(err == ERROR_SUCCESS, "got %d\n", err); RegCloseKey(hsubkey); RegCloseKey(hkey); run_reg_exe("reg delete HKCU\\" KEY_BASE " /f", &r); ok(r == REG_EXIT_SUCCESS, "got exit code %d, expected 0\n", r); err = RegOpenKeyExA(HKEY_CURRENT_USER, KEY_BASE, 0, KEY_READ, &hkey); ok(err == ERROR_FILE_NOT_FOUND, "got %d\n", err); run_reg_exe("reg delete HKCU\\" KEY_BASE " /f", &r); ok(r == REG_EXIT_FAILURE, "got exit code %u\n", r); }
//----- (00464BEF) -------------------------------------------------------- void __fastcall ReadWindowsRegistryString(const char *pKeyName, char *pOutString, int uBufLen, const char *pDefaultValue) { LSTATUS (__stdcall *v4)(HKEY); // esi@1 LSTATUS result; // eax@7 DWORD Type; // [sp+Ch] [bp-24h]@5 LPCSTR lpValueName; // [sp+10h] [bp-20h]@1 DWORD cbData; // [sp+14h] [bp-1Ch]@1 LPBYTE Dest; // [sp+18h] [bp-18h]@1 DWORD dwDisposition; // [sp+1Ch] [bp-14h]@2 HKEY phkResult; // [sp+20h] [bp-10h]@1 HKEY hKey; // [sp+24h] [bp-Ch]@1 HKEY v13; // [sp+28h] [bp-8h]@1 HKEY v14; // [sp+2Ch] [bp-4h]@1 cbData = uBufLen; Dest = (LPBYTE)pOutString; lpValueName = pKeyName; v14 = 0; v13 = 0; hKey = 0; phkResult = 0; v4 = RegCloseKey; if ( RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE", 0, 0x2001Fu, &hKey) || RegCreateKeyExA(hKey, "New World Computing", 0, "", 0, 0xF003Fu, 0, &phkResult, &dwDisposition) || RegCreateKeyExA(phkResult, "Might and Magic VII", 0, "", 0, 0xF003Fu, 0, &v13, &dwDisposition) || RegCreateKeyExA(v13, "1.0", 0, "", 0, 0xF003Fu, 0, &v14, &dwDisposition) ) { result = (LSTATUS)strncpy((char *)Dest, pDefaultValue, uBufLen); } else if ( RegQueryValueExA(v14, lpValueName, 0, &Type, Dest, &cbData) ) { RegCloseKey(v14); v14 = 0; result = (LSTATUS)strncpy((char *)Dest, pDefaultValue, uBufLen); } else { v4 = RegCloseKey; RegCloseKey(v14); v14 = 0; RegCloseKey(v13); v13 = 0; RegCloseKey(phkResult); phkResult = 0; result = RegCloseKey(hKey); hKey = 0; } if ( v14 ) result = v4(v14); if ( v13 ) result = v4(v13); if ( hKey ) result = v4(hKey); if ( phkResult ) result = v4(phkResult); }
/* good1() uses the GoodSink on both sides of the "if" statement */ static void good1() { if(globalReturnsTrueOrFalse()) { { char * keyName = "TEST\\TestKey"; HKEY hKey; /* FIX: Call RegCreateKeyExA() without KEY_ALL_ACCESS as the 6th parameter to limit access */ if (RegCreateKeyExA( HKEY_CURRENT_USER, keyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS) { printLine("Registry key could not be created"); } else { printLine("Registry key created successfully"); RegCloseKey(hKey); } } } else { { char * keyName = "TEST\\TestKey"; HKEY hKey; /* FIX: Call RegCreateKeyExA() without KEY_ALL_ACCESS as the 6th parameter to limit access */ if (RegCreateKeyExA( HKEY_CURRENT_USER, keyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS) { printLine("Registry key could not be created"); } else { printLine("Registry key created successfully"); RegCloseKey(hKey); } } } }
VOID SaveSetting(VOID) { HKEY hCompanyKey, hSoftwareKey; LONG result; INT i, n; DWORD dwDisp, dwSize; float e; CHAR szValue[256]; result = RegCreateKeyExA(HKEY_CURRENT_USER, pszCompany, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hCompanyKey, &dwDisp); if (result != ERROR_SUCCESS) return; result = RegCreateKeyExA(hCompanyKey, progname, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hSoftwareKey, &dwDisp); if (result == ERROR_SUCCESS) { for (i = 0; i < hack_argcount; i++) { switch (hack_arginfo[i].type) { case t_Bool: if (*(Bool *)hack_arginfo[i].data) strcpy(szValue, "True"); else strcpy(szValue, "False"); break; case t_Int: n = *(INT *)hack_arginfo[i].data; sprintf(szValue, "%d", n); break; case t_Float: e = *(float *)hack_arginfo[i].data; sprintf(szValue, "%g", e); break; case t_String: strcpy(szValue, *(char **)hack_arginfo[i].data); break; } dwSize = (strlen(szValue) + 1) * sizeof(CHAR); RegSetValueExA(hSoftwareKey, hack_arginfo[i].name, 0, REG_SZ, (LPBYTE)szValue, dwSize); } RegCloseKey(hSoftwareKey); } RegCloseKey(hCompanyKey); }
static void init_original_display_mode(void) { BOOL success = FALSE; HKEY mac_driver_hkey, parent_hkey; DWORD disposition; struct macdrv_display *displays = NULL; int num_displays, i; if (inited_original_display_mode) return; /* @@ Wine registry key: HKLM\Software\Wine\Mac Driver */ if (RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Mac Driver", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &mac_driver_hkey, NULL)) return; /* @@ Wine registry key: HKLM\Software\Wine\Mac Driver\Initial Display Mode */ if (RegCreateKeyExA(mac_driver_hkey, initial_mode_key, 0, NULL, REG_OPTION_VOLATILE, KEY_WRITE, NULL, &parent_hkey, &disposition)) { parent_hkey = NULL; goto fail; } /* If we didn't create a new key, then it already existed. Something already stored the initial display mode since Wine was started. We don't want to overwrite it. */ if (disposition != REG_CREATED_NEW_KEY) goto done; if (macdrv_get_displays(&displays, &num_displays)) goto fail; for (i = 0; i < num_displays; i++) { if (!write_display_settings(parent_hkey, displays[i].displayID)) goto fail; } done: success = TRUE; fail: macdrv_free_displays(displays); RegCloseKey(parent_hkey); if (!success && parent_hkey) RegDeleteTreeA(mac_driver_hkey, initial_mode_key); RegCloseKey(mac_driver_hkey); if (success) inited_original_display_mode = TRUE; }
int CGlobalSettings::SetGlobalValue(const char *product, const char *key, const char *value, const char *buffer) { HKEY hKey,hSubKey; DWORD dwLen; cvs::string regkey; if(!isAdmin()) return -1; if(!product || !strcmp(product,"cvsnt")) regkey="Software\\CVS"; else cvs::sprintf(regkey,64,"Software\\March Hare Software Ltd\\%s",product); if(RegOpenKeyExA(HKEY_LOCAL_MACHINE,regkey.c_str(),0,KEY_READ|KEY_WRITE,&hKey) && RegCreateKeyExA(HKEY_LOCAL_MACHINE,regkey.c_str(),0,NULL,0,KEY_READ|KEY_WRITE,NULL,&hKey,NULL)) { return -1; // Couldn't open or create key } if(key) { if(RegOpenKeyExA(hKey,key,0,KEY_WRITE,&hSubKey) && RegCreateKeyExA(hKey,key,0,NULL,0,KEY_WRITE,NULL,&hSubKey,NULL)) { RegCloseKey(hKey); return -1; // Couldn't open or create key } RegCloseKey(hKey); hKey=hSubKey; } if(!buffer) { RegDeleteValueA(hKey,value); } else { dwLen=(DWORD)strlen(buffer); if(RegSetValueExA(hKey,value,0,REG_SZ,(LPBYTE)buffer,dwLen+1)) { RegCloseKey(hKey); return -1; } } RegCloseKey(hKey); return 0; }
static void setVolumeIcon(char letter, bool create) { HRESULT hr; WCHAR data[MAX_PATH]; DWORD cbDataSize = sizeof(data); HKEY hKey; char path[] = "Software\\Classes\\Applications\\explorer.exe\\Drives\\a\\DefaultIcon"; path[sizeof(path)-sizeof("DefaultIcon")-2] = letter; if (create) { hr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\PCloud\\pCloud", 0, KEY_READ, &hKey); if (!hr) { hr = RegQueryValueEx(hKey, L"Install_Dir", NULL, NULL, (LPBYTE)data, &cbDataSize); wcscat(data, L"\\pCloud.exe,0"); RegCloseKey(hKey); if (!hr) { hr = RegCreateKeyExA(HKEY_LOCAL_MACHINE, path, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL); if(!hr) { hr = RegSetValueEx(hKey, NULL, 0, REG_SZ, (LPBYTE)data, 2*(wcslen(data)+1)); RegCloseKey(hKey); } } } } else { hr = RegDeleteKeyA(HKEY_LOCAL_MACHINE, path); } }
/****************************************************************************** * CheckTokenMembership [ADVAPI32.@] * * PARAMS * TokenHandle [] * SidToCheck [] * IsMember [] */ BOOL WINAPI CheckTokenMembership( HANDLE TokenHandle, PSID SidToCheck, PBOOL IsMember ) { FIXME("(0x%08x %p %p) stub!\n", TokenHandle, SidToCheck, IsMember); if (RunAsAdmin == -1) { HKEY hKey = 0; char buf[16]; DWORD size = sizeof (buf); RunAsAdmin = 1; if (!RegCreateKeyExA (HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\wine", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL) && !RegQueryValueExA (hKey, "NotAdministrator", 0, NULL, (LPBYTE)buf, &size) && size) { if ((buf[0] == 'y') || (buf[0] == 'Y') || (buf[0] == 't') || (buf[0] == 'T') || (buf[0] == '1')) RunAsAdmin = 0; } if (hKey) RegCloseKey (hKey); } *IsMember = RunAsAdmin; return TRUE; }
/*********************************************************************** * register_progids */ static HRESULT register_progids(struct progid const *list) { LONG res = ERROR_SUCCESS; for (; res == ERROR_SUCCESS && list->name; ++list) { WCHAR buf[39]; HKEY progid_key; res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->name, 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &progid_key, NULL); if (res != ERROR_SUCCESS) goto error_close_clsid_key; res = RegSetValueExA(progid_key, NULL, 0, REG_SZ, (CONST BYTE*)list->description, strlen(list->description) + 1); if (res != ERROR_SUCCESS) goto error_close_clsid_key; StringFromGUID2(list->clsid, buf, 39); res = register_key_defvalueW(progid_key, clsid_keyname, buf); if (res != ERROR_SUCCESS) goto error_close_clsid_key; if (list->curver) { res = register_key_defvalueA(progid_key, curver_keyname, list->curver); if (res != ERROR_SUCCESS) goto error_close_clsid_key; } error_close_clsid_key: RegCloseKey(progid_key); } return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK; }
Registry::Registry(std::string const & subKey, OpenMode openMode, HKEY root) { if (openMode == OpenMode::Create) { check( RegCreateKeyExA( root, subKey.size() ? subKey.c_str() : nullptr, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE | DELETE, nullptr, &hKey_, nullptr), "Error creating key: " + subKey); } else { REGSAM mode; switch (openMode) { case OpenMode::Read: mode = KEY_READ; break; case OpenMode::ReadWrite: mode = KEY_READ | KEY_WRITE | DELETE; break; default: throw std::runtime_error("Unknown OpenMode."); } check( RegOpenKeyExA(root, subKey.size() ? subKey.c_str() : nullptr, 0, mode, &hKey_), "Error opening key: " + subKey); } }
bool heapTraceEXE(const char* name) { std::string key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\"; key += name; HKEY hKey = NULL; LONG lRet = RegCreateKeyExA( HKEY_LOCAL_MACHINE, key.c_str(), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL ); if (lRet != ERROR_SUCCESS) return false; DWORD dw = 1; RegSetValueExA( hKey, "TracingFlags", 0, REG_DWORD, (const BYTE*)&dw, sizeof(dw) ); RegCloseKey(hKey); return true; }
static BOOL write_registry_settings(const DEVMODEW *dm) { char wine_x11_reg_key[128]; HKEY hkey; BOOL ret = TRUE; if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key))) return FALSE; if (RegCreateKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, NULL, REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hkey, NULL)) return FALSE; #define set_value(name, data) \ if (RegSetValueExA(hkey, name, 0, REG_DWORD, (LPBYTE)(data), sizeof(DWORD))) \ ret = FALSE set_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel); set_value("DefaultSettings.XResolution", &dm->dmPelsWidth); set_value("DefaultSettings.YResolution", &dm->dmPelsHeight); set_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency); set_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags); set_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x); set_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y); set_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation); set_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput); #undef set_value RegCloseKey(hkey); return ret; }
int KL1_OpenHKey (HKEY& hkey) { KL1_CheckOS(); if (g_os == KL1_OS_9X) { char *key = "System\\CurrentControlSet\\Services\\VXD\\kl1\\Parameters"; if (RegCreateKeyExA(HKEY_LOCAL_MACHINE, key, 0, "", REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, 0, &hkey, 0)!= ERROR_SUCCESS) { return KL1_ERR; } } else { WCHAR *key = L"System\\CurrentControlSet\\Services\\kl1\\Parameters"; if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, key, 0, L"", REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, 0, &hkey, 0)!= ERROR_SUCCESS) { return KL1_ERR; } } return KL1_SUCC; }
void raaUtilities::regWriteEntry(std::string sKey, std::string sEntry, std::string sStr) { HKEY hKey; DWORD dw; std::string s; s=sStr; for(std::string::iterator it=s.begin();it!=s.end();it++) if((*it)=='/') (*it)='\\'; s.append("\\"); RegCreateKeyExA(HKEY_LOCAL_MACHINE, sKey.c_str(), 0, NULL, 0, 0, NULL, &hKey, &dw); if(hKey) RegCloseKey(hKey); hKey=0; RegOpenKeyExA(HKEY_LOCAL_MACHINE, sKey.c_str(),0,KEY_ALL_ACCESS,&hKey); if(hKey) { RegSetValueExA(hKey, sEntry.c_str(),0,REG_SZ,(CONST BYTE*)(s.c_str()), sizeof(const char)*(s.length())); RegCloseKey(hKey); } }
/* good1() uses if(STATIC_CONST_FIVE!=5) instead of if(STATIC_CONST_FIVE==5) */ static void good1() { if(STATIC_CONST_FIVE!=5) { /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */ printLine("Benign, fixed string"); } else { { char * keyName = "TEST\\TestKey"; HKEY hKey; /* FIX: Call RegCreateKeyExA() without KEY_ALL_ACCESS as the 6th parameter to limit access */ if (RegCreateKeyExA( HKEY_CURRENT_USER, keyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS) { printLine("Registry key could not be created"); } else { printLine("Registry key created successfully"); RegCloseKey(hKey); } } } }
void CWE284_Improper_Access_Control__w32_char_RegCreateKeyEx_06_bad() { if(STATIC_CONST_FIVE==5) { { char * keyName = "TEST\\TestKey"; HKEY hKey; /* FLAW: Call RegCreateKeyExA() with KEY_ALL_ACCESS as the 6th parameter */ if (RegCreateKeyExA( HKEY_CURRENT_USER, keyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL) != ERROR_SUCCESS) { printLine("Registry key could not be created"); } else { printLine("Registry key created successfully"); RegCloseKey(hKey); } } } }
/* register_keys - helper function, which recursively creates the registry keys and values in * parameter 'keys' in the registry under hRootKey. */ static BOOL register_keys(HKEY hRootKey, const struct registry_key *keys, unsigned int numKeys) { HKEY hKey; unsigned int iKey, iValue; for (iKey = 0; iKey < numKeys; iKey++) { if (ERROR_SUCCESS == RegCreateKeyExA(hRootKey, keys[iKey].szName, 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL)) { for (iValue = 0; iValue < keys[iKey].cValues; iValue++) { const struct registry_value * value = &keys[iKey].pValues[iValue]; if (ERROR_SUCCESS != RegSetValueExA(hKey, value->szName, 0, value->dwType, REG_VALUE_ADDR(value), REG_VALUE_SIZE(value))) { RegCloseKey(hKey); return FALSE; } } if (!register_keys(hKey, keys[iKey].pSubKeys, keys[iKey].cSubKeys)) { RegCloseKey(hKey); return FALSE; } RegCloseKey(hKey); } } return TRUE; }
void WinRegistryKey::open() { if (!_hKey) { #if defined(POCO_WIN32_UTF8) std::wstring usubKey; Poco::UnicodeConverter::toUTF16(_subKey, usubKey); if (_readOnly) { if (RegOpenKeyExW(_hRootKey, usubKey.c_str(), 0, KEY_READ | _extraSam, &_hKey) != ERROR_SUCCESS) throw NotFoundException("Cannot open registry key: ", key()); } else { if (RegCreateKeyExW(_hRootKey, usubKey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE | _extraSam, NULL, &_hKey, NULL) != ERROR_SUCCESS) throw SystemException("Cannot open registry key: ", key()); } #else if (_readOnly) { if (RegOpenKeyExA(_hRootKey, _subKey.c_str(), 0, KEY_READ | _extraSam, &_hKey) != ERROR_SUCCESS) throw NotFoundException("Cannot open registry key: ", key()); } else { if (RegCreateKeyExA(_hRootKey, _subKey.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE | _extraSam, NULL, &_hKey, NULL) != ERROR_SUCCESS) throw SystemException("Cannot open registry key: ", key()); } #endif } }
// Note registry flush not needed (see SpoutPanel) because this is the receiver bool SpoutReceiverSDK2::WritePathToRegistry(const char *filepath, const char *subkey, const char *valuename) { HKEY hRegKey; LONG regres; char mySubKey[512]; // The required key strcpy_s(mySubKey, 512, subkey); // Does the key already exist ? regres = RegOpenKeyExA(HKEY_CURRENT_USER, mySubKey, NULL, KEY_ALL_ACCESS, &hRegKey); if(regres != ERROR_SUCCESS) { // Create a new key regres = RegCreateKeyExA(HKEY_CURRENT_USER, mySubKey, NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,NULL, &hRegKey, NULL); } if(regres == ERROR_SUCCESS && hRegKey != NULL) { // Write the path regres = RegSetValueExA(hRegKey, valuename, 0, REG_SZ, (BYTE*)filepath, ((DWORD)strlen(filepath) + 1)*sizeof(unsigned char)); RegCloseKey(hRegKey); } if(regres == ERROR_SUCCESS) return true; else return false; }
static HKEY GetImplicitLayersKey(bool writeable, bool wow6432) { std::string basepath = "SOFTWARE\\"; if(wow6432) basepath += "Wow6432Node\\"; basepath += "Khronos\\Vulkan\\ImplicitLayers"; HKEY key = NULL; LSTATUS ret = ERROR_SUCCESS; if(writeable) ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, basepath.c_str(), 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &key, NULL); else ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, basepath.c_str(), 0, KEY_READ, &key); if(ret != ERROR_SUCCESS) { if(key) RegCloseKey(key); // find to fail to open for read, the key may not exist if(writeable) RDCERR("Couldn't open %s for write", basepath.c_str()); return NULL; } return key; }
// return 0 success or winerror.h error code - probably no access int SetKeyAndValue(BOOL set, LPSTR keys, LPSTR szK, LPSTR szSubkey, LPSTR szV) { HKEY hKey; char szk[256]; long r; strcpy(szk, szK); if (szSubkey) { strcat(szk, "\\"); strcat(szk, szSubkey); } strcat(keys, "\t"); strcat(keys, szk); if(!set) return 0; r=RegCreateKeyExA(HKEY_CLASSES_ROOT,szk,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,NULL); if(ERROR_SUCCESS!=r) return r; if (szV) { r=RegSetValueExA(hKey, NULL, 0, REG_SZ, (BYTE *)szV, (UINT)((strlen(szV)+1))); if(ERROR_SUCCESS!=r) return r; } RegCloseKey(hKey); return 0; }
// returns TRUE if the mime type was not yet registered on the system, // it needs to be removed when the file extension gets removed BOOL AddRegMimeType(const char *pszMimeType,const char *pszFileExt) { BOOL fSuccess=FALSE; HKEY hRootKey,hDbKey,hTypeKey; /* some error checking for disallowed values (to avoid errors in registry) */ if (strchr(pszMimeType,'\\')!=NULL || strchr(pszMimeType,' ')!=NULL) return FALSE; /* try to open interactive user's classes key */ if (RegOpenKeyEx(HKEY_CURRENT_USER,_T("Software\\Classes"),0,KEY_QUERY_VALUE,&hRootKey)) hRootKey=HKEY_CLASSES_ROOT; /* database */ if (!RegOpenKeyEx(hRootKey,_T("MIME\\Database\\Content Type"),0,KEY_CREATE_SUB_KEY,&hDbKey)) { /* mime type */ if (!RegCreateKeyExA(hDbKey,pszMimeType,0,NULL,0,KEY_QUERY_VALUE|KEY_SET_VALUE,NULL,&hTypeKey,NULL)) { /* file ext */ if (RegQueryValueExA(hTypeKey,"Extension",NULL,NULL,NULL,NULL)) /* only set if not present */ fSuccess=!RegSetValueExA(hTypeKey,"Extension",0,REG_SZ,(BYTE*)pszFileExt,(int)mir_strlen(pszFileExt)+1); RegCloseKey(hTypeKey); } RegCloseKey(hDbKey); } if (hRootKey!=HKEY_CLASSES_ROOT) RegCloseKey(hRootKey); return fSuccess; }
bool spoutDirectX::WriteDwordToRegistry(DWORD dwValue, const char *subkey, const char *valuename) { HKEY hRegKey; LONG regres; char mySubKey[512]; // The required key strcpy_s(mySubKey, 512, subkey); // Does the key already exist ? regres = RegOpenKeyExA(HKEY_CURRENT_USER, mySubKey, NULL, KEY_ALL_ACCESS, &hRegKey); if(regres != ERROR_SUCCESS) { // Create a new key regres = RegCreateKeyExA(HKEY_CURRENT_USER, mySubKey, NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,NULL, &hRegKey, NULL); } if(regres == ERROR_SUCCESS && hRegKey != NULL) { // Write the DWORD value regres = RegSetValueExA(hRegKey, valuename, 0, REG_DWORD, (BYTE*)&dwValue, 4); // For immediate read after write - necessary here because the app might set the values // and read the registry straight away and it might not be available yet // The key must have been opened with the KEY_QUERY_VALUE access right (included in KEY_ALL_ACCESS) RegFlushKey(hRegKey); // needs an open key RegCloseKey(hRegKey); // Done with the key } if(regres == ERROR_SUCCESS) return true; else return false; }
/*********************************************************************** * regsvr_progid */ static LONG register_progid( WCHAR const *clsid, char const *progid, char const *name) { LONG res; HKEY progid_key; res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &progid_key, NULL); if (res != ERROR_SUCCESS) return res; if (name) { res = RegSetValueExA(progid_key, NULL, 0, REG_SZ, (CONST BYTE*)name, strlen(name) + 1); if (res != ERROR_SUCCESS) goto error_close_progid_key; } if (clsid) { res = register_key_defvalueW(progid_key, clsid_keyname, clsid); if (res != ERROR_SUCCESS) goto error_close_progid_key; } error_close_progid_key: RegCloseKey(progid_key); return res; }
LONG winfrip_confstore_RegCreateKeyEx(HKEY hKey, LPCSTR lpSubKey, DWORD Reserved, LPSTR lpClass, DWORD dwOptions, REGSAM samDesired, LPSECURITY_ATTRIBUTES lpSecurityAttributes, PHKEY phkResult, LPDWORD lpdwDisposition) { WINFRIPP_CONFSTORE_DEBUG_ASSERT(hKey); WINFRIPP_CONFSTORE_DEBUG_ASSERT(phkResult); WINFRIPP_CONFSTORE_DEBUG_ASSERT(lpSubKey); /* * XXX document */ switch (winfrip_confstore_backend_get()) { default: WINFRIPP_CONFSTORE_DEBUG_FAIL(); return ERROR_INVALID_FUNCTION; case WINFRIP_GENERAL_STORE_BACKEND_REGISTRY: return RegCreateKeyExA(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition); case WINFRIP_GENERAL_STORE_BACKEND_EPHEMERAL: case WINFRIP_GENERAL_STORE_BACKEND_FILE: if (winfripp_confstore_map_hkey_add(&winfripp_confstore_hkey_bitmap, &winfripp_confstore_hkey_bitmap_bits, winfripp_confstore_hkey_map, winfripp_confstore_hkey_map_len, hKey, lpSubKey, phkResult)) { return ERROR_SUCCESS; } else { return ERROR_NOT_ENOUGH_MEMORY; } } }
static void setRecycleBin(char letter, bool create) { #ifndef _USE_PIPE HRESULT hr; DWORD data; HKEY hKey; char root[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\BitBucket"; char path[] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\BitBucket\\a"; path[sizeof(path)-2] = letter; if (create) { hr = RegOpenKeyExA(HKEY_LOCAL_MACHINE, root, 0, KEY_ALL_ACCESS, &hKey); debug(D_NOTICE, "RegOpenKeyExA - %ld, %s", hr, path); if (hr == ERROR_FILE_NOT_FOUND) { hr = RegCreateKeyExA(HKEY_LOCAL_MACHINE, root, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL); debug(D_NOTICE, "RegCreateKeyExA - %ld", hr); } if (!hr) { data = 0; hr = RegSetValueExA(hKey, "UseGlobalSettings", 0, REG_DWORD, (LPBYTE)&data, 4); debug(D_NOTICE, "RegSetValueExA - %ld", hr); RegCloseKey(hKey); if (!hr) { hr = RegCreateKeyExA(HKEY_LOCAL_MACHINE, path, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL); debug(D_NOTICE, "RegCreateKeyExA - %ld", hr); if(!hr) { data = 1; hr = RegSetValueExA(hKey, "NukeOnDelete", 0, REG_DWORD, (LPBYTE)&data, 4); debug(D_NOTICE, "RegSetValueExA - %ld", hr); RegCloseKey(hKey); } } } } else { hr = RegDeleteKeyA(HKEY_LOCAL_MACHINE, path); debug(D_NOTICE, "RegDeleteKeyA - %ld", hr); } #endif //_USE_PIPE }