当前位置:首页 > 通信资讯 > 正文

1、RegOpenKeyEx 函数:

原形:

LONGRegOpenKeyEx(
HKEYhKey,//要打开主键名
LPCTSTRlpSubKey,// 需要打开的子键或路径
DWORDulOptions,//保留,为0
REGSAMsamDesired,//操作权限标志
PHKEYphkResult//指向你打开键的句柄 (通过指针返回)
);

返回值:不成功返回非0,成功返回ERROR_SUCCESS.
解释:该函数负责打开指定的键或子键,如果不存在他不建立。

查看微软官方文档:http://msdn.microsoft.com/zh-cn/aa912084

2、RegSetValueEx函数:

原形:

LONGRegSetValueEx(
HKEYhKey,//已打开的键的句柄
LPCTSTRlpValueName,//要查询值的名称,传如\"\"为查询键下的默认值
DWORDReserved,//保留
DWORDdwType,//写入键值的类型
CONSTBYTE*lpData,//变量数据的地址
DWORDcbData//变量的长度
);

返回值:不成功返回非0,成功返回ERROR_SUCCESS
解释:设置某子键下特定名称的值。

查看微软官方文档:http://msdn.microsoft.com/zh-cn/aa916717#

3、RegQueryValueEx函数:

原形:

LONGRegQueryValueEx(
HKEYhKey,//已打开的键的句柄
LPTSTRlpValueName,//要查询值的名称,传如\"\"为查询键下的默认值
LPDWORDlpReserved,//保留,为0
LPDWORDlpType,//查询的类型
LPBYTElpData,//数据存放的地址
LPDWORDlpcbData//数据长度+1
);

返回值:不成功返回非0,成功返回ERROR_SUCCESS
解释:读取某子键下特定名称的值。

查看微软官方文档:http://msdn.microsoft.com/zh-cn/aa914692

写入二进制数据代码示例:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 # include <windows.h> # include <tchar.h> int main(void) { HKEY hKey; HKEY rootKey = HKEY_CURRENT_USER; TCHAR * subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer" "\\MenuOrder\\Start Menu2\\Programs\\test"; TCHAR * keyValue = "c:\\test.exe"; long openReg; long setRegValue; DWORD dwType = REG_BINARY; BYTE value[256] = "c:\\test.exe"; openReg = RegOpenKeyEx(rootKey, subKey, 0, KEY_WRITE, &hKey); if (openReg == ERROR_SUCCESS) { setRegValue = RegSetValueEx(hKey, _T("order"), 0, dwType, value, 256); if (setRegValue == ERROR_SUCCESS) { MessageBox(NULL, _T("Write Sucess"), _T("call"), MB_OK); } else { MessageBox(NULL, _T("Write Fail"), _T("call"), MB_OK); } RegCloseKey(hKey); } return 0; }

读取二进制数据的代码示例:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # include <windows.h> # include <tchar.h> int main(void) { HKEY hKey; HKEY rootKey = HKEY_CURRENT_USER; TCHAR * subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer" "\\MenuOrder\\Start Menu2\\Programs\\test"; long openRegResult; long readRegResult; DWORD dwType = REG_BINARY; REGSAM mode = KEY_READ; BYTE value[256] = {0}; DWORD length = 256; openRegResult = RegOpenKeyEx(rootKey, subKey, 0, mode, &hKey); if (ERROR_SUCCESS == openRegResult) { readRegResult = RegQueryValueEx(hKey, _T("order"), 0, &dwType, value, &length); if (ERROR_SUCCESS == readRegResult) { MessageBox(NULL, _T(value), _T("call"), MB_OK); } else { MessageBox(NULL, _T("ERROR"), _T("call"), MB_OK); } } RegCloseKey(hKey); return 0; }

注:读写其他类型的注册表键值与上述的类似,不单独讲解了。

原文链接:https://blog.csdn.net/igolang/article/details/41212401

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。