Sunday, November 07, 2010

GetEnvironmentVariable 與 SetEnvironmentVariable

/* 讀取系統的環境變數 */
TCHAR EnvVar[ 1024+1 ] = {0};
::GetEnvironmentVariable(_T("Path"), EnvVar, _countof(EnvVar));
MessageBox(EnvVar);

/* 取得目前的 working directory */
TCHAR CurrentWorkingDir[MAX_PATH] = "";
if (!::GetCurrentDirectory(sizeof(CurrentWorkingDir) - 1, CurrentWorkingDir))
{
  MessageBox(CurrentWorkingDir);
}
else
{
  ::GetLastError();
}

/* 設定系統的環境變數 */
SetEnvironmentVariable(_T("Path"), EnvVar);

No comments: