关闭

使用CRegKey进行注册表操作

标签: userquerymfc
707人阅读 评论(0) 收藏 举报
分类:

用CRegKey类来操作注册表是非常方便的。CRegKey类并不是一个MFC类,而是一个ATL类,所以在使用的时候不要忘记在StdAfx.h头文件中加入#include <atlbase.h>。

         // 获取注册表中整数值
         CRegKey rk;        
         DWORD dwValue;
         CString strTemp;
         LPCTSTR lpszKeyName = _T ("Software//Microsoft//Windows//CurrentVersion//Explorer");
        
         if (rk.Open (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
         {
                 TRACE0 ("Open Success./n");
                 if (rk.QueryValue (dwValue, _T ("Browse For Folder Height")) == ERROR_SUCCESS)
                 {
                         TRACE0 ("Query Success./n");
                         strTemp.Format ("%0X", dwValue);
                         SetDlgItemText (IDC_EDIT1, strTemp);
                 }
         }
         rk.Close();

         // 获取注册表中字符串值
         CRegKey rk;        
         DWORD dwCount = 1024;
         CString strTemp;
         TCHAR szValue [1024];
         LPCTSTR lpszKeyName = _T ("Software//Microsoft//Windows//CurrentVersion//Explorer");
        
         if (rk.Open (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
         {
                 TRACE0 ("Open Success./n");
                 if (rk.QueryValue (szValue, _T ("Logon User Name"), &dwCount) == ERROR_SUCCESS)
                 {
                         TRACE0 ("Query Success./n");
                         strTemp = szValue;
                         SetDlgItemText (IDC_EDIT1, strTemp);
                 }
         }
         rk.Close();

         // 设置注册表中字符串值
         CRegKey rk;        
         LPCTSTR lpszKeyName = _T ("Software//JueYuanLongStudio//Test");
        
         // Create能在Open失败的情况下自动尝试建立
         if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
         {
                 TRACE0 ("Open Success./n");
                 if (rk.SetValue (_T ("palagon"), _T ("Logon User Name")) == ERROR_SUCCESS)
                 {
                         TRACE0 ("SetValue Success./n");
                 }
         }
         rk.Close();

         // 设置注册表中数据值
         CRegKey rk;        
         LPCTSTR lpszKeyName = _T ("Software//JueYuanLong Studio//Test");
        
         // Create能在Open失败的情况下自动尝试建立
         if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
         {
                 TRACE0 ("Open Success./n");
                 if (rk.SetValue (110, _T ("Logon User Data")) == ERROR_SUCCESS)
                 {
                         TRACE0 ("SetValue Success./n");
                 }
         }
         rk.Close();

         // 删除注册表中键值
         CRegKey rk;        
         LPCTSTR lpszKeyName = _T ("Software//JueYuanLong Studio//Test");
        
         // Create能在Open失败的情况下自动尝试建立
         if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
         {
                 TRACE0 ("Open Success./n");
                 if (rk.DeleteValue (_T ("Logon User Data")) == ERROR_SUCCESS)
                 {
                         TRACE0 ("DeleteValue Success./n");
                 }
         }
         rk.Close();

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:70345次
    • 积分:1379
    • 等级:
    • 排名:千里之外
    • 原创:61篇
    • 转载:10篇
    • 译文:0篇
    • 评论:22条
    最新评论