注册表 读写 操作 我自己编写的类

 

xxx.h头文件

#pragma once
#include   <winreg.h>

class CRegedit
{
public:
 LONG   Read(LPCTSTR   pszKey   ,   BYTE   *pData   ,   DWORD   &   dwLength);
 LONG   Read(LPCTSTR   pszKey,   CString sVal);
 LONG   Read(LPCTSTR   pszKey   ,   DWORD   &   dwVal);
 LONG   Write(LPCTSTR   pszKey   ,   const   BYTE   *pData   ,   DWORD   dwLength);
 LONG   Write(LPCTSTR   pszKey   ,   LPCTSTR   pszVal);
 LONG   Write(LPCTSTR   pszKey   ,   DWORD   dwVal);
 void   Close();
 LONG   Open(HKEY   hKeyRoot   ,   LPCTSTR   pszPath   ,   REGSAM   dwAccess   =   KEY_ALL_ACCESS);

 CRegedit(void);
 ~CRegedit(void);
protected:
 CString   m_sPath;
 HKEY   m_hKey;
};

 

 

 

 

 

//源文件

#include "StdAfx.h"
#include "Regedit.h"


CRegedit::CRegedit(void)
{
 m_hKey   =   NULL;
}

CRegedit::~CRegedit(void)
{
 Close();
}

LONG   CRegedit::Open(HKEY hKeyRoot,LPCTSTR pszPath,REGSAM dwAccess)
{
 DWORD   dw;
 m_sPath   =   pszPath;

 return   RegCreateKeyEx(hKeyRoot,pszPath,0L,NULL,REG_OPTION_NON_VOLATILE,dwAccess,NULL,&m_hKey,&dw);
}

void   CRegedit::Close()
{
 if(m_hKey)
 {
  RegCloseKey(m_hKey);
  m_hKey   =   NULL;
 }
}

LONG   CRegedit::Write(LPCTSTR   pszKey,   DWORD   dwVal)
{
 ASSERT(m_hKey);
 ASSERT(pszKey);

 return   RegSetValueEx(m_hKey,pszKey,0L,REG_DWORD,(CONST BYTE *)&dwVal,sizeof(DWORD));
}

LONG   CRegedit::Write(LPCTSTR   pszKey,   LPCTSTR   pszVal)
{
 ASSERT(m_hKey);
 ASSERT(pszKey);
 ASSERT(pszVal);
 ASSERT(AfxIsValidAddress(pszVal,strlen(pszVal),FALSE));

 return   RegSetValueEx(m_hKey,pszKey,0L,REG_SZ,(CONST BYTE*)pszVal,CString(pszVal).GetLength()+1);
}

LONG   CRegedit::Write(LPCTSTR   pszKey,   const   BYTE   *pData,   DWORD   dwLength)
{
 ASSERT(m_hKey);
 ASSERT(pszKey);
 ASSERT(pData   &&   dwLength> 0);
 ASSERT(AfxIsValidAddress(pData   ,   dwLength   ,   FALSE));

 return   RegSetValueEx(m_hKey   ,   pszKey   ,   0L   ,   REG_BINARY   ,   pData   ,   dwLength);
}

LONG   CRegedit::Read(LPCTSTR   pszKey,   DWORD   &dwVal)
{
 ASSERT(m_hKey);
 ASSERT(pszKey);

 DWORD   dwType;
 DWORD   dwSize   =   sizeof(DWORD);
 DWORD   dwDest;

 LONG   lRet   =   RegQueryValueEx(m_hKey   ,   pszKey   ,   NULL   ,   &dwType   ,   (LPBYTE)   &dwDest   ,   &dwSize);

 if(lRet   ==   ERROR_SUCCESS)
  dwVal   =   dwDest;

 return   lRet;
}

LONG   CRegedit::Read(LPCTSTR pszKey,CString sVal)
{
 ASSERT(m_hKey);
 ASSERT(pszKey);

 DWORD   dwType;
 DWORD   dwSize   =   512;
 // char   string[512] ;
 LPBYTE   string;

 LONG   lRet   =   RegQueryValueEx(m_hKey,pszKey,NULL,&dwType,string,   &dwSize);

 if(lRet   ==   ERROR_SUCCESS)
 {
  sVal = string;
 }

 return   lRet;
}

LONG   CRegedit::Read(LPCTSTR   pszKey,   BYTE   *pData,   DWORD   &dwLength)
{
 ASSERT(m_hKey);
 ASSERT(pszKey);

 DWORD   dwType;

 return   RegQueryValueEx(m_hKey,pszKey,NULL,&dwType,pData,&dwLength);
}

 

//======================================

 

//使用举例:

void Cdemo_print_win32_cDlg::OnBnClickedButton5()
{
 // TODO: Add your control notification handler code here
 CRegedit reg;
 CString   str = NULL;
// HKEY hkey;
 //RegCreateKeyEx(L"HKEY_LOCAL_MACHINE",L"HARDWARE\\DEVICEMAP\\SERIALCOMM",0L,NULL,REG_OPTION_NON_VOLATILE,KEY_READ| KEY_WRITE,)

 LPBYTE aaa = (LPBYTE)"sdfasdfsdf";
 reg.Open(HKEY_LOCAL_MACHINE,L"HARDWARE\\DEVICEMAP\\SERIALCOMM",KEY_READ|KEY_WRITE);
   if(reg.Read(_T("\\Device\\VCom15"),str)!=ERROR_SUCCESS)
    MessageBox(L"READ failed");
  else
  MessageBox(str,0,0);

//    if(reg.Write(L"NewItem1",L"B")!=ERROR_SUCCESS)
//     MessageBox(L"Write failed");

 reg.Close();

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值