[Win32] 환경변수 설정 값 가져오기 - GetEnvironmentVariable
Retrieves the contents of the specified variable from the environment block of the calling process.
__in LPCTSTR lpName,
__out LPTSTR lpBuffer,
__in DWORD nSize
);
Parameters
lpName
The name of the environment variable.
lpBuffer
A pointer to a buffer that receives the contents of the specified environment variable as a null-terminated string. An environment variable has a maximum size limit of 32,767 characters, including the null-terminating character.
환경변수 레지스트리 위치
시스템 환경 변수
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\control\session Manager\Enviroment
사용자 환경 변수
HKEY_CURRENT_USER\Environment
바로 적용시키는 코드
::SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0,(LPARAM)TEXT("Environment"));
- 이하 아래 내용은 프로그램 내에서만 유효한 변경이다.
프로그램 내에서 환경변수 가져오기
int nSize = ::GetEnvironmentVariable("TEMP", NULL, NULL);
TCHAR* buffer = new TCHAR[nSize +1];
::GetEnvironmentVariable("TEMP",buffer, nSize);
// 만약 버퍼의 크기가 변수의 크기보다 작다면 0이 리턴 되므로 버퍼의 크기를 nSize만큼 얻어와야 한다.
프로그램 내에서 환경변수 설정하기
::SetEnvironmentVariable("TEMP", "C:\\tmp");
프로그램 내에서 환경변수 지우기
::SetEnvironmentVariable("TEMP", NULL);
예제)
// * Map 처음 문자를 환경변수에 세팅
char buf[50] = {0,};
::GetPrivateProfileString("CONFIG", "PRE_CHAR", "S", buf, 50, szCfgFile);
SetEnvironmentVariable("PRE_CHAR", buf);
char buf[30] = {0,};
// 환경변수에서 Map 처음문자 가져오기
GetEnvironmentVariable("PRE_CHAR", buf, 30);