写注册表字符串值:
1 //访问注册表,hKEY则保存此函数所打开的键的句柄
2 HKEY hKey;
3 //如果无法打开hKEY,则创建
4 if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\EMFPrinter"
5 , 0, KEY_READ|KEY_WRITE|KEY_SET_VALUE, &hKey))
6 {
7 if (ERROR_SUCCESS != RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\EMFPrinter",&hKey))
8
9 {
10 LOG("创建注册表 %s 失败", hKey);
11 return FALSE;
12 }
13 }
14
15 DWORD dwTypeString = REG_SZ;
16 TCHAR pDirBuf[255] = {0};
17 DWORD len = 255;
18 //如果无法查询有关的注册表信息,设置相关注册表信息
19 if (ERROR_SUCCESS != RegQueryValueEx(hKey, "", 0, &dwTypeString,(LPBYTE)pDirBuf, &len))
20 {
21 RegSetValueEx(hKey, "", 0, REG_SZ,NULL, 0);
22 }
23 RegCloseKey(hKey);
读取注册表中DWORD值并且改变其值:
1 CString sPrinterRegPath = "SYSTEM\\CurrentControlSet\\Control\\Print\\Printers\\dd";
2 HKEY hkey;
3 if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, sPrinterRegPath, 0, KEY_READ|KEY_WRITE|KEY_SET_VALUE,
4 &hkey))
5 {
6 LOG("打开打印机注册表项%s失败", sPrinterRegPath);
7 return FALSE;
8 }
9
10 DWORD dwTypeString = REG_SZ;
11 DWORD len = 255;
12 DWORD dwValue;
13 DWORD dwData = 1;
14 //如果存在有关的注册表信息,修改相关注册表信息
15 if (ERROR_SUCCESS == RegQueryValueEx(hkey, "123", 0, &dwTypeString,(BYTE*)&dwValue, &len))
16 {
17 if (ERROR_SUCCESS == RegSetValueEx(hkey, "123", 0, REG_DWORD,(LPBYTE)&dwData, sizeof(DWORD)))
18 {
19 LOG("修改注册表成功!")
20 }
21 }
posted on 2012-04-13 17:54
王海光 阅读(1015)
评论(0) 编辑 收藏 引用 所属分类:
MFC