registertable.cpp
来自「◆◆◆ 《投掷飞镖记分工具》◆◆◆ 操作系统 : Windows Mobil」· C++ 代码 · 共 686 行 · 第 1/2 页
CPP
686 行
#include "StdAfx.h"
#include "PublicFunc.h"
DWORD g_dwLastSendMsgTime = 0;
/********************************************************************************
* Function Type : Global
* Parameter : mainKey - [in] 主键,如:HKEY_CURRENT_USER
* lpszSubKey - [in] 子键,如:"Software\\MySoft"
* KeyName - [in] 键名,如“SoftName”
* resBuf - [out]读到数据结果存放的缓冲地址
* bufSize - [in/out] 缓冲长度
* Return Value : -1 - Fail to read register key
* >=0 - type of result data
* Description : 从注册表中读取数据
*********************************************************************************/
long ReadRegister(HKEY mainKey, LPCTSTR lpszSubKey, LPCTSTR KeyName, LPVOID resBuf, DWORD *bufSize)
{
DWORD dwRet = 0L;
//打开注册表子键
HKEY hKey;
if(RegOpenKeyEx(
mainKey,
lpszSubKey,
0,
KEY_ALL_ACCESS,
&hKey) != ERROR_SUCCESS)
{
return -1;
}
//从注册表中读数据
DWORD dwType;
dwRet = RegQueryValueEx(
hKey,
KeyName,
NULL,
&dwType,
(LPBYTE)resBuf,
bufSize);
//关闭键
RegCloseKey(hKey);
if(dwRet != ERROR_SUCCESS)
{
return -1L;
}
return (long)dwType;
}
//
// 打开一个子键,如果不存在就创建它,不管多少层都可以一层一层地创建出来
// 注意:第一层子键必须是系统存在的键,例如:可以输入“SOFTWARE\123\456\789”,
// 但不可以输入“abc\123\456\789”,因为第一层的“abc”是不存在的,但 HKEY_CLASSES_ROOT 下的子键没有这个限制
// 如果指定了 pHkey 参数,打开的句柄会传回,要记得用 RegCloseKey(*pHkey); 关闭
//
BOOL CreateRegisterSubKey ( HKEY mainKey, CString csSubKey, HKEY *pHkey/*=NULL*/ )
{
if ( csSubKey.IsEmpty() ) return FALSE;
if ( csSubKey.GetAt(csSubKey.GetLength()-1) != _T('\\') )
csSubKey += _T('\\');
HKEY hKey = NULL;
CString csCreateSubKey;
DWORD dwAction;
for ( int i=0; i<csSubKey.GetLength(); i++ )
{
hKey = NULL;
if ( csSubKey.GetAt(i)==_T('\\') || i==csSubKey.GetLength()-1 )
{
if ( csSubKey.GetAt(i)!=_T('\\') ) csCreateSubKey += csSubKey.GetAt(i);
if ( !csCreateSubKey.IsEmpty() )
{
#ifdef WINCE
LONG lErr = RegCreateKeyEx ( mainKey, csCreateSubKey, 0L, NULL, 0,0, NULL, &hKey, &dwAction );
#else
LONG lErr = RegCreateKeyEx ( mainKey, csCreateSubKey, 0L, _T(""), REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, NULL, &hKey, &dwAction );
#endif
if ( lErr != ERROR_SUCCESS )
{
TRACE ( _T("Last Error : %d\n"), lErr );
return FALSE;
}
}
if ( csSubKey.GetAt(i)==_T('\\') ) csCreateSubKey += csSubKey.GetAt(i);
}
else
{
csCreateSubKey += csSubKey.GetAt(i);
}
if ( i>=csSubKey.GetLength()-1 )
break;
else if ( hKey )
RegCloseKey(hKey);
}
if ( !hKey ) return FALSE;
if ( pHkey ) *pHkey = hKey;
else RegCloseKey(hKey);
return TRUE;
}
/********************************************************************************
* Function Type : Global
* Parameter : mainKey - [in] 主键,如:HKEY_CURRENT_USER
* lpszSubKey - [in] 子键,如:"Software\\MySoft"
* KeyName - [in] 键名,如“SoftName”
* dwType - [in] 数据类型,如“REG_BINARY”
* resBuf - [in] 要写入数据存放的缓冲地址
* bufSize - [in] 缓冲长度
* Return Value : TRUE - writing successfully
* FALSE - error occured when write register
* Description : 将数据写到注册表中保存起来
*********************************************************************************/
BOOL WriteRegister(HKEY mainKey, LPCTSTR lpszSubKey, LPCTSTR KeyName, DWORD dwType, LPVOID resBuf, DWORD *bufSize)
{
DWORD dwRet = 0L;
//打开或生成子键
HKEY hKey;
DWORD dwAction;
if(RegCreateKeyEx(
mainKey,
lpszSubKey,
0L,
_T("hwSoftReg"),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
&dwAction) != ERROR_SUCCESS)
{
return FALSE;
}
//设置值
dwRet = RegSetValueEx(
hKey,
KeyName,
0,
dwType,
(CONST BYTE *)resBuf,
*bufSize);
//关闭键
RegCloseKey(hKey);
return (dwRet == ERROR_SUCCESS);
}
//
// 删除一个键值
//
BOOL DeleteValueRegister ( HKEY mainKey, LPCTSTR lpszSubKey, LPCTSTR lpValueName )
{
ASSERT ( lpValueName );
DWORD dwRet = 0L;
//打开或生成子键
HKEY hKey;
if(RegOpenKeyEx(
mainKey,
lpszSubKey,
0,
KEY_ALL_ACCESS,
&hKey) != ERROR_SUCCESS)
{
return FALSE;
}
// 删除键值
dwRet = RegDeleteValue ( hKey, lpValueName );
//关闭键
RegCloseKey(hKey);
return (dwRet == ERROR_SUCCESS);
}
CString PartKeyBySubKey ( LPCTSTR lpszSubKey, CString *pcsSubKeyLeft/*=NULL*/ )
{
CString csSubKeyLeft, csSubKeyRight, csSubKey = GET_SAFE_STRING(lpszSubKey);
csSubKey.TrimRight ( _T("\\") );
int nFindPos = csSubKey.ReverseFind ( _T('\\') );
if ( nFindPos >= 0 )
{
csSubKeyLeft = csSubKey.Left ( nFindPos );
csSubKeyRight = csSubKey.Mid ( nFindPos + 1 );
}
else
{
csSubKeyRight = csSubKey;
}
if ( pcsSubKeyLeft ) *pcsSubKeyLeft = csSubKeyLeft;
return csSubKeyRight;
}
//
// 删除一个子键
//
BOOL DeleteKeyRegister (
HKEY mainKey, // 如: HKEY_CLASSES_ROOT; HKEY_CURRENT_CONFIG; HKEY_CURRENT_USER; 等等
LPCTSTR lpszSubKey, // 如: "SOFTWARE\\MySoft\\"
LPCTSTR lpSubKeyToDel // 如: "aaa",这里的 "aaa" 是一个子键
)
{
ASSERT ( lpSubKeyToDel );
DWORD dwRet = 0L;
//打开或生成子键
HKEY hKey;
if(RegOpenKeyEx(
mainKey,
lpszSubKey,
0,
KEY_ALL_ACCESS,
&hKey) != ERROR_SUCCESS)
{
return FALSE;
}
// 删除键值
dwRet = RegDeleteKey ( hKey, lpSubKeyToDel );
//关闭键
RegCloseKey(hKey);
return (dwRet == ERROR_SUCCESS);
}
//
// 删除一个子键,连下属所有的子键一同删除掉
//
BOOL RegDeleteAllSubKey (
HKEY mainKey, // 如: HKEY_CLASSES_ROOT; HKEY_CURRENT_CONFIG; HKEY_CURRENT_USER; 等等
LPCTSTR lpszSubKey // 如: "SOFTWARE\\MySoft\\"
)
{
if ( !lpszSubKey ) return FALSE;
BOOL bRet = TRUE;
while ( TRUE )
{
//打开注册表子键
HKEY hKey = NULL;
if ( RegOpenKeyEx ( mainKey, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey ) != ERROR_SUCCESS )
break;
ULONG numSubKey = 0;
RegQueryInfoKey ( hKey, NULL, NULL, NULL, &numSubKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
if ( numSubKey < 1 )
{
RegCloseKey(hKey);
break;
}
TCHAR szEnumSubKey[MAX_KEY_LENGTH] = {0};
ULONG nSubKeySize = MAX_KEY_LENGTH;
if ( ERROR_SUCCESS == RegEnumKeyEx ( hKey, 0,szEnumSubKey, &nSubKeySize, NULL, NULL, NULL, NULL ) &&
lstrlen(szEnumSubKey) > 0 )
{
CString csSubKey = lpszSubKey;
if ( !csSubKey.IsEmpty() && csSubKey.GetAt(csSubKey.GetLength()-1)!=_T('\\') )
csSubKey += _T("\\");
csSubKey += szEnumSubKey;
csSubKey += _T("\\");
// TRACE ( _T("RegEnumKeyEx() : %s\n"), csSubKey );
if ( !RegDeleteAllSubKey ( mainKey, csSubKey ) )
bRet = FALSE;
RegCloseKey(hKey);
}
}
CString csSubKeyLeft, csSubKeyRight;
csSubKeyRight = PartKeyBySubKey ( lpszSubKey, &csSubKeyLeft );
if ( !DeleteKeyRegister ( mainKey, csSubKeyLeft, csSubKeyRight ) )
bRet = FALSE;
return bRet;
}
BOOL StandardExtensionName ( CString &csExtensionName )
{
if ( csExtensionName.IsEmpty() )
return FALSE;
csExtensionName.MakeLower ();
if ( csExtensionName.GetAt(0) != _T('.') ) csExtensionName.Insert ( 0, _T(".") );
return TRUE;
}
CString GetFileTypeDescFromReg ( CString csExtensionName )
{
if ( !StandardExtensionName(csExtensionName) )
return _T("");
CString csFileTypeDesc_FromReg;
DWORD dwBufSize = 1024;
ReadRegister ( HKEY_CLASSES_ROOT, csExtensionName, _T(""), csFileTypeDesc_FromReg.GetBuffer(dwBufSize), &dwBufSize );
csFileTypeDesc_FromReg.ReleaseBuffer ();
return csFileTypeDesc_FromReg;
}
//
// 注册一个文件类型
//
BOOL RegFileType (
CString csExtensionName, // 扩展名,如:"txt"
CString &csFileTypeDesc, // 文件类型,如:"txtfile"
CString csIconParameter, // 图标参数,如:"%SystemRoot%\system32\shell32.dll,-152"
CString csOpenCommand, // 打开这个文件默认的程序和参数,如:"%SystemRoot%\system32\NOTEPAD.EXE %1"
BOOL bDeleteIfExist // 存在时是否删除重新注册
)
{
if ( !StandardExtensionName(csExtensionName) || csFileTypeDesc.IsEmpty() )
return FALSE;
BOOL bExistFileType = FALSE;
CString csSubKey;
//该扩展名的相应子键存在的话,如果需要删除就删除,否则就打开
HKEY hKey = NULL;
if ( RegOpenKeyEx ( HKEY_CLASSES_ROOT,
csExtensionName,
0,
KEY_ALL_ACCESS,
&hKey ) == ERROR_SUCCESS)
{
if ( bDeleteIfExist )
{
RegCloseKey(hKey);
hKey = NULL;
RegDeleteAllSubKey ( HKEY_CLASSES_ROOT, csExtensionName );
}
else
{
bExistFileType = TRUE;
}
}
else hKey = NULL;
BOOL bNeedSetFileTypeDesc = FALSE;
DWORD dwBufSize = 0;
// 获取文件类型描述字符串
if ( hKey )
{
CString csFileTypeDesc_FromReg;
dwBufSize = 1024;
ReadRegister ( HKEY_CLASSES_ROOT, csExtensionName, _T(""), csFileTypeDesc_FromReg.GetBuffer(dwBufSize), &dwBufSize );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?