vs2013 MFC自己封装的CMyKey类

从vc6.0转到vs2013,原先都是ANSI字符编码,现在vs默认的是Unicode编码。MessageBox("test");都要改成MessageBox(_T("test"));很麻烦!其他方面就不说了。

看到推酷的一篇文章 http://www.tuicool.com/articles/IbEZBr 《对多字节字符编码的支持》是从http://blog.csdn.net/xinzhiyounizhiyouni/article/details/20617981转的。

这句是重点:使vs2013支持多字节字符,这个就需要下载和安装一个文件 http://pan.baidu.com/s/1Acu6M。然后:在项目属性中,把字符集改成多字节字符集。

搞定编码了,接下来就是介绍CMyKey类。

// MyKey.h: interface for the CMyKey class.
//Author:阮家友
//时  间:
//QQ:1439120442
//我是个技术宅,欢迎交流

#pragma once

class CMyKey  
{
public:
	CMyKey();
	virtual ~CMyKey();
public:
    //修改项 不存在则新建
    BOOL WriteK(HKEY RootKey /*根键*/, CString path /*路径*/, CString strName /*字符串名称*/, CString str /*要写入的字符串*/);
    BOOL WriteK(HKEY RootKey /*根键*/, CString path /*路径*/, CString ValueName /*数值名称*/, DWORD value /*数值型*/);
    //获取值
    BOOL GetK(HKEY RootKey /*根键*/, CString path /*路径*/, CString strName /*字符串名称*/, char *pStr /*要保存的字符串对象地址*/);
    BOOL GetK(HKEY RootKey /*根键*/, CString path /*路径*/, CString strName /*数值名称*/, DWORD &pValue /*要保存的数值变量地址*/);
    //删除项
    BOOL DelK(HKEY RootKey /*根键*/, CString path /*路径*/, CString strName /*子项名称*/);
};

// MyKey.cpp: implementation of the CMyKey class.
//Author:阮家友
//时  间:2015年9月24日13:07:40
//    QQ:1439120442
//我是个技术宅,欢迎交流

#include "stdafx.h"
#include "MyKey.h"

CMyKey::CMyKey()
{

}

CMyKey::~CMyKey()
{

}
//创建项
//返回1代表成功;返回0代表失败
BOOL CMyKey::WriteK(HKEY RootKey /*根键*/, CString path /*路径*/,CString strName /*字符串名称*/, CString str /*要写入的字符串*/)
{
    HKEY newkey;
    if(ERROR_SUCCESS==RegOpenKey(RootKey,path,&newkey))
    {
        char *pData=NULL,strArr[MAX_PATH];
        pData=(LPSTR)(LPCSTR)str;
        strcpy_s(strArr,pData);
        //新建字符串项
        if(ERROR_SUCCESS==RegSetValueEx(newkey,strName,NULL,REG_SZ,(unsigned char*)strArr,sizeof(strArr)))
        {
			RegCloseKey(newkey);
			return true;
		}
		else
		{
			RegCloseKey(newkey);
			return false;
		}
    }
    else
    {
        return false;
    }
}
BOOL CMyKey::WriteK(HKEY RootKey /*根键*/, CString path /*位置*/, CString ValueName /*数值名称*/, DWORD value /*数值型*/)
{
    HKEY newkey;
    if(ERROR_SUCCESS==RegOpenKey(RootKey,path,&newkey))
    {
        //新建数值项
        if(ERROR_SUCCESS==RegSetValueEx(newkey,ValueName,NULL,REG_DWORD,(CONST BYTE*)&value,4))
		{
			RegCloseKey(newkey);
			return true;
		}
		else
		{
			RegCloseKey(newkey);
			return false;
		}
        
    }
    else
    {
        return false;
    }  
}
//获取字符串
BOOL CMyKey::GetK(HKEY RootKey /*根键*/, CString path /*路径*/, CString strName /*字符串名称*/, char *pStr /*要保存的字符串对象地址*/)
{
	HKEY newkey;
	if(ERROR_SUCCESS==RegOpenKey(RootKey,path,&newkey))
	{
		DWORD keyType,len;
		if(ERROR_SUCCESS==RegQueryValueEx(newkey,strName,NULL,&keyType,(unsigned char *)pStr,&len))
		{
			RegCloseKey(newkey);
			return true;
		}
		RegCloseKey(newkey);
	}
    //打开失败
    return false;
}
//获取数值
BOOL CMyKey::GetK(HKEY RootKey /*根键*/, CString path /*路径*/, CString strName /*数值名称*/, DWORD &pValue /*要保存的数值变量地址*/)
{
	HKEY newkey;
    if(ERROR_SUCCESS==RegOpenKey(RootKey,path,&newkey))
    {
        //成功打开键
		
        DWORD dwType,dwValue,len;
		//关键问题出在下一句 竟然使得 OnCreate返回了
		if(ERROR_SUCCESS==RegQueryValueEx(newkey,strName,0,&dwType,(unsigned char *)(LPBYTE)&dwValue,&len))
		{
			pValue=dwValue;
			RegCloseKey(RootKey);
			return true;
		}
		RegCloseKey(RootKey);
    }
	return false;
}
//删除项
BOOL CMyKey::DelK(HKEY RootKey /*根键*/, CString path /*位置*/,CString keyname/*名称*/)
{
	HKEY newkey;
    if(ERROR_SUCCESS==RegOpenKey(RootKey,path,&newkey))
    {
        if(ERROR_SUCCESS==RegDeleteValue(newkey,keyname))
        {
			RegCloseKey(RootKey);
            return true;
        }
    }
    return false;
}

测试结果:
vs2013 MFC自己封装的CMyKey类_第1张图片

测试代码(都是放在按钮的单击事件中的):

void CRegister测试View::OnTest()
{
	//MessageBox("show?");

	
	//字符串型 获取
	char str[MAX_PATH] = { '\0' };
	BOOL result = key1.GetK(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\TypedPaths", "url1", str);
	if (result){
		MessageBox(str);//C:\Users\sophsis\AppData\Roaming\yxj.exe
	}
	else{
		MessageBox("error!");
	}
	

	/*
	//数值型 获取
	DWORD value = 20;
	CString str;
	if (key1.GetK(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartPage2", "FavoritesVersion", value))
	{
		str.Format("value=%d", value);//value=2
		MessageBox(str);
	}
	else
	{
		MessageBox("error");//失败则经过这里
	}
	*/

	//修改字符串型
	//key1.WriteK(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\TypedPaths","url1","test");
	//修改整数型
	//key1.WriteK(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartPage2","FavoritesVersion",3);
	//删除
	//key1.DelK(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\TypedPaths","url1");
	//key1.DelK(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartPage2","FavoritesVersion");
}




你可能感兴趣的:(mfc,注册表,VS2013,HKEY)