VC++ 中对注册表的操作

这里只是对注册表的几个api函数的使用简介。在MFC中,你可以使用CRegKey类操作注册表,关于CRegKey类在这里就不做介绍了,可以参考msdn。

以下代码实现了在HKEY_LOCAL_MACHINE的SOFTWARE下打开MyProgram键,如果MyProgram不存在,则创建之,存在则进行查询

 HKEY hKEY;
 HKEY hKEYResult;
 LPCTSTR lpszSubKey;
 LPCTSTR lpszValueName;
 LONG ret;
 DWORD dwType;
 DWORD dwSize;
 
 hKEY = HKEY_LOCAL_MACHINE;
 lpszSubKey = _T("SOFTWARE//MyProgram");

 ret = RegOpenKeyEx(
               hKEY,                                      // 根键
               lpszSubKey,                           // 子键
               0,                                             // 必须为0
               KEY_ALL_ACCESS,            // 访问权限
               &hKEYResult);                     // 保存打开的键的句柄
           
 if (ret != ERROR_SUCCESS)       // 若打开失败,则进行创建
 {
     ret = RegCreateKeyEx(
                   hKEY,                                                   // 根键
                   lpszSubKey,                                       // 子键
                   0,                                                          // 必须为0
                   NULL,                                                  // 键的class
                   REG_OPTION_NON_VOLATILE,  // 永久保持
                   KEY_READ|KEY_WRITE,               // 访问权限
                   NULL,                                                 // 安全属性
                   &hKEYResult,                                   // 保存打开的键的句柄
                   NULL); 
    if (ret != ERROR_SUCCESS)
    {
        MessageBox(_T("Create failed!"));
        RegCloseKey(hKEYResult);        // 使用完记得关闭句柄
        return;
      }
      else
      {
          lpszValueName = _T("Number");
          DWORD dwNumber = 10;  
          ret = RegSetValueEx(
                        hKEYResult,                                // 打开的句柄
                        lpszValueName,                         // 键名
                        0,                                                   // 必须为0
                        REG_DWORD,                           // 键类型
                        (CONST BYTE*)&dwNumber, // 需存储的数据
                        sizeof(DWORD));                       // 数据长度

          lpszValueName = _T("Date");
          LPCTSTR lpszDate = _T("2006/4/29");
          ret = RegSetValueEx(
                        hKEYResult,
                        lpszValueName,
                        0,
                        REG_SZ,
                        (CONST BYTE*)lpszDate,
                        MAX_PATH);
                        RegCloseKey(hKEYResult);
      }
  }
  else // 如打开成功,则进行查询
  {
      lpszValueName = _T("Number");
      dwType = REG_DWORD;
      DWORD dwRes = 0;
      dwSize = sizeof(DWORD);
      ret = RegQueryValueEx(
                    hKEYResult,               // 打开的句柄
                    lpszValueName,        // 查找的键名
                    0,                                  // 必须为0
                    &dwType,                    // 键类型
                    (BYTE*)&dwRes,      // 返回的数据
                    &dwSize);                   // 数据长度
      if (ret != ERROR_SUCCESS)
      {
          MessageBox(_T("Create failed!"));
          RegCloseKey(hKEYResult);
          return;
      }
     
      lpszValueName = _T("Date");
      dwType = REG_SZ;
      dwSize = MAX_PATH;
      BYTE *pByteSubKey = new BYTE[MAX_PATH];
      ZeroMemory(pByteSubKey, MAX_PATH);
      ret = RegQueryValueEx(
                    hKEYResult,
                    lpszValueName,
                    0,
                    &dwType,
                    pByteSubKey,
                    &dwSize);
      RegCloseKey(hKEYResult);
      delete []pByteSubKey;
      pByteSubKey = NULL;
      if (ret != ERROR_SUCCESS)
      {
          MessageBox(_T("Create failed!"));
          RegCloseKey(hKEYResult);
          return;
      }
  }

你可能感兴趣的:(Date,null,mfc,Path,vc++,byte)