现在的位置: 首页 > 综合 > 正文

环境变量设置(reg)

2018年02月08日 ⁄ 综合 ⁄ 共 4814字 ⁄ 字号 评论关闭

功能:将指定关键字的数据置于最首。

#pragma once

#include <afx.h>

enum REG_RESULT
{
	REG_ERROR = 0,
	REG_SUCCESS,
};

const int MAX_LEN		= 4048;
const int REG_MAX_LEN	= 4048;
#define REG_ROOT_KEY	HKEY_LOCAL_MACHINE

const char REG_KEY_NAME[]		= "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
const char STR_KEY_NAME[]		= "path";
const char MARVELL_KEY_WD[]		= "marvell";
const char LEADCORE_KEY_WD[]	= "leadcore";
enum ENV_SWITCH
{
	ENV_MARVEL = 0,
	ENV_LEADCORE,
};


REG_RESULT SetValue_S_A( HKEY ReRootKey,HKEY &hKey,const char *ReSubKey,const char *ReValueName, BYTE *ReSetContent_S,char * sLog = NULL);
REG_RESULT GetValue_S_A ( HKEY ReRootKey,HKEY &hKey,const char *cSubKey,const char *cValueName,char *cResult,char *sLog = NULL);

REG_RESULT SetEnvValue(const char* pName,BYTE *cResult,char *sLog = NULL);
REG_RESULT GetEnvValue(const char* pName,char *cResult,char *sLog = NULL);

REG_RESULT SetEnv(BYTE *cResult,char *sLog = NULL);
REG_RESULT GetEnv(char *cResult,char *sLog = NULL);

bool SplitStrToGetData(CStringArray & arrData, CString SrcString);
bool SpliteValue(char *szValue);
bool CombineStr(CStringArray & arrData, CString &SrcString);
int GetKeyList(const char *KeyWord);

bool ChangeValue(char *szValue,ENV_SWITCH env);

bool SwitchEnv(ENV_SWITCH env);

#include "StdAfx.h"
#include "EnvReg.h"

HKEY g_hKey;
CStringArray g_arrbuf;

