//***********************************************************
// Registry Access Module //
// //
// Author : C. Chris //
// http://www.PassportONE.com //
// Email: chris@PassportONE.com //
// � Oct/Apr 2001 //
// //
//***********************************************************
#include "RegEdit.h"
//-----------------------------------------------------------
//
// STRING
//
//-----------------------------------------------------------
BOOL SaveString (HKEY lpszKey, LPCTSTR lpszSubkey, LPCTSTR lpszValueName, LPCTSTR lpData)
{
// PURPOSE:
// - Save the data into registry.
// PARAMETERS:
// - NIL
// OPERATION:
// - Open the registry at the given location.
// - Save the given data into the given key and value name.
// RETURN VALUE:
// - 0 :: Fail
// 1 :: Pass
//Registry Key Handle
HKEY CurKey;
DWORD dwDisposition=0;
BOOL fRes=false;
// Sanity check
if (lpszSubkey != NULL && lpszValueName != NULL)
{
// Sanity check
if (_tcslen(lpszSubkey) > 0 && _tcslen(lpszValueName) > 0)
{
// OPen registry
if (RegOpenKeyEx(lpszKey, lpszSubkey, 0, 0, &CurKey) == ERROR_SUCCESS)
{
// Update value
if (RegSetValueEx(CurKey, lpszValueName, 0, REG_SZ, (PBYTE)lpData, _tcslen(lpData) * sizeof(LPCTSTR)) == ERROR_SUCCESS)
fRes = true;
}
else
{
// Create new key
if (RegCreateKeyEx(lpszKey, lpszSubkey, 0, 0, 0, 0, NULL, &CurKey, &dwDisposition) == ERROR_SUCCESS)
{
// UPdate value
if (RegSetValueEx(CurKey, lpszValueName, 0, REG_SZ, (PBYTE)lpData, _tcslen(lpData) * sizeof(LPCTSTR)) == ERROR_SUCCESS)
fRes = true;
}
}
}
}
// Close the register
RegCloseKey (CurKey);
// Set the return value
return fRes;
}
LPTSTR ReadString (HKEY lpszKey, LPCTSTR lpszSubkey, LPCTSTR lpszValueName, LPCTSTR pcDefault)
{
// PURPOSE:
// - Read the data from registry.
// PARAMETERS:
// - NIL
// OPERATION:
// - Open the registry at the given location.
// - Write the given data into the given key and value name.
// RETURN VALUE:
// - Value
// Registry Key Handle
HKEY CurKey;
DWORD dwType = REG_SZ;
DWORD dwDataSize=0;;
TCHAR *lpszValue=NULL;
// Sanity check
if (lpszSubkey != NULL && lpszValueName != NULL)
{
// Sanity check
if (_tcslen(lpszSubkey) > 0 && _tcslen(lpszValueName) > 0)
{
// Sanity check
if (RegOpenKeyEx(lpszKey, lpszSubkey, 0, 0, &CurKey) == ERROR_SUCCESS)
{
// Get the record size first
if (RegQueryValueEx(CurKey, lpszValueName, NULL, &dwType, (PBYTE)NULL, &dwDataSize) == ERROR_SUCCESS)
{
// Allocate memory block & Initialize to NULL
lpszValue = new TCHAR[dwDataSize + 1];
memset(lpszValue, TEXT('\0'), dwDataSize + 1);
// Read the data value...
if (RegQueryValueEx(CurKey, lpszValueName, NULL, &dwType, (PBYTE)(LPTSTR)lpszValue, &dwDataSize) == ERROR_SUCCESS)
{
//Return the default value
RegCloseKey (CurKey);
return lpszValue;
}
else
{
//Return the default value
RegCloseKey (CurKey);
return (LPTSTR)pcDefault;
}
}
else
{
//Return the default value
RegCloseKey (CurKey);
return (LPTSTR)pcDefault;
}
}
else
{
//Return the default value
RegCloseKey (CurKey);
return (LPTSTR)pcDefault;
}
}
else
//Return the default value
return (LPTSTR)pcDefault;
}
//Return the default value
return (LPTSTR)pcDefault;
}
//-----------------------------------------------------------
//
// DWORD
//
//-----------------------------------------------------------
BOOL SaveDWORD (HKEY lpszKey, LPCTSTR lpszSubkey, LPCTSTR lpszValueName, DWORD dwValue)
{
// PURPOSE:
// - Save the data into registry.
// PARAMETERS:
// - NIL
// OPERATION:
// - Open the registry at the given location.
// - Save the given data into the given key and value name.
// RETURN VALUE:
// - 0 :: Fail
// 1 :: Pass
//Registry Key Handle
HKEY CurKey;
DWORD dwDisposition=0;
BOOL fRes=false;
// Sanity check
if (lpszSubkey != NULL && lpszValueName != NULL)
{
// Sanity check
if (_tcslen(lpszSubkey) > 0 && _tcslen(lpszValueName) > 0)
{
// OPen registry
if (RegOpenKeyEx(lpszKey, lpszSubkey, 0, 0, &CurKey) == ERROR_SUCCESS)
{
// Update value
if (RegSetValueEx(CurKey, lpszValueName, 0, REG_DWORD, (PBYTE)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS)
fRes = true;
}
else
{
// Create new key
if (RegCreateKeyEx(lpszKey, lpszSubkey, 0, 0, 0, 0, NULL, &CurKey, &dwDisposition) == ERROR_SUCCESS)
{
// Update value
if (RegSetValueEx(CurKey, lpszValueName, 0, REG_DWORD, (PBYTE)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS)
fRes = true;
}
}
}
}
// Close the register
RegCloseKey (CurKey);
// Set the return value
return fRes;
}
DWORD ReadDWORD (HKEY lpszKey, LPCTSTR lpszSubkey, LPCTSTR lpszValueName, DWORD dwDefault)
{
// PURPOSE:
// - Read the data from registry.
// PARAMETERS:
// - NIL
// OPERATION:
// - Open the registry at the given location.
// - Write the given data into the given key and value name.
// RETURN VALUE:
// - Value
// Registry Key Handle
HKEY CurKey;
DWORD dwType = REG_DWORD;
DWORD dwDataSize = sizeof(DWORD);
DWORD dwValue = 0;
// Sanity check
if (lpszSubkey != NULL && lpszValueName != NULL)
{
// Sanity check
if (_tcslen(lpszSubkey) > 0 && _tcslen(lpszValueName) > 0)
{
// Sanity check
if (RegOpenKeyEx(lpszKey, lpszSubkey, 0, 0, &CurKey) == ERROR_SUCCESS)
{
// Read the data value...
if (RegQueryValueEx(CurKey, lpszValueName, NULL, &dwType, (PBYTE)&dwValue, &dwDataSize) == ERROR_SUCCESS)
{
//Return the default value
RegCloseKey (CurKey);
return dwValue;
}
else
{
//Return the default value
RegCloseKey (CurKey);
return dwDefault;
}
}
else
{
//Return the default value
RegCloseKey (CurKey);
return dwDefault;
}
}
else
//Return the default value
return dwDefault;
}
//Return the default value
return dwDefault;
}
//-----------------------------------------------------------
//
// BINARY
//
//-----------------------------------------------------------
BOOL SaveBinary (HKEY lpszKey, LPCTSTR lpszSubkey, LPCTSTR lpszValueName, DWORD dwValue)
{
// PURPOSE:
// - Save the data into registry.
// PARAMETERS:
// - NIL
// OPERATION:
// - Open the registry at the given location.
// - Save the given data into the given key and value name.
// RETURN VALUE:
// - 0 :: Fail
// 1 :: Pass
return false;
}
DWORD ReadBinary (HKEY lpszKey, LPCTSTR lpszSubkey, LPCTSTR lpszValueName, DWORD dwDefault)
{
// PURPOSE:
// - Read the data from registry.
// PARAMETERS:
// - NIL
// OPERATION:
// - Open the registry at the given location.
// - Write the given data into the given key and value name.
// RETURN VALUE:
// - Value
return 0;
}