VC++ 读写注冊表,注冊文件图标关联

#include <string>
#include <iostream>
#include <Windows.h>
#include <shlobj.h>
using namespace std;

//-------------------------------------------------------------------------
// 注冊文件图标关联
//-------------------------------------------------------------------------
// @strExt				[in]: 须要检測的文件后缀(.txt)
// @strAppKey			[in]: 扩展名在注冊表中的键值(txtfile)
// @strAppName			[in]: 须要关联的引用程序(c:\app\app.exe)
// @strDefaultIcon		[in]: 关联的图标
// @strDescribe			[in]: 描写叙述
//-------------------------------------------------------------------------
void RegisterFileRelation(char *strExt,char *strAppKey,char *strAppName, char *strDefaultIcon, char *strDescribe)
{
	char strTemp[_MAX_PATH];
	HKEY hKey;

	RegCreateKey(HKEY_CLASSES_ROOT,strExt,&hKey);
	RegSetValue(hKey,"",REG_SZ,strAppKey,strlen(strAppKey)+1);
	RegCloseKey(hKey);

	RegCreateKey(HKEY_CLASSES_ROOT,strAppKey,&hKey);
	RegSetValue(hKey,"",REG_SZ,strDescribe,strlen(strDescribe)+1);
	RegCloseKey(hKey);

	sprintf(strTemp,"%s\\DefaultIcon",strAppKey);
	RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
	RegSetValue(hKey,"",REG_SZ,strDefaultIcon,strlen(strDefaultIcon)+1);
	RegCloseKey(hKey);

	sprintf(strTemp,"%s\\Shell",strAppKey);
	RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
	RegSetValue(hKey,"",REG_SZ,"Open",strlen("Open")+1);
	RegCloseKey(hKey);

	sprintf(strTemp,"%s\\Shell\\Open\\Command",strAppKey);
	RegCreateKey(HKEY_CLASSES_ROOT,strTemp,&hKey);
	sprintf(strTemp,"%s \"%%1\"",strAppName);
	RegSetValue(hKey,"",REG_SZ,strTemp,strlen(strTemp)+1);
	RegCloseKey(hKey);
	SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_IDLIST,NULL,NULL);
}

//-------------------------------------------------------------------------
// 写注冊表的的方法
//-------------------------------------------------------------------------
// @root			[in]: 注冊表根
// @subDir			[in]: 子结构
// @regKey			[in]: 注冊表项键
// @regValue		[in]: 要写入的注冊表值
//-------------------------------------------------------------------------
// @return			    : 0表示成功,其它表示错误码
//-------------------------------------------------------------------------
int WriteRegKey(HKEY root,char * subDir,char * regKey,char * regValue){
	char strTemp[_MAX_PATH];
	HKEY hKey;
	sprintf(strTemp,subDir);

	char* sclass = "";
	DWORD nbf = 0;
	long ret = RegCreateKeyEx(root,strTemp,0,"",REG_OPTION_NON_VOLATILE,KEY_READ|KEY_WRITE,NULL,&hKey,&nbf);
	if(ret != ERROR_SUCCESS) {
		return -1;
	}
	
	ret = RegSetValueEx(hKey,regKey,0,REG_SZ,LPBYTE(regValue),strlen(regValue)+1);
	if(ret != ERROR_SUCCESS) {
		return -1;
	}

	return 0;
}

//-------------------------------------------------------------------------
// 写注冊表的的方法
//-------------------------------------------------------------------------
// @root			[in] : 注冊表根
// @subDir			[in] : 子结构
// @regKey			[in] : 注冊表项键
// @regValue		[out]: 读的的注冊表值
//-------------------------------------------------------------------------
// @return				 : 0表示成功,其它表示错误码
//-------------------------------------------------------------------------
int ReadRegKey(HKEY root,char * subDir,char * regKey,char* & regValue){
	char strTemp[_MAX_PATH];
	HKEY hKey;
	sprintf(strTemp,subDir);

	long ret = (::RegOpenKeyExA(root,strTemp,0,KEY_READ,&hKey));
	if (ret !=ERROR_SUCCESS)
	{
		return -1;
	}

	DWORD valueType = REG_SZ;
	DWORD cbData = 255;

	ret = ::RegQueryValueExA(hKey,regKey,NULL,&valueType,(LPBYTE)regValue,&cbData);
	if (ret != ERROR_SUCCESS)
	{
		return -1;
	}
	RegCloseKey(hKey);

	return 0;
}

int main(){
	WriteRegKey(HKEY_CURRENT_USER,"Software\\Demo 1.0\\AppKey","path","C:\\hello\\world");

	char* ret;
	ret = new char[255];
	ret[0] = '\0';

	ReadRegKey(HKEY_CURRENT_USER,"Software\\Demo 1.0\\AppKey","path",ret);
	cout << ret << endl;
	delete[] ret;

	return 0;
}

posted on 2017-04-26 10:09  blfbuaa  阅读(129)  评论(0编辑  收藏  举报