REG_RESULT SetValue_S_A( HKEY ReRootKey,HKEY &hKey,const char *ReSubKey,const char *ReValueName, BYTE *ReSetContent_S,char * sLog)
{
	REG_RESULT res=REG_SUCCESS;
	if(RegOpenKeyExA(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
	{
		if(RegSetValueExA(hKey,ReValueName,NULL,REG_SZ,ReSetContent_S,CStringA(ReSetContent_S).GetLength())!=ERROR_SUCCESS)
		{
			if(sLog != NULL)
			{
				sprintf(sLog,"错误:无法设置有关的注册表信息\n");
				res=REG_ERROR;
			}
		}
		RegCloseKey(hKey);
	}
	else
	{
		if(sLog != NULL)
		{
			sprintf(sLog,"错误:无法查询有关的注册表信息\n");
			res=REG_ERROR;
		}
	}
	return res;
}


/*********************
* Name:	GetValue_S
* Func:	查看键值的函数
* Input:HKEY 路径项			hKey当前项		cSubKey项路径	cValueName获取的键名	cResult获取的值	
* Out:	1 TRUE 0 FALSE		sLog 反馈字符串(默认NULL)
**********************/
REG_RESULT GetValue_S_A ( HKEY ReRootKey,HKEY &hKey,const char *cSubKey,const char *cValueName,char *cResult,char *sLog)
{
	ASSERT(cSubKey!=NULL);
	ASSERT(cValueName!=NULL);
	ASSERT(cResult!=NULL);

	REG_RESULT res=REG_SUCCESS; //操作结果:0==succeed
	DWORD dwLen  = REG_MAX_LEN;
	if(RegOpenKeyExA(ReRootKey,cSubKey,0,KEY_READ,&hKey)==ERROR_SUCCESS)
	{
		if(RegQueryValueExA(hKey,cValueName,0,NULL,(LPBYTE)cResult,&dwLen) != ERROR_SUCCESS)
		{
			if(sLog != NULL)
				sprintf(sLog,"错误:无法查询有关的注册表信息\n");
			res=REG_ERROR;
		}
		RegCloseKey(hKey);
	}
	else
	{
		if(sLog != NULL)
			sprintf(sLog,"错误:无法打开有关的hKEY\n");
		res=REG_ERROR;
	}
	return res;
}

//确定主键 
REG_RESULT SetEnvValue(const char* pName,BYTE *cResult,char *sLog)
{
	return SetValue_S_A(REG_ROOT_KEY,g_hKey,REG_KEY_NAME,pName,cResult,sLog);
}

REG_RESULT GetEnvValue(const char* pName,char *cResult,char *sLog)
{
	return GetValue_S_A(REG_ROOT_KEY,g_hKey,REG_KEY_NAME,pName,cResult,sLog);
}

//添加的操作
REG_RESULT SetEnv(BYTE *cResult,char *sLog)
{
	return SetEnvValue(STR_KEY_NAME,cResult,sLog);
}

REG_RESULT GetEnv(char *cResult,char *sLog)
{
	return GetEnvValue(STR_KEY_NAME,cResult,sLog);
}

bool SplitStrToGetData(CStringArray & arrData, CString SrcString)
{
	CString arr_temp;						//缓存拆分的需要补发
	int nRes=0;
	arrData.RemoveAll();
	while(1)
	{
		nRes=SrcString.Find(";");
		if(nRes==-1) break;					//没有找到
		arr_temp=SrcString.Left(nRes);		//提取数据
		SrcString=SrcString.Right(SrcString.GetLength()-nRes-1);	//抛弃已获取数据 去掉空格
		arrData.Add(arr_temp);
	}
	if(SrcString.GetLength()>0)
	{
		arrData.Add(SrcString);		//最后一项追击
	}

	if(arrData.GetCount() == 0)
		return false;
	else
		return true;
}


bool SpliteValue(char *szValue)
{
	return SplitStrToGetData(g_arrbuf,szValue);
}

bool CombineStr(CStringArray & arrData, CString &SrcString)
{
	int nCount = arrData.GetCount();
	SrcString.Empty();

	for(int i=0;i<nCount;i++)
	{
		SrcString= SrcString + arrData.GetAt(i) + ";";
	}
	return true;
}

int GetKeyList(const char *KeyWord)
{
	int nCount = g_arrbuf.GetCount();
	int nkey = -1;
	for(int i=0;i<nCount;i++)
	{
		if(g_arrbuf.GetAt(i).Find(KeyWord) != -1)
		{
			nkey = i;
			break;
		}
	}
	return nkey;
}

bool ChangeValue(char *szValue,ENV_SWITCH env)
{
	CString strMarvel,strLeadcore;
	int nResult = -1;
	CString strBuf;
	
	if( !SpliteValue(szValue))
		return false;

	strBuf.Empty();
	switch(env)
	{
	case ENV_MARVEL:
		{
			nResult = GetKeyList(MARVELL_KEY_WD);
			if(nResult == -1)
				return false;
			strBuf = g_arrbuf.GetAt(nResult);
			g_arrbuf.RemoveAt(nResult);
			g_arrbuf.InsertAt(0,strBuf);
			break;
		}
	case ENV_LEADCORE:
		{
			nResult = GetKeyList(LEADCORE_KEY_WD);
			if(nResult == -1)
				return false;
			strBuf = g_arrbuf.GetAt(nResult);
			g_arrbuf.RemoveAt(nResult);
			g_arrbuf.InsertAt(0,strBuf);
			break;
		}
	default:
		return false;;
	}

	CombineStr(g_arrbuf,strBuf);
	sprintf(szValue,"%s",strBuf);

	
	if(SetEnv((BYTE*)szValue,NULL) == REG_ERROR)
		return false;
	else
		return true;
}

bool SwitchEnv(ENV_SWITCH env)
{
	char szResult[REG_MAX_LEN] = {0};
	char szLog[256] = {0};

	if(GetEnv(szResult,szLog) == REG_ERROR)
		return false;
	else
	{
		return ChangeValue(szResult,env);
	}
}

测试:

#include "EnvReg.h"


int _tmain(int argc, _TCHAR* argv[])
{
	if(argc != 2)
	{
		printf("命令格式:工具名 参数\n参数说明:\n0:设置含marvell关键字为最首\n1:设置含leadcore关键字的为最首\n");
		system("pause");
		return 0;
	}
	int nResult = atoi(argv[1]);

	if(SwitchEnv((ENV_SWITCH)nResult))
		printf("SwitchEnv success");
	else
		printf("SwitchEnv false");

	return 0;
}

抱歉!评论已关闭.