145 lines
3.6 KiB
C++
145 lines
3.6 KiB
C++
//+-------------------------------------------------------------------------
|
|
//
|
|
// Microsoft Windows
|
|
//
|
|
// Copyright (C) Microsoft Corporation, 1997 - 1997
|
|
//
|
|
// File: regkey.cpp
|
|
//
|
|
//--------------------------------------------------------------------------
|
|
|
|
#include <windows.h>
|
|
#include "regkey.h"
|
|
|
|
LONG REGKEY::Close()
|
|
{
|
|
LONG lRes = ERROR_SUCCESS;
|
|
if (m_hKey != NULL)
|
|
{
|
|
lRes = RegCloseKey(m_hKey);
|
|
m_hKey = NULL;
|
|
}
|
|
return lRes;
|
|
}
|
|
|
|
void REGKEY::Attach(HKEY hKey)
|
|
{
|
|
assert(m_hKey == NULL);
|
|
m_hKey = hKey;
|
|
}
|
|
|
|
|
|
LONG REGKEY::Create(HKEY hKeyParent, LPCTSTR lpszKeyName,
|
|
LPTSTR lpszClass, DWORD dwOptions, REGSAM samDesired,
|
|
LPSECURITY_ATTRIBUTES lpSecAttr, LPDWORD lpdwDisposition)
|
|
{
|
|
assert(hKeyParent != NULL);
|
|
DWORD dw;
|
|
HKEY hKey = NULL;
|
|
LONG lRes = RegCreateKeyEx(hKeyParent, lpszKeyName, 0,
|
|
lpszClass, dwOptions, samDesired, lpSecAttr, &hKey, &dw);
|
|
if (lpdwDisposition != NULL)
|
|
*lpdwDisposition = dw;
|
|
if (lRes == ERROR_SUCCESS)
|
|
{
|
|
lRes = Close();
|
|
m_hKey = hKey;
|
|
}
|
|
return lRes;
|
|
}
|
|
|
|
LONG REGKEY::Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired)
|
|
{
|
|
assert(hKeyParent != NULL);
|
|
HKEY hKey = NULL;
|
|
LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyName, 0, samDesired, &hKey);
|
|
if (lRes == ERROR_SUCCESS)
|
|
{
|
|
lRes = Close();
|
|
assert(lRes == ERROR_SUCCESS);
|
|
m_hKey = hKey;
|
|
}
|
|
return lRes;
|
|
}
|
|
|
|
LONG REGKEY::QueryValue(DWORD& dwValue, LPCTSTR lpszValueName)
|
|
{
|
|
DWORD dwType = NULL;
|
|
DWORD dwCount = sizeof(DWORD);
|
|
LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
|
|
(LPBYTE)&dwValue, &dwCount);
|
|
assert((lRes!=ERROR_SUCCESS) || (dwType == REG_DWORD));
|
|
assert((lRes!=ERROR_SUCCESS) || (dwCount == sizeof(DWORD)));
|
|
return lRes;
|
|
}
|
|
|
|
LONG REGKEY::QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount)
|
|
{
|
|
assert(pdwCount != NULL);
|
|
DWORD dwType = NULL;
|
|
LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
|
|
(LPBYTE)szValue, pdwCount);
|
|
assert((lRes!=ERROR_SUCCESS) || (dwType == REG_SZ) ||
|
|
(dwType == REG_MULTI_SZ) || (dwType == REG_EXPAND_SZ));
|
|
return lRes;
|
|
}
|
|
|
|
LONG WINAPI REGKEY::SetValue(HKEY hKeyParent, LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName)
|
|
{
|
|
assert(lpszValue != NULL);
|
|
REGKEY key;
|
|
LONG lRes = key.Create(hKeyParent, lpszKeyName);
|
|
if (lRes == ERROR_SUCCESS)
|
|
lRes = key.SetValue(lpszValue, lpszValueName);
|
|
return lRes;
|
|
}
|
|
|
|
LONG REGKEY::SetKeyValue(LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName)
|
|
{
|
|
assert(lpszValue != NULL);
|
|
REGKEY key;
|
|
LONG lRes = key.Create(m_hKey, lpszKeyName);
|
|
if (lRes == ERROR_SUCCESS)
|
|
lRes = key.SetValue(lpszValue, lpszValueName);
|
|
return lRes;
|
|
}
|
|
|
|
LONG REGKEY::SetValue(DWORD dwValue, LPCTSTR lpszValueName)
|
|
{
|
|
assert(m_hKey != NULL);
|
|
return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_DWORD,
|
|
(BYTE * const)&dwValue, sizeof(DWORD));
|
|
}
|
|
|
|
HRESULT REGKEY::SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName)
|
|
{
|
|
assert(lpszValue != NULL);
|
|
assert(m_hKey != NULL);
|
|
return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_SZ,
|
|
(BYTE * const)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
|
|
}
|
|
|
|
//RecurseDeleteKey is necessary because on NT RegDeleteKey doesn't work if the
|
|
//specified key has subkeys
|
|
LONG REGKEY::RecurseDeleteKey(LPCTSTR lpszKey)
|
|
{
|
|
REGKEY key;
|
|
LONG lRes = key.Open(m_hKey, lpszKey);
|
|
if (lRes != ERROR_SUCCESS)
|
|
return lRes;
|
|
FILETIME time;
|
|
TCHAR szBuffer[256];
|
|
DWORD dwSize = 256;
|
|
while (RegEnumKeyEx(key.m_hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL,
|
|
&time)==ERROR_SUCCESS)
|
|
{
|
|
lRes = key.RecurseDeleteKey(szBuffer);
|
|
if (lRes != ERROR_SUCCESS)
|
|
return lRes;
|
|
dwSize = 256;
|
|
}
|
|
key.Close();
|
|
return DeleteSubKey(lpszKey);
|
|
}
|
|
|