hbyh的专栏

安全原则: 3重保护 周1维护 实时检测 立刻提醒

使用CRegKey进行注册表操作

用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();

 
阅读更多
文章标签: user query mfc
个人分类: ATL编程
上一篇CString GetBuffer()
下一篇DLL 添加MFC 支持
想对作者说点什么? 我来说一句

使用CRegKey操作注册表

2012年11月29日 64KB 下载

没有更多推荐了,返回首页

关闭
关闭