レジストリ操作一般は、以下のようにします。
// hKeyParent=キーのハンドル、sKeyName=サブキーのハンドル、sValueName=値の名前、sValue=設定値
// レジストリ値を削除
bool deleteRegString(HKEY hKeyParent, const char* sKeyName, const char* sValueName)
{
HKEY hkResult;
bool ret(false);
if(RegOpenKeyEx(hKeyParent, sKeyName, 0, KEY_READ, &hkResult) == ERROR_SUCCESS){
if(RegDeleteKey(hkResult, sValueName) == ERROR_SUCCESS){
ret = true;
}
RegCloseKey(hkResult);
}
return ret;
}
// レジストリに文字値を設定
bool setRegValue(HKEY hKeyParent, const char* sKeyName, const char* sValueName, const char* sValue)
{
DWORD dwOptions = REG_OPTION_NON_VOLATILE;
REGSAM samDesired = KEY_ALL_ACCESS;
LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
HKEY hkResult;
DWORD dwDisposition;
if(RegCreateKeyEx(hKeyParent,
sKeyName,
0,
(LPTSTR)sKeyName,
dwOptions,
samDesired,
lpSecurityAttributes,
&hkResult,
&dwDisposition) != ERROR_SUCCESS){
return false;
}
if(RegSetValueEx(hkResult,
sValueName,
0,
REG_SZ,
(CONST BYTE *)(sValue),
(DWORD)strlen(sValue)) != ERROR_SUCCESS){
return false;
}
RegCloseKey(hkResult);
return true;
}
// レジストリに整数(long)値を設定
bool setRegValue(HKEY hKeyParent, const char* sKeyName, const char* sValueName, long nValue)
{
DWORD dwOptions = REG_OPTION_NON_VOLATILE;
REGSAM samDesired = KEY_ALL_ACCESS;
LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
HKEY hkResult;
DWORD dwDisposition;
if(RegCreateKeyEx(hKeyParent,
sKeyName,
0,
(LPTSTR)sKeyName,
dwOptions,
samDesired,
lpSecurityAttributes,
&hkResult,
&dwDisposition) != ERROR_SUCCESS){
return false;
}
DWORD val = nValue;
if(RegSetValueEx(hkResult,
sValueName,
0,
REG_DWORD,
(CONST BYTE *)(&val),
sizeof(DWORD)) != ERROR_SUCCESS){
return false;
}
RegCloseKey(hkResult);
return true;
}
// レジストリから文字列値を取得
const bool getRegValue(HKEY hKeyParent, const char* sKeyName, const char* sValueName, CString& strValue)
{
strValue = "";
HKEY result;
if (RegOpenKeyEx(hKeyParent, sKeyName, 0, KEY_READ, &result) != ERROR_SUCCESS ){
return false;
}
DWORD pdwCount;
DWORD dwType;
bool ret(false);
if( RegQueryValueEx(result, sValueName, NULL, &dwType, NULL, &pdwCount) == ERROR_SUCCESS ) {
if(pdwCount >= 1){
char *buf = new char[pdwCount];
if( RegQueryValueEx(result, sValueName, NULL, &dwType, (LPBYTE)buf, &pdwCount) == ERROR_SUCCESS ) {
strValue = buf;
ret = true;
}
delete [] buf;
}
}
RegCloseKey(result);
return ret;
}
// レジストリから整数(long)値を取得
bool getRegValue(HKEY hKeyParent, const char* sKeyName, const char* sValueName, long& nValue)
{
HKEY result;
if (RegOpenKeyEx(hKeyParent, sKeyName, 0, KEY_READ, &result) != ERROR_SUCCESS ){
return false;
}
bool ret(false);
DWORD dwSize;
DWORD dwType;
DWORD data;
if(RegQueryValueEx(result,sValueName, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS
&& RegQueryValueEx(result,sValueName, NULL, &dwType, (LPBYTE)&data, &dwSize) == ERROR_SUCCESS
){
nValue = data;
ret = true;
}
RegCloseKey(result);
return ret;
}
|