📄 registerwin32.cpp
字号:
//--FILE DEFINITION-----------------------------------------------------------
//
/*! \file
File name: RegisterWIN32.cpp(CRegisterWIN32)
Author: Yves Lessard
Date: 2001/12/19(YYYY/MM/DD)
Library: Win32
Version: 1.01
Usage: implementation of the CRegistreWIN32 class
Notes: See Header infos.
*/
//----------------------------------------------------------------------------
//--FILE INCLUDES-------------------------------------------------------------
#include "RegisterWIN32.h"
//!--CONSTRUCTOR--------------------------------------------------------------
//
// Method Name: CRegisterWIN32()
//
/*!
Notes Constructor.
*/
//----------------------------------------------------------------------------
CRegisterWIN32::CRegisterWIN32()
{
//** Default is HKEY_LOCAL_MACHINE
m_RootKey = HKEY_LOCAL_MACHINE;
m_hKey = NULL;
m_pMyCaes = NULL;
m_pMyCaes = new Rijndael;
m_pszTemp = new char[MAX_SIZE];
}
//!--DESTRUCTOR----------------------------------------------------------------
//
// Method Name: ~CRegisterWIN32()
//
/*!
Notes Destructor. Clean-up
*/
//-----------------------------------------------------------------------------
CRegisterWIN32::~CRegisterWIN32()
{
//** Clean up memory and Close Session
if ( m_hKey )
RegCloseKey(m_hKey);
if(m_pMyCaes)
delete m_pMyCaes;
if(m_pszTemp)
delete []m_pszTemp;
}
///////////////////////////////////////////////////////////////////////////
// Private Functions
///////////////////////////////////////////////////////////////////////////
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: VerifyKey
//
/*!Access: Private
Parameters
IN Description
<None>
OUT Description
<BOOL> TRUE if the Key founded.
Notes:
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::VerifyKey()
{
return ( RegOpenKeyEx(m_RootKey, m_szPath.c_str(), 0L, KEY_ALL_ACCESS, &m_hKey) == ERROR_SUCCESS );
}
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: CreateKey
//
/*!Access: Private
Parameters
IN Description
<None>
OUT Description
<BOOL> TRUE if succeeded else FALSE
Notes: We create the Key path.
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::CreateKey()
{
Close();
return (RegCreateKeyEx(m_RootKey, m_szPath.c_str(), 0, 0,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
0, &m_hKey, 0) == ERROR_SUCCESS );
}
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: GetOSversion
//
/*!Access: Private
Parameters
IN Description
<None>
OUT Description
<int> 1 NT4 ou Win2000
0 = Win9x
Notes:
*/
//--------------------------------------------------------------------------
int CRegisterWIN32::GetOSversion()
{
int iResult = 0;
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (GetVersionEx(&osv))
{
// note: szCSDVersion = service pack release
switch(osv.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
iResult=1;
break;
default:
iResult=0;
break;
}
}
return iResult;
}
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: SetValue
//
/*!Access: Private
Parameters
IN Description
<LPCTSTR> The Key Name
<DWORD> Value as DWORD
OUT Description
<BOOL> TRUE if Succeeded.
Notes: We can write integer value
but we must cast it.
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::SetValue(LPCTSTR _szKey, DWORD _dVal)
{
//** Write the Value
return ( RegSetValueEx(m_hKey, _szKey,0,
REG_DWORD, (CONST BYTE*)&_dVal,
sizeof(DWORD)) == ERROR_SUCCESS );
}
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: StringIN
//
/*!Access: Private
Parameters
IN Description
<LPCTSTR> The Key Name
OUT Description
<BOOL> TRUE if succeeded.
Notes: String reading.
The result is in m_pszTemp.
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::StringIN(LPCTSTR _szKey)
{
BOOL bResult = FALSE;
DWORD dwType;
DWORD dwSize = MAX_SIZE;
if ( strlen(_szKey) > 0 )
{
bResult = ( RegQueryValueEx(m_hKey, _szKey, 0, &dwType,
(BYTE*)m_pszTemp,
&dwSize) == ERROR_SUCCESS);
}
return bResult;
}
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: GetValue
//
/*!Access: Private
Parameters
IN Description
<LPCTSTR> The Key Name
OUT Description
<BOOL> TRUE if Succeeded.
Notes: The value is store in m_dTemp
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::GetValue(LPCTSTR _szKey)
{
BOOL bResult = FALSE;
DWORD dwType;
DWORD dwSize = sizeof(DWORD);
bResult = ( RegQueryValueEx(m_hKey, _szKey, 0, &dwType,
(BYTE*)&m_dTemp, &dwSize) == ERROR_SUCCESS );
return bResult;
}
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: DeleteNTway
//
/*!Access: Private
Parameters
IN Description
<HKEY> The RootKey
<LPCTSTR> The Subkey to Delete
OUT Description
<BOOL> TRUE if SUcceeded.
Notes: Delete each Subkey
This is a recursive Function .
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::DeleteNTway(HKEY _hKey, LPCTSTR _szSubKey)
{
BOOL bResult = FALSE;
LPTSTR pszTemp = new char[_MAX_PATH];
HKEY lhKey;
FILETIME ft;
LONG lResult;
DWORD dwKeyLen;
//** If Path empty we get out
if( strlen(_szSubKey) > 0)
{
if(RegOpenKeyEx(_hKey, _szSubKey, 0L, KEY_ENUMERATE_SUB_KEYS,
&lhKey) == ERROR_SUCCESS )
{
//** So far the Key exist
do
{
dwKeyLen = _MAX_PATH;
lResult = RegEnumKeyEx(lhKey, 0, pszTemp, &dwKeyLen, NULL, NULL,
NULL, &ft);
switch (lResult)
{
case ERROR_NO_MORE_ITEMS:
//** No more Subkey so delete the base
if ( RegDeleteKey(_hKey ,_szSubKey) == ERROR_SUCCESS )
{
bResult= TRUE;
break;
}
break;
case ERROR_SUCCESS:
if( DeleteNTway(lhKey, pszTemp) )
bResult = TRUE;
break;
}
}while( lResult == ERROR_SUCCESS);
RegCloseKey(lhKey);
}
}
if(pszTemp)
delete []pszTemp;
return bResult;
}
///////////////////////////////////////////////////////////////////////////
// Public Functions
///////////////////////////////////////////////////////////////////////////
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: Open
//
/*!Access: Public
Parameters
IN Description
<HKEY> Root Key
<LPCTSTR> Sub Key Name
<BOOL> TRUE ->Read Mode Only else Read/Write
OUT Description
<BOOL> TRUE if succeeded.
Notes: Open a registry Session (1st Method)
Example:
m_MyRegTool.Open(HKEY_CURRENT_USER, "Software\\SevySoft\\Admin");
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::Open(HKEY _hKeyRoot, LPCTSTR _szPath,
BOOL _bReadOnly)
{
BOOL bResult = FALSE;
m_szPath.erase();
//** If a Session Key Opened then close it
Close();
//** 5 Roots Key choice
if ( _hKeyRoot == HKEY_CLASSES_ROOT || _hKeyRoot == HKEY_CURRENT_USER ||
_hKeyRoot == HKEY_LOCAL_MACHINE || _hKeyRoot == HKEY_USERS
|| _hKeyRoot == HKEY_CURRENT_CONFIG )
{
//** Save RootKey for reference
m_RootKey = _hKeyRoot;
if ( strlen(_szPath) > 0 )
{
//** We have a path so save it
m_szPath = _szPath;
switch (_bReadOnly)
{
case TRUE:
//** Read Mode Only
bResult = VerifyKey();
break;
default:
//** Else Read/Write
if ( !VerifyKey() )
//** Key not Found so create it
bResult = CreateKey();
break;
}
}
}
return bResult;
}
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: Open
//
/*!Access: Public
Parameters
IN Description
<LPCTSTR> Sub Key Name
<BOOL> TRUE -> Read Mode (Default)
OUT Description
<BOOL> TRUE if Succeeded.
Notes: Open a registry Session (2nd Method)
The Key is HKEY_LOCAL_MACHINE.
*/
//--------------------------------------------------------------------------
BOOL CRegisterWIN32::Open(LPCTSTR _szPath, BOOL _bReadOnly)
{
BOOL bResult = FALSE;
m_szPath.erase();
//** If Session Key Opened then close it
Close();
//** Always use this one
m_RootKey = HKEY_LOCAL_MACHINE;
if ( strlen(_szPath) > 0 )
{
m_szPath = _szPath;
switch (_bReadOnly)
{
case TRUE:
//** Read only mode
bResult = VerifyKey();
break;
default:
//** Read Write Mode
if ( !VerifyKey() )
//** If Path not Found Create it
bResult = CreateKey();
break;
}
}
return bResult;
}
//-METHOD IMPLEMENTATION----------------------------------------------------
//
// Method Name: Close
//
/*!Access: Public
Parameters
IN Description
<None>
OUT Description
<None>
Notes: Close a Registry Session.
*/
//--------------------------------------------------------------------------
void CRegisterWIN32::Close()
{
if ( m_hKey )
RegCloseKey(m_hKey);
m_hKey = NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -