操作注册表的两种方法实现
方法一:一般操作注册表的方法
注册表内部结构:
key->subkey->value->名称,类型,数据
函数:
RegCreateKey()
RegCreateKeyEx()
RegOpenKey()
RegOpenKeyEx()
RegSetValue()
RegSetValueEx()
RegQueryValue()
RegQueryValueEx()
RegDeleteKey()
RegDeleteKeyEx()
RegCloseKey()
1.写注册表操作
有些系统需要管理员才能对注册表进行修改,所以想要程序运行成功必须要以管理员身份运行,否则将写入失败
HKEY hKey = nullptr;
//创建成功,将得到hKey,一个注册表句柄,用于下面操作注册表
if (ERROR_SUCCESS != RegCreateKey(HKEY_LOCAL_MACHINE, TEXT("SoftWare\\Test"), &hKey))
{
AfxMessageBox(TEXT("创建注册表失败!"));
return;
}
//写入注册表 //类型 //名称 //字符个数
//只能写入一个value
if (ERROR_SUCCESS != RegSetValue(hKey,nullptr,REG_SZ,TEXT("Test"),4))
{
AfxMessageBox(TEXT("写入注册表失败!"));
return;
}
if (hKey ==nullptr)
{
return;
}
DWORD dwData = 100;
LONG lRet = 0;
//这个函数可以写入更多的Value //名称 //类型 //数据
if (ERROR_SUCCESS != (lRet = RegSetValueEx(hKey, TEXT("other"), 0, REG_DWORD, (CONST BYTE*)&dwData, 4)))
{
AfxMessageBox(TEXT("写入注册表失败Ex!"));
return;
}
AfxMessageBox(TEXT("写入注册表成功!"));
RegCloseKey(hKey);
2.读注册表操作
LONG lValue = 0;
//先读取数据的长度
RegQueryValue(HKEY_LOCAL_MACHINE, TEXT("SoftWare\\Test"), nullptr, &lValue);
TCHAR *pBuf = new TCHAR[lValue];
RegQueryValue(HKEY_LOCAL_MACHINE, TEXT("SoftWare\\Test"), pBuf, &lValue);
AfxMessageBox(pBuf);
HKEY hKey = nullptr;
DWORD dwTpye = 0;
DWORD dwValue = sizeof(DWORD);//这个大小一定要先求出来,否则会出错(234)
LPDWORD dwData = new DWORD;//一定要分配内存,否则也会出错
LONG lRet = 0;
//打开注册表
RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SoftWare\\Test"),0, KEY_ALL_ACCESS,&hKey);
//RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SoftWare\\Test"), &hKey);
//读注册表 //valuename //返回value类型 //数据缓存区 //缓存区大小
if (ERROR_SUCCESS != (lRet = RegQueryValueEx(hKey, TEXT("other"), 0, &dwTpye, (LPBYTE)dwData, &dwValue)))
{
AfxMessageBox(TEXT("读取注册表失败!"));
return;
}
AfxMessageBox(TEXT("读取注册表成功!"));
CString s;
s.Format(TEXT("data = %d"), *dwData);
AfxMessageBox(s);
delete dwData;
RegQueryValueEx这个函数根据不同的REG类型有不同的用法,需要谨慎小心。
方法二:超简单操作注册表的方法(超简单,可用)
除了使用方法一中的CRegKey类直接操作注册表,MFC框架还提供了个便捷的方式.
MFC的CWinApp 类提供了很容易的注册表访问函数---以前从来没注意过---看一下下面几个成员函数---
void SetRegistryKey( LPCTSTR lpszRegistryKey ); | Causes application settings to be stored in the registry instead of .INI files. |
void SetRegistryKey( UINT nIDRegistryKey ); | Causes application settings to be stored in the registry instead of .INI files. |
参数:
lpszRegistryKey:字符串指针,包含了键的名字。
nIDRegistryKey:注册表中键的ID/索引。
功能:
SetRegistryKey 这个函数功能是设置MFC程序的注册表访问键,并把读写 ini 文件的成员函数映射到读写注册表。只要调用一下 SetRegistryKey 并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了。
WriteProfileBinary | Writes binary data to an entry in the application's .INI file. |
WriteProfileInt | Writes an integer to an entry in the application's .INI file. |
WriteProfileString | Writes a string to an entry in the application's .INI file. |
GetProfileBinary | Retrieves binary data from an entry in the application's .INI file. |
GetProfileInt | Retrieves an integer from an entry in the application's .INI file. |
GetProfileString | Retrieves a string from an entry in the application's .INI file. |
MSDN上面写上面6个函数是写到INI文件的。所以俺就忽略了其访问注册表的功能。无意中看了其MFC实现才有所了解。
MFC的入口点,继承自CWinApp(.cpp文件里面)的那个类中有个InitInstance()函数即.cpp文件中的InitInstance()函数。我们新建一个project时,该函数默认生成这样一行代码SetRegistryKey(_T("Local AppWizard-Generated Applications"));
这里就是在HKEY_CURRENT_USER \ SOFTWARE 下面新建一个项.你可以把名字改成其他任意值.比如注释掉上面那行代码,改成这样SetRegistryKey(_T("KellyApp"));
在这里这样设置后,以后查看或往注册表添加信息就非常简单了.
往注册表写信息就是
WriteProfileString(_T("Settings"),_T("Password"),_T("123")); //写入字符串值
WriteProfileInt(_T("Settings"),_T("Count"),123); //写入数字
其中第一个参数相当于在前面的KellyApp下面再生成新的项,.然后第二个参数是值名,第三个是键值.当然如果你还想多几层项,可以样
WriteProfileString(_T("ConfigInfo\\Settings"),_T("Password"),_T("123")); //写入字符串值
读取注册表信息 (InitInstance()函数里面)
CString strPassword = GetProfileString(_T("Settings"),_T("Password"));
int num = GetProfileInt(_T("Settings"),_T("Count"));
不过这些函数是CWinApp的成员函数(.cpp文件里面)。在其他地方用,得这样
theApp.WriteProfileString(_T("Settings"),_T("Password"),_T("123")); //写入字符串值
或
AfxGetApp()->WriteProfileString(_T("Settings"),_T("Password"),_T("123")); //写入字符串值
CString strPassword = theApp.GetProfileString("Settings","Password");
或
CString strPassword = AfxGetApp()->GetProfileString("Settings","Password");
AfxGetApp()可以得到CWinApp的指针