bool RegistryUtil::readInt32(const std::string& key, const std::string& value, int32& data) { size_t pos = key.find('\\', 0); if (pos == std::string::npos) { return false; } HKEY hkey = getRootKeyFromString(key.c_str(), pos); if ( hkey == NULL ) { return false; } HKEY openKey; int32 result = RegOpenKeyExA(hkey, (key.c_str() + pos + 1), 0, KEY_READ, &openKey); ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); if (result == ERROR_SUCCESS) { uint32 dataSize = sizeof(int32); result = RegQueryValueExA(openKey, value.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(&data), reinterpret_cast<LPDWORD>(&dataSize)); ASSERT(result == ERROR_SUCCESS); RegCloseKey(openKey); } return (result == ERROR_SUCCESS); }
bool RegistryUtil::writeBytes(const std::string& key, const std::string& value, const uint8* data, uint32 dataSize) { ASSERT(data); size_t pos = key.find('\\', 0); if (pos == std::string::npos) { return false; } HKEY hkey = getRootKeyFromString(key.c_str(), pos); if (hkey == NULL) { return false; } HKEY openKey; int32 result = RegOpenKeyExA(hkey, (key.c_str() + pos + 1), 0, KEY_WRITE, &openKey); ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); if (result == ERROR_SUCCESS) { if (data) { result = RegSetValueExA(openKey, value.c_str(), 0, REG_BINARY, reinterpret_cast<const BYTE*>(data), dataSize); } ASSERT(result == ERROR_SUCCESS); RegCloseKey(openKey); } return (result == ERROR_SUCCESS); }
bool RegistryUtil::writeInt32(const std::string& key, const std::string& value, int32 data) { size_t pos = key.find('\\', 0); if (pos == std::string::npos) { return false; } HKEY hkey = getRootKeyFromString(key.c_str(), pos); if (hkey == NULL) { return false; } HKEY openKey; int32 result = RegOpenKeyExA(hkey, (key.c_str() + pos + 1), 0, KEY_WRITE, &openKey); debugAssert(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); if (result == ERROR_SUCCESS) { result = RegSetValueExA(openKey, value.c_str(), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&data), sizeof(int32)); debugAssertM(result == ERROR_SUCCESS, "Could not write registry key value."); RegCloseKey(openKey); } return (result == ERROR_SUCCESS); }
bool RegistryUtil::readBytes(const std::string& key, const std::string& value, uint8* data, uint32& dataSize) { size_t pos = key.find('\\', 0); if (pos == std::string::npos) { return false; } HKEY hkey = getRootKeyFromString(key.c_str(), pos); if (hkey == NULL) { return false; } HKEY openKey; int32 result = RegOpenKeyExA(hkey, (key.c_str() + pos + 1), 0, KEY_READ, &openKey); debugAssert(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); if (result == ERROR_SUCCESS) { if (data == NULL) { result = RegQueryValueExA(openKey, value.c_str(), NULL, NULL, NULL, reinterpret_cast<LPDWORD>(&dataSize)); } else { result = RegQueryValueExA(openKey, value.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(&data), reinterpret_cast<LPDWORD>(&dataSize)); } debugAssertM(result == ERROR_SUCCESS, "Could not read registry key value."); RegCloseKey(openKey); } return (result == ERROR_SUCCESS); }
bool RegistryUtil::valueExists(const String& key, const String& value) { size_t pos = key.find('\\', 0); if (pos == String::npos) { return false; } HKEY hkey = getRootKeyFromString(key.c_str(), pos); if ( hkey == NULL ) { return false; } HKEY openKey; int32 result = RegOpenKeyExA(hkey, (key.c_str() + pos + 1), 0, KEY_READ, &openKey); debugAssert(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); if (result == ERROR_SUCCESS) { uint32 dataSize = 0; result = RegQueryValueExA(openKey, value.c_str(), NULL, NULL, NULL, reinterpret_cast<LPDWORD>(&dataSize)); debugAssert(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); RegCloseKey(openKey); } return (result == ERROR_SUCCESS); }
bool RegistryUtil::writeString(const String& key, const String& value, const String& data) { size_t pos = key.find('\\', 0); if (pos == String::npos) { return false; } HKEY hkey = getRootKeyFromString(key.c_str(), pos); if (hkey == NULL) { return false; } HKEY openKey; int32 result = RegOpenKeyExA(hkey, (key.c_str() + pos + 1), 0, KEY_WRITE, &openKey); debugAssert(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); if (result == ERROR_SUCCESS) { alwaysAssertM(data.size() < 0xFFFFFFFE, "String too long"); result = RegSetValueExA(openKey, value.c_str(), 0, REG_SZ, reinterpret_cast<const BYTE*>(data.c_str()), (int)(data.size() + 1)); debugAssertM(result == ERROR_SUCCESS, "Could not write registry key value."); RegCloseKey(openKey); } return (result == ERROR_SUCCESS); }
bool RegistryUtil::readString(const std::string& key, const std::string& value, std::string& data) { size_t pos = key.find('\\', 0); if (pos == std::string::npos) { return false; } HKEY hkey = getRootKeyFromString(key.c_str(), pos); if (hkey == NULL) { return false; } HKEY openKey; int32 result = RegOpenKeyExA(hkey, (key.c_str() + pos + 1), 0, KEY_READ, &openKey); ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); if (result == ERROR_SUCCESS) { uint32 dataSize = 0; result = RegQueryValueExA(openKey, value.c_str(), NULL, NULL, NULL, reinterpret_cast<LPDWORD>(&dataSize)); ASSERT(result == ERROR_SUCCESS); // increment datasize to allow for non null-terminated strings in registry dataSize += 1; if (result == ERROR_SUCCESS) { char* tmpStr = static_cast<char*>(System::malloc(dataSize)); System::memset(tmpStr, 0, dataSize); result = RegQueryValueExA(openKey, value.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(tmpStr), reinterpret_cast<LPDWORD>(&dataSize)); ASSERT(result == ERROR_SUCCESS); if (result == ERROR_SUCCESS) { data = tmpStr; } RegCloseKey(openKey); System::free(tmpStr); } } return (result == ERROR_SUCCESS); }
bool RegistryUtil::keyExists(const std::string& key) { size_t pos = key.find('\\', 0); if (pos == std::string::npos) { return false; } HKEY hkey = getRootKeyFromString(key.c_str(), pos); if (hkey == NULL) { return false; } HKEY openKey; int32 result = RegOpenKeyExA(hkey, (key.c_str() + pos + 1), 0, KEY_READ, &openKey); ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND); if (result == ERROR_SUCCESS) { RegCloseKey(openKey); return true; } else { return false; } }