设置PATH环境变量,注意缓冲区指针的类型
lpSetValue = (LPBYTE)TEXT(lpszSet);
dwCount = strlen(lpszSet) + 1;
dwType = REG_SZ;
if(ERROR_SUCCESS == RegSetValueEx(hkey, TEXT("PATH"), NULL, dwType, lpSetValue, dwCount)){
fModified = TRUE;
}
注意:直接lpSetValue = (LPBYTE)lpszSet时%xxx%变量将不会被自动替换!
修改之后,通知所有应用环境变量已经改变
SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)"Environment",SMTO_ABORTIFHUNG,500,&dwDisposition);
函数getenv不好用,总是返回win系统启动时的环境;可能GetEnvironmentVariable要好用些,需要继续测试才知道...
---------------------------------------------------
以下的使用CRegKey的实现
---------------------------------------------------
// EnvManager.h: interface for the CEnvManager class.
//
//
#if !defined(AFX_ENVMANAGER_H__C4B551D3_3E8C_43F7_BF77_E76288246BC7__INCLUDED_)
#define AFX_ENVMANAGER_H__C4B551D3_3E8C_43F7_BF77_E76288246BC7__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <string>
using namespace std;
class CEnvManager
{
public:
CEnvManager();
virtual ~CEnvManager();
public:
VOID SetPrjName(LPSTR name);
VOID SetWorkPath(LPSTR lpszCmdLine);
BOOL verify(LPSTR lpszCmdLine = NULL);
void Delete();
void SetInt();
void SetStr();
void GetStr();
void GetInt();
private:
string errmsg;
string workpath;
string prjname;
};
#endif // !defined(AFX_ENVMANAGER_H__C4B551D3_3E8C_43F7_BF77_E76288246BC7__INCLUDED_)
// EnvManager.cpp: implementation of the CEnvManager class.
//
//
#include "StdAfx.h"
#include <atlbase.h>
#include "sdk_env.h"
#include "sdk_macro.h"
//
// Construction/Destruction
//
CEnvManager::CEnvManager()
{
}
CEnvManager::~CEnvManager()
{
}
VOID CEnvManager::SetWorkPath(LPSTR lpszCmdLine)
{
workpath = lpszCmdLine;
}
VOID CEnvManager::SetPrjName(LPSTR name)
{
prjname = name;
}
// 用CRegKey类来操作注册表是非常方便的。CRegKey类并不是一个MFC类,而是一个ATL类,
// 所以在使用的时候不要忘记在StdAfx.h头文件中加入#include <atlbase.h>。
void CEnvManager::GetInt()
{
// 获取注册表中整数值
CRegKey rk;
DWORD dwValue;
CString strTemp;
LPCTSTR lpszKeyName = _T ("Software//Microsoft//Windows//CurrentVersion//Explorer");
if (rk.Open (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success./n");
if (rk.QueryValue (dwValue, _T ("Browse For Folder Height")) == ERROR_SUCCESS)
{
TRACE0 ("Query Success./n");
strTemp.Format ("%0X", dwValue);
}
}
rk.Close();
}
void CEnvManager::GetStr()
{
// 获取注册表中字符串值
CRegKey rk;
DWORD dwCount = 1024;
CString strTemp;
TCHAR szValue [1024];
// LPCTSTR lpszKeyName = _T ("Software//Microsoft//Windows//CurrentVersion//Explorer");
// if (rk.Open (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
// {
// TRACE0 ("Open Success./n");
// if (rk.QueryValue (szValue, _T ("Logon User Name"), &dwCount) == ERROR_SUCCESS)
// {
// TRACE0 ("Query Success./n");
// strTemp = szValue;
// }
// }
rk.Close();
}
void CEnvManager::SetStr()
{
// 设置注册表中字符串值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software//JueYuanLongStudio//Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success./n");
if (rk.SetValue (_T ("palagon"), _T ("Logon User Name")) == ERROR_SUCCESS)
{
TRACE0 ("SetValue Success./n");
}
}
rk.Close();
}
void CEnvManager::SetInt()
{
// 设置注册表中数据值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software//JueYuanLong Studio//Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success./n");
if (rk.SetValue (110, _T ("Logon User Data")) == ERROR_SUCCESS)
{
TRACE0 ("SetValue Success./n");
}
}
rk.Close();
}
void CEnvManager::Delete()
{
// 删除注册表中键值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software//JueYuanLong Studio//Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success./n");
if (rk.DeleteValue (_T ("Logon User Data")) == ERROR_SUCCESS)
{
TRACE0 ("DeleteValue Success./n");
}
}
rk.Close();
}
BOOL CEnvManager::verify(LPSTR lpszCmdLine)
{
TCHAR szKey[5120]= {0};
// get project name
SetPrjName((CHAR *)szKey);
// get work directory
SetWorkPath(GetCommandLine());
// verify project name & work directory
if (prjname.size() <= 0) {
return FALSE;
} else if (workpath.size() <= 0) {
return FALSE;
}
LPCTSTR lpszKeyEnv = _T ("SYSTEM//CurrentControlSet//Control//Session Manager//Environment");
CRegKey rk;
BOOL fModified = FALSE;
CString strAcs;
CString strTmp;
DWORD dwCount = sizeof(szKey);
// open enviroment registry key
if (rk.Open (HKEY_LOCAL_MACHINE, lpszKeyEnv) != ERROR_SUCCESS) {
errmsg = "Cann't open HKEY_LOCAL_MACHINE !";
return FALSE;
}
memset(szKey, 0, sizeof(szKey));
dwCount = sizeof(szKey);
if (rk.QueryValue (szKey, _T ("PATH"), &dwCount) == ERROR_SUCCESS) {
strAcs = szKey;
} else {
strAcs.Empty();
}
rk.Close();
return TRUE;
}
lpSetValue = (LPBYTE)TEXT(lpszSet);
dwCount = strlen(lpszSet) + 1;
dwType = REG_SZ;
if(ERROR_SUCCESS == RegSetValueEx(hkey, TEXT("PATH"), NULL, dwType, lpSetValue, dwCount)){
fModified = TRUE;
}
注意:直接lpSetValue = (LPBYTE)lpszSet时%xxx%变量将不会被自动替换!
修改之后,通知所有应用环境变量已经改变
SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)"Environment",SMTO_ABORTIFHUNG,500,&dwDisposition);
函数getenv不好用,总是返回win系统启动时的环境;可能GetEnvironmentVariable要好用些,需要继续测试才知道...
---------------------------------------------------
以下的使用CRegKey的实现
---------------------------------------------------
// EnvManager.h: interface for the CEnvManager class.
//
//
#if !defined(AFX_ENVMANAGER_H__C4B551D3_3E8C_43F7_BF77_E76288246BC7__INCLUDED_)
#define AFX_ENVMANAGER_H__C4B551D3_3E8C_43F7_BF77_E76288246BC7__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <string>
using namespace std;
class CEnvManager
{
public:
CEnvManager();
virtual ~CEnvManager();
public:
VOID SetPrjName(LPSTR name);
VOID SetWorkPath(LPSTR lpszCmdLine);
BOOL verify(LPSTR lpszCmdLine = NULL);
void Delete();
void SetInt();
void SetStr();
void GetStr();
void GetInt();
private:
string errmsg;
string workpath;
string prjname;
};
#endif // !defined(AFX_ENVMANAGER_H__C4B551D3_3E8C_43F7_BF77_E76288246BC7__INCLUDED_)
// EnvManager.cpp: implementation of the CEnvManager class.
//
//
#include "StdAfx.h"
#include <atlbase.h>
#include "sdk_env.h"
#include "sdk_macro.h"
//
// Construction/Destruction
//
CEnvManager::CEnvManager()
{
}
CEnvManager::~CEnvManager()
{
}
VOID CEnvManager::SetWorkPath(LPSTR lpszCmdLine)
{
workpath = lpszCmdLine;
}
VOID CEnvManager::SetPrjName(LPSTR name)
{
prjname = name;
}
// 用CRegKey类来操作注册表是非常方便的。CRegKey类并不是一个MFC类,而是一个ATL类,
// 所以在使用的时候不要忘记在StdAfx.h头文件中加入#include <atlbase.h>。
void CEnvManager::GetInt()
{
// 获取注册表中整数值
CRegKey rk;
DWORD dwValue;
CString strTemp;
LPCTSTR lpszKeyName = _T ("Software//Microsoft//Windows//CurrentVersion//Explorer");
if (rk.Open (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success./n");
if (rk.QueryValue (dwValue, _T ("Browse For Folder Height")) == ERROR_SUCCESS)
{
TRACE0 ("Query Success./n");
strTemp.Format ("%0X", dwValue);
}
}
rk.Close();
}
void CEnvManager::GetStr()
{
// 获取注册表中字符串值
CRegKey rk;
DWORD dwCount = 1024;
CString strTemp;
TCHAR szValue [1024];
// LPCTSTR lpszKeyName = _T ("Software//Microsoft//Windows//CurrentVersion//Explorer");
// if (rk.Open (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
// {
// TRACE0 ("Open Success./n");
// if (rk.QueryValue (szValue, _T ("Logon User Name"), &dwCount) == ERROR_SUCCESS)
// {
// TRACE0 ("Query Success./n");
// strTemp = szValue;
// }
// }
rk.Close();
}
void CEnvManager::SetStr()
{
// 设置注册表中字符串值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software//JueYuanLongStudio//Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success./n");
if (rk.SetValue (_T ("palagon"), _T ("Logon User Name")) == ERROR_SUCCESS)
{
TRACE0 ("SetValue Success./n");
}
}
rk.Close();
}
void CEnvManager::SetInt()
{
// 设置注册表中数据值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software//JueYuanLong Studio//Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success./n");
if (rk.SetValue (110, _T ("Logon User Data")) == ERROR_SUCCESS)
{
TRACE0 ("SetValue Success./n");
}
}
rk.Close();
}
void CEnvManager::Delete()
{
// 删除注册表中键值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software//JueYuanLong Studio//Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success./n");
if (rk.DeleteValue (_T ("Logon User Data")) == ERROR_SUCCESS)
{
TRACE0 ("DeleteValue Success./n");
}
}
rk.Close();
}
BOOL CEnvManager::verify(LPSTR lpszCmdLine)
{
TCHAR szKey[5120]= {0};
// get project name
SetPrjName((CHAR *)szKey);
// get work directory
SetWorkPath(GetCommandLine());
// verify project name & work directory
if (prjname.size() <= 0) {
return FALSE;
} else if (workpath.size() <= 0) {
return FALSE;
}
LPCTSTR lpszKeyEnv = _T ("SYSTEM//CurrentControlSet//Control//Session Manager//Environment");
CRegKey rk;
BOOL fModified = FALSE;
CString strAcs;
CString strTmp;
DWORD dwCount = sizeof(szKey);
// open enviroment registry key
if (rk.Open (HKEY_LOCAL_MACHINE, lpszKeyEnv) != ERROR_SUCCESS) {
errmsg = "Cann't open HKEY_LOCAL_MACHINE !";
return FALSE;
}
memset(szKey, 0, sizeof(szKey));
dwCount = sizeof(szKey);
if (rk.QueryValue (szKey, _T ("PATH"), &dwCount) == ERROR_SUCCESS) {
strAcs = szKey;
} else {
strAcs.Empty();
}
rk.Close();
return TRUE;
}