注册表操作函数封装,并实现开机自启动、电脑安装程序信息获取、开机提示界面设置功能

  1 #ifndef BCL_REGEDIT_H
  2 #define BCL_REGEDIT_H
  3 
  4 #if defined(BCL_EXPORT)
  5 #define BCL_API extern "C" __declspec(dllexport)
  6 #else
  7 #define BCL_API extern "C" __declspec(dllimport)
  8 #endif
  9 #ifndef CALLBACK
 10 #define CALLBACK __stdcall
 11 #endif
 12 #include <string>
 13 #include <list>
 14 #include <windows.h>
 15 #define BCL_SMALL_BUFFER_LEN  256
 16 using namespace std;
 17 
 18 struct ProgramInfo
 19 {
 20     string PiName;//名称
 21     string PiPath;//路径
 22     string PiPublisher;//发布者
 23     string PiVersion;//版本
 24     string PiUninstall;//卸载
 25     string PiInstallTime;//安装时间
 26     int   PiSize;//大小
 27     ProgramInfo()
 28     {
 29         PiName="";
 30         PiPath="";
 31         PiPublisher="";
 32         PiVersion="";
 33         PiUninstall="";
 34         PiInstallTime="";
 35         PiSize=0;
 36     }
 37 
 38 };
 39 
 40 /****************
 41 功能:向注册表的某个键中添加一个键值项
 42 参数:
 43 HKEY hRootKey             in    根键名称
 44 string strKeyItermPath,     in    键值项路径或名称
 45 string strKeyItermNane,    in    键值项名称
 46 DWORD nItermType       in    键值的类型
 47 void* cItermValue ,          in     键值的值
 48 DWORD nItermValueLen            in     键值长度
 49 返回值:0成功,>0 失败
 50 *****************/
 51 BCL_API int CALLBACK BCL_RegAddKeyIterm(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,DWORD nItermType,void* cItermValue ,DWORD nItermValueLen);
 52 /****************
 53 功能:删除注册表的某个键的键值项
 54 参数:
 55 HKEY hRootKey             in    根键名称
 56 string strKeyItermPath,     in    键值项路径或名称,为空时,表示当前目录
 57 string strKeyItermNane,    in    键值项名称
 58 返回值:0成功,>0 失败
 59 *****************/
 60 BCL_API int CALLBACK BCL_RegDeleteKeyIterm(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane);
 61 /****************
 62 功能:设置注册表的某个键的键值项
 63 参数:
 64 HKEY hRootKey             in    根键名称
 65 string strKeyItermPath,     in    键值项路径或名称,为空时,表示当前目录
 66 string strKeyItermNane,    in    键值项名称
 67 DWORD nItermType       in    键值的类型
 68 void* cItermValue ,          in     键值的值
 69 DWORD nItermValueLen  in     键值长度
 70 返回值:0成功,>0 失败
 71 *****************/
 72 BCL_API int CALLBACK BCL_RegSetKeyIterm(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,DWORD nItermType,void* cItermValue ,DWORD nItermValueLen);
 73 /****************
 74 功能:获取注册表的某个键的键值项
 75 参数:
 76 HKEY hRootKey             in    根键名称
 77 string strKeyItermPath,     in    键值项路径或名称,为空时,表示当前目录
 78 string strKeyItermNane,    in    键值项名称
 79 DWORD nItermType       out    键值的类型
 80 void* cItermValue ,        out     键值的值
 81 DWORD* nItermValueLen  out     传入键值缓冲区的长度,传出键值的长度
 82 返回值:0成功,>0 失败
 83 *****************/
 84 BCL_API int CALLBACK BCL_RegGetKeyIterm(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,DWORD* nItermType,void* cItermValue ,DWORD* nItermValueLen);
 85 
 86 /****************
 87 功能:获取字符串类型REG_SZ的键值项
 88 参数:
 89 HKEY hRootKey             in    根键名称
 90 string strKeyItermPath,     in    键值项路径或名称,为空表示当前路径
 91 string strKeyItermNane,    in    键值项名称
 92 ,string& strItermValue              out  string类型的值
 93 返回值:0成功,>0 失败
 94 *****************/
 95 BCL_API int CALLBACK BCL_RegGetKeyItermSZ(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,string& strItermValue);
 96 
 97 /****************
 98 功能:获取字符串类型REG_SZ的键值项
 99 参数:
100 HKEY hRootKey             in    根键名称
101 string strKeyItermPath,     in    键值项路径或名称,为空表示当前路径
102 string strKeyItermNane,    in    键值项名称
103 ,DWORD* pdwItermValue       out  DWORD类型的值
104 返回值:0成功,!=0失败
105 *****************/
106 BCL_API int CALLBACK BCL_RegGetKeyItermDWORD(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,DWORD* pdwItermValue);
107 
108 /****************
109 功能:向注册表添加一个键,已有键打开
110 参数:
111 HKEY hRootKey             in    根键名称
112 string strKeyPath,     in    键路径或名称,为空时,表示当前目录
113 HKEY& hKey         out  添加的键句柄
114 DWORD*  lpNewOrOpen  out   1新建 2打开已有键
115 返回值:0成功,-1失败
116 *****************/
117 BCL_API int CALLBACK BCL_RegAddKey(HKEY hRootKey,string strKeyPath,HKEY* hKey,DWORD*  lpNewOrOpen);
118 /****************
119 功能:删除某个键下的子健和键值项,递归删除
120 参数:
121 HKEY hRootKey             in    根键名称
122 string strKeyPath,     in    键值项路径或名称,为空时,表示当前目录
123 返回值:0成功,>0 失败
124 *****************/
125 
126 BCL_API int CALLBACK BCL_RegDeleteKey(HKEY hRootKey,string strKeyPath);
127 /****************
128 功能:打开某个键,返回句柄
129 参数:
130 HKEY hRootKey             in    根键或者
131 string strKeyPath,     in    键值项路径或名称,,为空时,表示当前目录
132 HKEY* hOpenKey  out  返回的键句柄
133 返回值:0成功,>0 失败
134 *****************/
135 BCL_API int CALLBACK BCL_RegOpenKey(HKEY hRootKey,string strKeyPath,HKEY* hOpenKey);
136 
137 /****************
138 功能:关闭某个键,返回句柄
139 参数:
140 HKEY hRootKey             in    根键名称
141 string strKeyPath,     in    键值项路径或名称,为空时,表示当前目录
142 HKEY& hKey  out  返回的键句柄
143 返回值:0成功,>0 失败
144 *****************/
145 BCL_API int CALLBACK BCL_RegCloseKey(HKEY& hKey);
146 /****************
147 功能:轮询一个键的子健
148 参数:
149 HKEY hRootKey,   in   主键或者键
150 string strKeyPath   in  子健路径或名称,如果为空,则hRootKey为当前子健
151 DWORD dwIndex   in  查询的键的索引,从0开始,没有的则返回ERROR_NO_MORE_ITEMS
152 DWORD*  dwKeyNameLen
153 返回值:0成功,>0错误码
154 *****************/
155 BCL_API int CALLBACK BCL_RegEnumKey(HKEY hRootKey,string strKeyPath,DWORD dwIndex,string& strKeyName,DWORD*  dwKeyNameLen);
156 /****************
157 功能:轮询一个键的键值
158 参数:
159 HKEY hRootKey,   in   主键或者键
160 string strKeyPath   in  子健路径或名称,如果为空,则hRootKey为当前子健
161 DWORD dwIndex   in  查询的键的索引,从0开始,没有的则返回ERROR_NO_MORE_ITEMS
162 DWORD*  dwKeyNameLen
163 返回值:0成功,>0错误码
164 *****************/
165 BCL_API int CALLBACK BCL_RegEnumKeyIterm(HKEY hRootKey,string strKeyPath,DWORD dwIndex,string& strItermName,DWORD* dwItermType,LPBYTE bItermValue,DWORD* dwItermValueLen);
166 
167 /****************
168 功能:保存一个键下的子健和键值信息到文件
169 参数:
170 HKEY hRootKey,   in   主键或者键
171 string strKeyPath   in  子健路径或名称,如果为空,则hRootKey为当前子健
172 string strFilePath  in  保存文件的名称包含路径
173 返回值:0成功,>0错误码
174 *****************/
175 BCL_API int CALLBACK BCL_RegSaveKeyInfoToFile(HKEY hRootKey,string strKeyPath,string strFilePath);
176 /****************
177 功能:将一个文件的信息导入到一个键下
178 参数:
179 HKEY hRootKey,   in   主键或者键
180 string strKeyPath   in  子健路径或名称,如果为空,则hRootKey为当前子健
181 string strFilePath  in  保存文件的名称包含路径
182 返回值:0成功,>0错误码
183 *****************/
184 BCL_API int CALLBACK BCL_RegLoadKeyInfoFromFile(HKEY hRootKey,string strKeyPath,string strFilePath);
185 
186 /****************
187 功能:查询一个键的子健个数和子健名称最大长度,键值个数
188 参数:
189 HKEY hRootKey,   in   主键或者键
190 string strKeyPath   in  子健路径或名称,如果为空,则hRootKey为当前子健
191 DWORD* dwsubKeyNum,  out  子健数量
192 DWORD* dwMaxKeyNameLen,  out  子健名称最大长度
193 DWORD*  dwItermNum,       out 键值数量
194 DWORD*  dwMaxItermNameLen   out  键值名称最大长度
195 DWORD*  dwMaxItermValueLen   out  键值最大长度
196 返回值:0成功,>0错误码
197 *****************/
198 BCL_API int CALLBACK BCL_RegQueryKeyInfo(HKEY hRootKey,string strKeyPath,DWORD* dwsubKeyNum,DWORD* dwMaxKeyNameLen,DWORD*  dwItermNum,DWORD*  dwMaxItermNameLen,DWORD*  dwMaxItermValueLen);
199 /****************
200 功能:添加一个程序到开机自启动菜单
201 参数:
202 string strProgramName ,程序名称
203 string strKeyPath  程序路径或名称,为空时,表示当前目录
204 返回值:0成功,>0 失败
205 *****************/
206 
207 BCL_API int CALLBACK BCL_RegAddProgramToAutoStartManu(string strProgramName ,string strProgramPath);
208 /****************
209 功能:删除一个程序到开机自启动菜单
210 参数:
211 string strProgramName ,程序名称
212 返回值:0成功,-1失败
213 *****************/
214 BCL_API int CALLBACK BCL_RegDeleteProgramFromAutoStartManu(string strProgramName);
215 /****************
216 功能:从注册表中获取安装程序的信息
217 参数:
218 list<ProgramInfo>&  listProgramInfo,程序名称
219 返回值:0成功,>0 失败
220 *****************/
221 BCL_API int CALLBACK BCL_RegGetProgramsInfo(list<ProgramInfo>&  listProgramInfo);
222 
223 /****************
224 功能:添加开机启动时显示的提示信息
225 参数:
226 string strTitle,  in 标题
227 string strContent   in 内容
228 返回值:0成功,>0 失败
229 *****************/
230 BCL_API int CALLBACK BCL_RegAddTipTitleAndContent(string strTitle,string strContent);
231 /****************
232 功能:删除开机启动时显示的提示信息
233 参数:
234 
235 返回值:0成功,>0 失败
236 *****************/
237 BCL_API int CALLBACK BCL_RegRemoveTipTitleAndContent();
238 
239 
240 #endif
BCL_Regedit.h
  1 // BCL_Regedit.cpp : 定义 DLL 应用程序的导出函数。
  2 //
  3 
  4 #include "stdafx.h"
  5 #define BCL_EXPORT
  6 #include "BCL_Regedit.h"
  7 
  8 
  9 #include "stdafx.h"
 10 #include <iostream>
 11 #include <windows.h>
 12 #include <string>
 13 
 14 //// wchar_t to string
 15 //std::string WCharToString(LPCWSTR pwszSrc)
 16 //{
 17 //    int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL); 
 18 //    if (nLen<= 0) return std::string("");
 19 //    char* pszDst = new char[nLen];
 20 //    if (NULL == pszDst) return std::string("");
 21 //    WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
 22 //    pszDst[nLen -1] = 0;
 23 //    std::string strTemp(pszDst);
 24 //    delete [] pszDst;
 25 //    return strTemp;
 26 //}
 27 //// string to wstring
 28 //int StringToWstring(wchar_t* szDst, int bufflen ,std::string str)
 29 //{
 30 //    std::string temp = str;
 31 //    int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL,0); 
 32 //    if (len>bufflen)
 33 //    {
 34 //        return 0;
 35 //    }
 36 //    MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1,szDst, len);
 37 //    return len;
 38 //}
 39 
 40 
 41 
 42 
 43 BCL_API int CALLBACK BCL_RegAddKeyIterm(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,DWORD nItermType,void* cItermValue ,DWORD nItermValueLen)
 44 {
 45 
 46     if (cItermValue==NULL||nItermValueLen<=0||nItermType<0||nItermType>11)
 47     {
 48         return -1;
 49     }
 50     HKEY hSubKey;
 51     int nReval=-1;
 52     if (strKeyItermPath=="")
 53     {
 54         hSubKey=hRootKey;
 55     }
 56     else
 57     {
 58         nReval=RegOpenKeyEx(hRootKey,LPCTSTR(strKeyItermPath.c_str()),0,KEY_ALL_ACCESS,&hSubKey);
 59         if (nReval!=ERROR_SUCCESS)
 60         {
 61             return nReval;
 62         }
 63     }
 64     if (nItermType==REG_SZ||nItermType==REG_EXPAND_SZ||nItermType==REG_MULTI_SZ)
 65     {
 66         nItermValueLen=nItermValueLen+1;//字符串类型的必须加上结束符
 67     }
 68     nReval=RegSetValueEx(hSubKey,LPCTSTR(strKeyItermNane.c_str()),0,nItermType,(BYTE *)cItermValue,nItermValueLen);
 69     //RegCloseKey(hSubKey);
 70     return nReval;
 71 }
 72 
 73 BCL_API int CALLBACK BCL_RegDeleteKeyIterm(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane)
 74 {
 75     int nReval=-1;
 76     if (strKeyItermPath=="")
 77     {
 78         return RegDeleteValue(hRootKey,LPCTSTR (strKeyItermNane.c_str()));
 79     }
 80     else
 81     {
 82         return RegDeleteKeyValue(hRootKey,LPCTSTR(strKeyItermPath.c_str()),LPCTSTR (strKeyItermNane.c_str()));    
 83     }
 84 }
 85 
 86 
 87 BCL_API int CALLBACK BCL_RegSetKeyIterm(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,DWORD nItermType,void* cItermValue ,DWORD nItermValueLen)
 88 {
 89     int nReval;
 90     if (cItermValue==NULL||nItermValueLen<=0||nItermType<0||nItermType>11)
 91     {
 92         return -1;
 93     }
 94     if (strKeyItermPath=="")
 95     {
 96         nReval=RegSetValueEx(hRootKey,LPCTSTR(strKeyItermNane.c_str()),0,nItermType,(BYTE *)cItermValue,nItermValueLen);
 97     }
 98     else
 99     {
100         nReval=RegSetKeyValue(hRootKey,LPCTSTR(strKeyItermPath.c_str()),LPCSTR(strKeyItermNane.c_str()),nItermType,LPCVOID(cItermValue),nItermValueLen);
101     }
102     return nReval;
103 }
104 BCL_API int CALLBACK BCL_RegGetKeyIterm(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,DWORD* nItermType,void* cItermValue ,DWORD* nItermValueLen)
105 {
106     long nReval=-1;
107     if (cItermValue==NULL)
108     {
109         return nReval;
110     }
111     DWORD far dwRelen=0;//
112     if (strKeyItermPath=="")
113     {
114         nReval=RegQueryValueEx(hRootKey,LPCSTR(strKeyItermNane.c_str()),NULL,nItermType,(LPBYTE)cItermValue,&dwRelen);
115     } 
116     else
117     {
118         nReval=RegGetValue(hRootKey,LPCSTR(strKeyItermPath.c_str()),LPCSTR(strKeyItermNane.c_str()),RRF_RT_ANY,nItermType,cItermValue,&dwRelen);
119         if (nReval==ERROR_SUCCESS||dwRelen<=*nItermValueLen)
120         {
121             *nItermValueLen=dwRelen;
122             return 0;
123         }
124     }
125     return nReval;
126 }
127 BCL_API int CALLBACK BCL_RegGetKeyItermSZ(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,string& strItermValue)
128 {
129     long nReval=-1;
130     DWORD nItermType=REG_SZ;
131     DWORD nItermValueLen=512;
132     if (strKeyItermPath=="")
133     {
134         nReval=RegQueryValueEx(hRootKey,LPCSTR(strKeyItermNane.c_str()),NULL,&nItermType,(LPBYTE)(strItermValue.c_str()),&nItermValueLen);
135     } 
136     else
137     {
138         nReval=RegGetValue(hRootKey,LPCSTR(strKeyItermPath.c_str()),LPCSTR(strKeyItermNane.c_str()),RRF_RT_ANY,&nItermType,PVOID(strItermValue.c_str()),&nItermValueLen);
139     }
140     return nReval;
141 }
142 BCL_API int CALLBACK BCL_RegGetKeyItermDWORD(HKEY hRootKey,string strKeyItermPath,string strKeyItermNane,DWORD* pdwItermValue)
143 {
144     if (pdwItermValue==NULL)
145     {
146         return -1;
147     }
148     long nReval=-1;
149     DWORD nItermType=REG_DWORD;
150     DWORD nItermValueLen=sizeof(DWORD);
151     if (strKeyItermPath=="")
152     {
153         nReval=RegQueryValueEx(hRootKey,LPCSTR(strKeyItermNane.c_str()),NULL,&nItermType,(LPBYTE)(pdwItermValue),&nItermValueLen);
154     } 
155     else
156     {
157         nReval=RegGetValue(hRootKey,LPCSTR(strKeyItermPath.c_str()),LPCSTR(strKeyItermNane.c_str()),RRF_RT_ANY,&nItermType,PVOID(pdwItermValue),&nItermValueLen);
158     }
159     return nReval;
160 }
161 BCL_API int CALLBACK BCL_RegAddKey(HKEY hRootKey,string strKeyPath,HKEY* hKey ,DWORD*  lpNewOrOpen)
162 {
163     return RegCreateKeyEx(hRootKey,LPCSTR(strKeyPath.c_str()),0,NULL,0,KEY_ALL_ACCESS,NULL,hKey,lpNewOrOpen);
164 }
165 BCL_API int CALLBACK BCL_RegDeleteKey(HKEY hRootKey,string strKeyPath)
166 {
167     if(strKeyPath=="")
168     {
169         return RegDeleteTree(hRootKey,NULL);
170     }
171     return RegDeleteTree(hRootKey,LPCSTR(strKeyPath.c_str()));
172 }
173 
174 
175 BCL_API int CALLBACK BCL_RegOpenKey(HKEY hRootKey,string strKeyPath,HKEY* hOpenKey)
176 {
177     return RegOpenKeyEx(hRootKey,LPCSTR(strKeyPath.c_str()),0,KEY_ALL_ACCESS,hOpenKey);
178 }
179 
180 BCL_API int CALLBACK BCL_RegCloseKey(HKEY& hKey)
181 {
182     return RegCloseKey(hKey);
183 }
184 BCL_API int CALLBACK BCL_RegEnumKey(HKEY hRootKey,string strKeyPath,DWORD dwIndex,string& strKeyName,DWORD*  dwKeyNameLen)
185 {
186     long lResult=-1;
187     HKEY hSubKey;
188     if (strKeyPath!="")
189     {
190         lResult=BCL_RegOpenKey(hRootKey,strKeyPath,&hSubKey);
191         if (lResult!=ERROR_SUCCESS)
192         {
193             return lResult;
194         }
195     }
196     else
197     {
198         hSubKey=hRootKey;
199     }
200     lResult=RegEnumKeyEx(hSubKey,dwIndex,LPSTR(strKeyName.c_str()),dwKeyNameLen,0,NULL,NULL,NULL);
201     if (strKeyPath!="")
202     {
203         BCL_RegCloseKey(hSubKey);
204     }
205     return lResult;
206 }
207 
208 BCL_API int CALLBACK BCL_RegEnumKeyIterm(HKEY hRootKey,string strKeyPath,DWORD dwIndex,string& strItermName,DWORD* dwItermType,LPBYTE bItermValue,DWORD* dwItermValueLen)
209 {
210     long lResult=-1;
211     HKEY hSubKey;
212     if (strKeyPath!="")
213     {
214         lResult=BCL_RegOpenKey(hRootKey,strKeyPath,&hSubKey);
215         if (lResult!=ERROR_SUCCESS)
216         {
217             return lResult;
218         }
219     }
220     else
221     {
222         hSubKey=hRootKey;
223     }
224     lResult=RegEnumValue(hSubKey,dwIndex,LPSTR(strItermName.c_str()),NULL,NULL,dwItermType,bItermValue,dwItermValueLen);
225     if (strKeyPath!="")
226     {
227         BCL_RegCloseKey(hSubKey);
228     }
229     return lResult;
230 }
231 BCL_API int CALLBACK BCL_RegQueryKeyInfo(HKEY hRootKey,string strKeyPath,DWORD* dwsubKeyNum,DWORD* dwMaxKeyNameLen,DWORD*  dwItermNum,DWORD*  dwMaxItermNameLen,DWORD*  dwMaxItermValueLen)
232 {
233     long lResult=-1;
234     HKEY hSubKey;
235     if (strKeyPath!="")
236     {
237         lResult=BCL_RegOpenKey(hRootKey,strKeyPath,&hSubKey);
238         if (lResult!=ERROR_SUCCESS)
239         {
240             return lResult;
241         }
242     }
243     else
244     {
245         hSubKey=hRootKey;
246     }
247     lResult=RegQueryInfoKey(hSubKey,NULL,NULL,NULL,dwsubKeyNum,dwMaxKeyNameLen,NULL,dwItermNum,dwMaxItermNameLen, dwMaxItermValueLen,NULL,NULL);
248     if (strKeyPath!="")
249     {
250         BCL_RegCloseKey(hSubKey);
251     }
252     return lResult;
253 
254 }
255 BCL_API int CALLBACK BCL_RegSaveKeyInfoToFile(HKEY hRootKey,string strKeyPath,string strFilePath)
256 {
257     if (strKeyPath=="")
258     {
259         return RegSaveKeyEx(hRootKey,strFilePath.c_str(),NULL,REG_LATEST_FORMAT);
260     }
261     else
262     {
263         HKEY subkey;
264         int lResult=BCL_RegOpenKey(hRootKey,strKeyPath,&subkey);
265         if (lResult!=ERROR_SUCCESS)
266         {
267             return lResult;
268         }
269         lResult=RegSaveKeyEx(subkey,strFilePath.c_str(),NULL,REG_LATEST_FORMAT);
270         RegCloseKey(subkey);
271         return lResult;
272     }
273 }
274 BCL_API int CALLBACK BCL_RegLoadKeyInfoFromFile(HKEY hRootKey,string strKeyPath,string strFilePath)
275 {
276     if (strKeyPath=="")
277     {
278         return RegLoadKey(hRootKey,strFilePath.c_str(),NULL,REG_LATEST_FORMAT);
279     }
280     else
281     {
282         HKEY subkey;
283         int lResult=BCL_RegOpenKey(hRootKey,strKeyPath,&subkey);
284         if (lResult!=ERROR_SUCCESS)
285         {
286             return lResult;
287         }
288         lResult=RegSaveKeyEx(subkey,strFilePath.c_str(),NULL,REG_LATEST_FORMAT);
289         RegCloseKey(subkey);
290         return lResult;
291     }
292 }
293 BCL_API int CALLBACK BCL_RegAddProgramToAutoStartManu(string strProgramName ,string strProgramPath)
294 {
295     int lResult =-1;
296     DWORD nPathLen = sizeof(strProgramPath)*2; 
297     lResult=BCL_RegAddKeyIterm(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",strProgramName,REG_SZ,(void *)strProgramPath.c_str(),nPathLen);
298     return lResult;
299 }
300 BCL_API int CALLBACK BCL_RegDeleteProgramFromAutoStartManu(string strProgramName)
301 {
302     return BCL_RegDeleteKeyIterm(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",strProgramName);
303 }
304 
305 BCL_API int CALLBACK BCL_RegGetProgramsInfo(list<ProgramInfo>&  listProgramInfo)
306 {
307     HKEY hSubKey;
308     DWORD dwIndex = 0;
309     long lResult;
310     string location;
311     //SYSTEM_INFO si; 
312     //GetNativeSystemInfo(&si); 
313     //if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
314     //    si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_IA64 ) 
315     //{ 
316     //    //64 位操作系统 
317     //    location="SoftWare\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
318     //} 
319     //else
320     //{ 
321     //    // 32 位操作系统 
322     //    location="SoftWare\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
323     //}
324     lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SoftWare\\Microsoft\\Windows\\CurrentVersion\\Uninstall"), 0, KEY_ALL_ACCESS, &hSubKey);        // 打开注册表Uninstall键
325     if(ERROR_SUCCESS == lResult) 
326     {
327         DWORD typeSZ = REG_SZ;            // SZ 注册表类型
328         DWORD typeDWORD = REG_DWORD;    // DWORD 注册表类型
329 
330         char s_keyName[BCL_SMALL_BUFFER_LEN] = {0}; 
331         DWORD cbkeyNamelen =BCL_SMALL_BUFFER_LEN;    
332         char s_itermName[BCL_SMALL_BUFFER_LEN]={0};
333         DWORD cbItermNameLen=BCL_SMALL_BUFFER_LEN;
334         DWORD s_EstimatedSize;                    // 大小
335         DWORD cbDWsize =BCL_SMALL_BUFFER_LEN; 
336 
337         lResult = RegEnumKeyEx(hSubKey, dwIndex, s_keyName, &cbkeyNamelen, 0, NULL, NULL, NULL);        // 获取字段
338         while (lResult == ERROR_SUCCESS && lResult != ERROR_MORE_DATA)
339         {    
340             ProgramInfo infos;
341             HKEY hItem;        // 下一级注册表句柄
342             if(::RegOpenKeyEx(hSubKey, s_keyName, 0, KEY_ALL_ACCESS, &hItem) == ERROR_SUCCESS)
343             {        // 提取下一级注册表程序的属性信息
344                 int irrel=-1;
345                 cbItermNameLen =BCL_SMALL_BUFFER_LEN;  
346                 irrel=::RegQueryValueEx(hItem, TEXT("DisplayName"), 0, &typeSZ, (LPBYTE)s_itermName, &cbItermNameLen);//显示名称
347                 if ( irrel== ERROR_SUCCESS) 
348                 {
349                     infos.PiName=(s_itermName);
350                 }
351                 memset(s_itermName,0,BCL_SMALL_BUFFER_LEN);
352                 cbItermNameLen =BCL_SMALL_BUFFER_LEN; 
353                 irrel=::RegQueryValueEx(hItem, TEXT("DisplayIcon"), 0, &typeSZ, (LPBYTE)s_itermName, &cbItermNameLen) ;//可执行文件路径 
354                 if (irrel== ERROR_SUCCESS) 
355                 {
356                     infos.PiPath= (s_itermName);
357                     
358                 }
359                 memset(s_itermName,0,BCL_SMALL_BUFFER_LEN);
360                 cbItermNameLen =BCL_SMALL_BUFFER_LEN; 
361                 irrel=::RegQueryValueEx(hItem, TEXT("UninstallString"), 0, &typeSZ, (LPBYTE)s_itermName, &cbItermNameLen) ;//卸载程序路径
362                 if (irrel== ERROR_SUCCESS) 
363                 {
364                     infos.PiUninstall= (s_itermName);    
365                 }
366                 memset(s_itermName,0,BCL_SMALL_BUFFER_LEN);
367                 cbItermNameLen = BCL_SMALL_BUFFER_LEN; 
368                 irrel=::RegQueryValueEx(hItem,TEXT("Publisher"), 0, &typeSZ, (LPBYTE)s_itermName, &cbItermNameLen) ;//发布者
369                 if (irrel== ERROR_SUCCESS)
370                 {
371                     infos.PiPublisher= (s_itermName);
372             
373                 }
374                 memset(s_itermName,0,BCL_SMALL_BUFFER_LEN);
375                 cbItermNameLen = BCL_SMALL_BUFFER_LEN; 
376                 irrel=::RegQueryValueEx(hItem, TEXT("InstallDate"), 0, &typeSZ, (LPBYTE)s_itermName, &cbItermNameLen) ;//安装时间
377                 if (irrel== ERROR_SUCCESS) 
378                 {
379                     infos.PiInstallTime= (s_itermName);
380                 
381                 }
382                 memset(s_itermName,0,BCL_SMALL_BUFFER_LEN);
383                 cbItermNameLen =BCL_SMALL_BUFFER_LEN; 
384                 irrel=::RegQueryValueEx(hItem, TEXT("EstimatedSize"), 0, &typeDWORD, (LPBYTE)&s_EstimatedSize, &cbDWsize) ;//大小
385                 if (irrel== ERROR_SUCCESS)
386                 {
387                     infos.PiSize= s_EstimatedSize/1024;    //
388                     s_EstimatedSize=0;
389                 }
390                 cbItermNameLen = BCL_SMALL_BUFFER_LEN; 
391                 irrel=::RegQueryValueEx(hItem, TEXT("DisplayVersion"), NULL, &typeSZ, (LPBYTE)s_itermName, &cbItermNameLen);//版本
392                 if ( irrel== ERROR_SUCCESS) 
393                 {
394                     infos.PiVersion=(s_itermName);
395                 }
396                 cbItermNameLen = BCL_SMALL_BUFFER_LEN; 
397                 memset(s_itermName,0,BCL_SMALL_BUFFER_LEN);
398                 if (infos.PiName!="")
399                 {
400                     listProgramInfo.push_back(infos);
401                 }
402                 ::RegCloseKey(hItem);    // 关闭二级注册表(程序属性信息)
403             }
404             dwIndex = dwIndex + 1;            // 改变dwIndex,循环获取下移字段
405             cbkeyNamelen = BCL_SMALL_BUFFER_LEN;
406             memset(s_keyName,0,cbkeyNamelen);
407             lResult = RegEnumKeyEx(hSubKey, dwIndex, s_keyName, &cbkeyNamelen, 0, NULL, NULL, NULL);
408         }
409         RegCloseKey(hSubKey);    // 关闭一级注册表(读取程序信息)
410         return ERROR_SUCCESS;
411     }
412     RegCloseKey(hSubKey);
413     return lResult;
414 }
415 
416 BCL_API int CALLBACK BCL_RegAddTipTitleAndContent(string strTitle,string strContent)
417 {
418     long lRval=0;
419     lRval=BCL_RegSetKeyIterm(HKEY_LOCAL_MACHINE,"SoftWare\\Microsoft\\Windows\\CurrentVersion\\Policies\\System","legalnoticecaption",REG_SZ,(void*)(strTitle.c_str()),strTitle.size());
420     if (lRval!=ERROR_SUCCESS)
421     {
422         return lRval;
423     }
424     lRval=BCL_RegSetKeyIterm(HKEY_LOCAL_MACHINE,"SoftWare\\Microsoft\\Windows\\CurrentVersion\\Policies\\System","legalnoticetext",REG_SZ,(void*)(strContent.c_str()),strContent.size());
425     return lRval;
426 }
427 BCL_API int CALLBACK BCL_RegRemoveTipTitleAndContent()
428 {
429     long lRval=0;
430     lRval=BCL_RegDeleteKeyIterm(HKEY_LOCAL_MACHINE,"SoftWare\\Microsoft\\Windows\\CurrentVersion\\Policies\\System","legalnoticecaption");
431     if (lRval!=ERROR_SUCCESS)
432     {
433         return lRval;
434     }
435     lRval=BCL_RegDeleteKeyIterm(HKEY_LOCAL_MACHINE,"SoftWare\\Microsoft\\Windows\\CurrentVersion\\Policies\\System","legalnoticetext");
436     return lRval;
437 }
BCL_Regedit.cpp

1.注册表的作用

注册表是windows系统的数据库,保存了系统、用户、程序的信息,通过修改注册表可以对系统进行设置,可以获取程序信息、用户信息等,可以修改环境变量,可以设置程序开机自启动,修改主页,取消运行、重启、关机、注销按钮等。只要你知道注册表中对应键和键值的功能了解,就可以通过修改键值来重新对系统进行设置或修改。修改注册表是直接对系统的核心数据库进行修改,具有很强大的功能,不能乱用,如果注册表受到了破坏,轻则使windows的启动过程出现异常,重则可能会导致整个windows系统的完全瘫痪。因此正确地认识、使用,特别是及时备份以及有问题恢复注册表对windows用户来说就显得非常重要。

2.注册表的结构

注册表的组织方式跟文件目录比较相似,主要分为根键、子键和键值项三部分,与文件目录对应的话就是根目录、子目录和文件。

2.1根键

注册表由五个根键构成,每个根键保存的信息如下所示。

(1)HKEY_CLASSES_ROOT

保存着所有的文件和打开程序对应关系。

(2)HKEY_CURRENT_CONFIG

保存着对硬件的配置,包括了系统中现有的所有配置文件的细节。

(3)HKEY_USERS

保存用户和目前登陆用户的信息。

(4)HKEY_LOCAL_MACHINE

包括网络和硬件上所有的软件设置。

(5)HKEY_CURRENT_USER

任何在HKEY_CURRENT_USER里的改动也都会立即HKEY_USERS改动。所有当前的操作改变只是针对当前用户而改变,并不影响其他用户。

2.2子键

可以有多个子键和键值项,就像一个目录中可以有多个子目录和多个文件一样。
2.3键值项

可以理解为文件,它由三部分组成,分别为:名称、类型、数据。

键值类型又分为多种主要包括如下:
REG_BINARY 二进制数据
REG_DWORD 32位双字节数据
REG_SZ 以0结尾的字符串
REG_DWORD_BIG_ENDIAN 高位排在底位的双字
REG_EXPAND_SZ 扩展字符串,可以加入变量如%PATH%
REG_LINK UNICODE 符号链接
REG_RESOURCE_LIST 设备驱动程序资源列表
REG_MULTI_SZ 多字符串
注册表数据项的数据类型有8种,但最常用的主要是前3种。

知道注册表的键和键值的功能就可以通过修改注册表来设置系统参数,或者获取信息。

3.对注册表的操作

注册表键的操作包括打开、关闭、新建、删除、导入、导出、获取信息、枚举键和键值等,注册表对键值的操作有设置值、获取值、添加、删除。

下面对键和键值的操作做了封装,将注册表的操作统一成一种风格,主要包括根键hRootKey、子键路径strKeyPath包括子键名称,如果strKeyPath为空,则hRootKey表示打开的子键函数是对改键的子键或键值进行操作。可以很方便的实现键和键值的增删改查。HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

自己编了一个股票监控软件,有如下功能,有兴趣的朋友可以下载;

(1)   个股监测。监测个股实时变化,可以监测个股大单交易、急速拉升和下降、主力入场和出场、股票最高点和最低点提醒。检测到最高点、最低点、主力进场点、主力退场点、急速拉升点、急速下跌点,给出语音或者声音提醒,不用再时刻看着大盘了,给你更多自由的时间;

(2)   大盘监测。监测大盘的走势,采用上证、深证、创业三大指数的综合指数作为大盘走势。并实时监测大盘的最高点和最低点、中间的转折点。

(3)   股票推荐。还能根据历史数据长期或短期走势进行分析,对股市3千多个股票进行分析对比,选出涨势良好的股票,按照增长速度从大到小排序,推荐给你涨势良好的股票;

下载地址:

1.0.3版本(修复大盘指数崩溃缺陷)下载地址:

链接:https://pan.baidu.com/s/1BJcTp-kdniM7VE9K5Kd3vg 提取码:003h

更新链接:

https://www.cnblogs.com/bclshuai/p/10621613.html

猜你喜欢

转载自www.cnblogs.com/bclshuai/p/6215971.html