MFC对注册表的操作
2013-08-05 20:40
471 查看
今天刚学了对注册表的读写操作。在这里总结一下。
主要用到的函数有:
2.接着调用RegSetValueEx()函数,代码如下:
主要用到的函数有:
LONG WINAPI RegCreateKey(//Creates the specified registry key. If the key already exists in the registry, the function opens it. __in HKEY hKey,//创建一个指定的注册表项,如果这个表项已经存在,则打开它 __in_opt LPCTSTR lpSubKey, __out PHKEY phkResult ); LONG WINAPI RegSetValue(//Sets the data for the default or unnamed value of a specified registry key. The data must be a text string. __in HKEY hKey,//设置一个指定的默认的或者是未命名的注册表项的值。这个值必须是文本串,也就是说只能写入文本类型的值 __in_opt LPCTSTR lpSubKey, __in DWORD dwType, __in_opt LPCTSTR lpData, __in DWORD cbData ); LONG WINAPI RegQueryValue(//Retrieves the data associated with the default or unnamed value of a specified registry __in HKEY hKey,// The data must be a null-terminated string __in_opt LPCTSTR lpSubKey,//获得一个指定的默认的或者是未命名的注册表项的值,这个值必须是文本串 __out_opt LPTSTR lpValue,//与RegSetValue相对应 __inout_opt PLONG lpcbValue );
实现:
1.首先调用RegCreateKey()函数,创建指定的注册表项,代码如下:
HKEY hKey; RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey);
参数说明:
第一个参数hKey的取值可以是下面的这些,这些值在注册表中都可看到,如下:HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS
第二个参数可以理解为在上面那些文件夹的路径,代码中"Software\\Lin\\admin",如果没有找到相应的文件夹,则新建。
第三个参数是个接收句柄,得到的句柄在下面要用到。
2.然后调用RegSetValue()函数,写入想写入的字符串值。(完成了数据的写入)代码如下:
RegSetValue(hKey, "hello", REG_SZ, "hi", strlen("hi"));
写入后:
参数说明:
第一个参数不用说了,就是上面得到的句柄。
第二个参数是上面得到的句柄的一个子项,可以理解为基于上面路径的子文件夹。比如上面的"Software\\Lin\\admin",如果想在admin下再继续创建子项(文件夹)可以继续写上子项(文件夹)的名称。如果是直接在admin中写入数据,则用
NULL来表示默认路径。
第三个参数就是要写入的字符串值了。
第四个是写入的长度。
注:在不需要用到hKey时要调用RegCloseKey(hKey);来关闭它。
3.在写入之后,就要测试读取了。调用RegQueryValue()函数即可。方法有两种。代码如下:
LONG lValue;<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">//方法一</span> char *pBuf; HKEY hKey; RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey); RegQueryValue(hKey, "hello", NULL, &lValue); pBuf = new char[lValue]; RegQueryValue(hKey, "hello", pBuf, &lValue); MessageBox(pBuf);
LONG lValue;<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">//方法二</span>
char *pBuf;
CString path = "Software\\Lin\\admin\\hello";
RegQueryValue(HKEY_LOCAL_MACHINE, path, NULL, &lValue);
pBuf = new char[lValue];
RegQueryValue(HKEY_LOCAL_MACHINE, path, pBuf, &lValue);
MessageBox(pBuf);
说明:两种方法其实基本上相同,第一种方法调用了RegOpenKey()函数,打开一个表项,用hKey接收。第二种方法直接调用RegQueryValue()读取。写第一种方法主要是为了下面拓展函数读取时的用法的一致。
RegQueryValue()函数参数说明:
第一,第二个参数跟上面所说的一样。
第三个参数是一个接收值。值得说明的是:当这个值被设置为NULL,而第四个参数不为NULL时,第四个参数将得到所要在注册表中获得的值的长度(包括结束符)
第四个参数也是一个接收值。即获得所读取数据的长度。
这样,就完成了数据表的写入与读取操作了。
另外是拓展函数:
LONG WINAPI RegSetValueEx(//Sets the data and type of a specified value under a registry key. __in HKEY hKey,//这个函数可以写入多种类型的值 __in_opt LPCTSTR lpValueName, __reserved DWORD Reserved, __in DWORD dwType, __in_opt const BYTE* lpData, __in DWORD cbData ); LONG WINAPI RegOpenKey(//Opens the specified registry key. __in HKEY hKey, __in_opt LPCTSTR lpSubKey, __out PHKEY phkResult ); LONG WINAPI RegQueryValueEx(//Retrieves the type and data for the specified value name associated with an open registry key. __in HKEY hKey,//在获得数据之前要想用RegOpenKey()函数来打开指定的注册表项 __in_opt LPCTSTR lpValueName, __reserved LPDWORD lpReserved, __out_opt LPDWORD lpType, __out_opt LPBYTE lpData, __inout_opt LPDWORD lpcbData );
拓展函数比上面的函数强大多了。不仅仅可以写入文本值,也可以写入其他类型的值。这里举个写入32位整型的例子。
实现:
1.首先也是先调用RegCreateKey()函数,创建指定的注册表项,代码如下:
HKEY hKey; RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey);
2.接着调用RegSetValueEx()函数,代码如下:
DWORD dwAge = 30; RegSetValueEx(hKey, "age", 0, REG_DWORD, (CONST BYTE*)&dwAge, sizeof(dwAge)); RegSetValueEx(hKey, "hello", 0, REG_SZ, (CONST BYTE*)"hello", strlen("helle"));
写入之后:
RegSetValueEx()参数说明:
第一个参数,和RegSetValue()函数相同。
第二个参数要注意的是,它不完全相同与RegSetValue()函数的第二个参数。上面代码中的age,hello是位于admin下被写入数据的名称,而不是admin的子项(文件夹)。
第三个参数是一个保留值,要设置为0。
第四个参数为要写入的数据类型,其取值如下图。
第五个参数为要写入的数据。由于是CONST BYTE*类型,所以要强制转换。
第六个参数为写入数据的大小。
3.读取操作。如下代码:
HKEY hKey; DWORD dwType; DWORD dwAge; DWORD dwValue; RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey); RegQueryValueEx(hKey, "age", NULL, &dwType, (LPBYTE)&dwAge, &dwValue); CString str; str.Format("%d", dwAge); MessageBox(str);
RegQueryValueEx()参数说明:
第一个参数为句柄。
第二个参数为admin项目下的记录的名称,如下图。
第三个参数是个保留值,必须设置为NULL。
第四个参数为接收值。获得读取数据的类型。
第四个参数为接收值。要强制转换为LPBYTE类型。
第五个参数为接收值。获得所读取数据的大小。
搞定。。。。
最后再总结一下数据表项和数据的删除。
这里用到两个函数:
LONG WINAPI RegDeleteKey(//Deletes a subkey and its values. Note that key names are not case sensitive. __in HKEY hKey, __in LPCTSTR lpSubKey ); LONG WINAPI RegDeleteValue(//Removes a named value from the specified registry key. Note that value names are not case sensitive. __in HKEY hKey, __in_opt LPCTSTR lpValueName );
说明:从函数的名称可以看出,一个是删除表项的,一个是删除数据名称的。比如下面代码:
HKEY hKey; RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Lin\\admin", &hKey);
RegDeleteKey(hKey, "hello");//删除的是上面第一幅图的hello项
RegDeleteValue(hKey, "hello");//删除的是admin下的hello
删除后:
这样,就把这个基本上总结完了哈。。。
相关文章推荐
- MFC对注册表的操作
- MFC的注册表操作—CRegKey类的使用
- vs2008环境下MFC对注册表的读写操作
- MFC 从注册表读取一个程序的操作路径
- MFC 操作注册表
- MFC注册表操作
- 开机启动运行的注册表操作(MFC)
- MFC 注册表操作
- MFC操作注册表
- MFC操作注册表
- 关于开机启动运行的注册表操作(MFC)
- MFC注册表操作
- MFC注册表操作
- MFC操作注册表常用函数
- MFC操作注册表获取USB磁盘代码阅读步骤总结
- MFC注册表操作详解
- C/C++/VC++/mfc对注册表的操作
- MFC 注册表操作
- MFC操作--数据输出、注册表、文件统计和获取上级目录
- 九:[VC++]MFC操作注册表