Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370019
  • 博文数量: 132
  • 博客积分: 3066
  • 博客等级: 中校
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-14 16:19
文章分类

全部博文(132)

文章存档

2012年(1)

2010年(50)

2009年(81)

我的朋友

分类: C/C++

2010-05-24 13:52:43

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

         // 获取注册表中整数值
         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();

阅读(1151) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~