C++ 读写注册表

最近做项目的原因,要用到C++操作注册表的程序,结果找网上的博客,发现没有一个对的,路径好多的符号写反了。。。于是把自己能用的代码放上来。

代码:

 

  1 #include <iostream>  
  2 #include <algorithm>  
  3 #include <cmath>  
  4 #include <vector>  
  5 #include <string>  
  6 #include <cstring>
  7 #include <atlbase.h>
  8 #include <Windows.h>
  9 #pragma warning(disable:4996)  
 10 using namespace std;
 11 
 12 void read_dword()//读取操作表,其类型为DWORD
 13 {
 14     HKEY hKEY;//定义有关的键,在查询结束时关闭
 15     //打开与路径data_Set相关的hKEY
 16 
 17     LPCTSTR data_set = _T("Software\\Chicony\\Lenovo1"); 
 18     
 19     //访问注册表,hKEY则保存此函数所打开的键的句柄
 20     if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_READ, &hKEY))
 21     {
 22         DWORD dwValue;//长整型数据,如果是字符串数据用char数组
 23         DWORD dwSize = sizeof(DWORD);
 24         DWORD dwType = REG_DWORD;
 25 
 26         if (::RegQueryValueEx(hKEY, _T("123"), 0, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
 27         {
 28             cout << "错误:无法查询有关的注册表信息" << endl;
 29         }
 30 
 31         cout << dwValue << endl;
 32     }
 33     ::RegCloseKey(hKEY);
 34 }
 35 
 36 void read_reg_sz()//读取操作表,其类型为REG_SZ
 37 {
 38     HKEY hkey;
 39     LPCTSTR data_set = _T("Software\\Chicony\\Lenovo1");
 40 
 41     if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_READ, &hkey))
 42     {
 43         char dwValue[256];
 44         DWORD dwSzType = REG_SZ;
 45         DWORD dwSize = sizeof(dwValue);
 46         if (::RegQueryValueEx(hkey, _T("wangchong"), 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
 47         {
 48             cout << "无法查询有关的注册表信息" << endl;
 49         }
 50         cout << dwValue<< endl;
 51     }
 52     ::RegCloseKey(hkey);
 53 }
 54 
 55 void write_dword()//在\Software\\Chicony\\Lenovo1文件夹下写入一个test111的子键,设置其名称为Name,其值为6
 56 {
 57     HKEY hkey;//定义有关的hkey,在查询结束时要关闭
 58     HKEY hTempKey;
 59     
 60     DWORD dwValue = 6;
 61     DWORD dwSize = sizeof(DWORD);
 62     DWORD dwType = REG_DWORD;
 63 
 64     LPCTSTR data_set= _T("Software\\Chicony\\Lenovo1");
 65     if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
 66     {
 67         if (ERROR_SUCCESS == ::RegCreateKey(hkey, _T("test111"), &hTempKey))
 68         {
 69             if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, _T("Name"), 0, REG_DWORD, (CONST BYTE*)&dwValue, sizeof(DWORD)))
 70             {
 71                 cout <<"写入注册表失败"<< endl;
 72             }
 73         }
 74     }
 75     ::RegCloseKey(hkey);
 76 }
 77 
 78 void write_reg_sz()
 79 {
 80     HKEY hkey;
 81     HKEY hTempKey;   
 82     char m_name_set[256]="China";
 83 
 84     DWORD len = strlen(m_name_set) + 1;
 85     LPCTSTR data_set = _T("Software\\Chicony\\Lenovo1");
 86     if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
 87     {
 88         if (ERROR_SUCCESS == ::RegCreateKey(hkey,_T("test1112"),&hTempKey))
 89         {
 90             if (ERROR_SUCCESS!=::RegSetValueEx(hTempKey,_T("Name"),0,REG_SZ,(const BYTE*)m_name_set,len))
 91             {
 92                 cout << "写入错误" << endl;
 93             }
 94         }
 95     }
 96     ::RegCloseKey(hkey);
 97 }
 98 
 99 void write_binary()
100 {
101     HKEY hkey;
102     HKEY hTempKey;
103     BYTE m_name[10];
104     memset(m_name, 0, sizeof(m_name));
105     m_name[0] = 0xff;
106     m_name[1] = 0xac;
107     m_name[2] = 0x05;
108     m_name[3] = 0x4e;
109 
110     LPCTSTR data_set= _T("Software\\Chicony\\Lenovo1");
111     if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
112     {
113         if (ERROR_SUCCESS==::RegCreateKey(hkey,_T("test111"),&hTempKey))
114         {
115             if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, _T("Name"), 0, REG_BINARY, (unsigned char *)m_name, 5))
116             {
117                 cout << "写入错误" << endl;
118             }
119         }
120     }
121     ::RegCloseKey(hkey);
122 }
123 
124 void delete_value()
125 {
126     HKEY hkey;
127     LPCTSTR data_set = _T("Software\\Chicony\\Lenovo1\\test1112");
128 
129     if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
130     {
131         if (ERROR_SUCCESS != ::RegDeleteValue(hkey, _T("Name")))
132         {
133             cout << "删除错误" << endl;
134         }
135     }
136     ::RegCloseKey(hkey);
137 }
138 
139 void delete_key()
140 {
141     HKEY hkey;
142     LPCTSTR data_set = _T("Software\\Chicony\\Lenovo1");
143 
144     if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
145     {
146         if (ERROR_SUCCESS != ::RegDeleteKey(hkey,"test1112"))
147         {
148             cout << "删除错误" << endl;
149         }
150     }
151     ::RegCloseKey(hkey);
152 }
153 
154 int main()
155 {    
156     read_dword();
157     read_reg_sz();
158     write_reg_sz();
159     write_binary();
160     delete_value();
161     delete_key();
162     system("pause");  
163     return 0;
164 }

 

效果:

 

posted on 2015-10-22 15:35  光速小子  阅读(855)  评论(0编辑  收藏  举报

导航