include "stdafx.h" #include <Windows.h> #include <iostream> #pragma warning(disable:4996) int _tmain(int argc, _TCHAR* argv[]) { char appPath[MAX_PATH]; GetEnvironmentVariableA("%%TEMP%%", appPath, 256); //取不到 GetEnvironmentVariableA("%TEMP%", appPath, 256); //取不到 GetEnvironmentVariableA("TEMP", appPath, 256); //取到了
char* psz = NULL; psz = getenv("%%TEMP%%"); //取不到 psz = getenv("%TEMP%"); //取不到 psz = getenv("TEMP"); //取到了 char computerName[MAX_PATH]; DWORD bufferSize = sizeof(computerName); //环境变量相关api,调用成功 //@err,hr 0x000000cb 操作系统找不到已输入的环境选项 //内部引用了环境变量 GetComputerNameA(computerName, &bufferSize); TCHAR appPath[MAX_PATH]; char* psz = getenv("TEMP"); //%%TEMP%%\%s_p.ax /* GetEnvironmentVariable(_T("TEMP"), appPath, MAX_PATH); appPath[0] = 0; //结果正常 "C:UsersvvAppDataLocalTemp" GetEnvironmentVariable(_T("ProgramFiles"), appPath, MAX_PATH); appPath[0] = 0; //结果正常 "C:Program Files" ExpandEnvironmentStrings(_T("ProgramFiles"), appPath, MAX_PATH); appPath[0] = 0; //结果取不到, ProgramFiles */ ExpandEnvironmentStrings(_T("%%ProgramFiles%%"), appPath, MAX_PATH); appPath[0] = 0; //结果 取出结果套了一层符号 "%C:Program Files%" ExpandEnvironmentStrings(_T("%%ProgramFiles%%\xxx_.xx"), appPath, MAX_PATH); appPath[0] = 0; //结果 取出结果套了一层符号 "%C:Program Files%\xxx" ExpandEnvironmentStrings(_T("%ProgramFiles%\xxx"), appPath, MAX_PATH); //结果正常 "C:Program Files" printf("%s ",appPath); wchar_t buffer[MAX_PATH]; DWORD code = GetEnvironmentVariable(L"WINDIR", buffer, MAX_PATH); std::wcout << L"WINDIR: " << buffer << std::endl; GetEnvironmentVariable(L"SYSTEMROOT", buffer, MAX_PATH); std::wcout << L"SYSTEMROOT: " << buffer << std::endl; GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, MAX_PATH); std::wcout << L"SYSTEMDRIVE: " << buffer << std::endl; GetEnvironmentVariable(L"HOMEDRIVE", buffer, MAX_PATH); std::wcout << L"HOMEDRIVE: " << buffer << std::endl; GetEnvironmentVariable(L"USERPROFILE", buffer, MAX_PATH); std::wcout << L"USERPROFILE: " << buffer << std::endl; GetEnvironmentVariable(L"HOMEPATH", buffer, MAX_PATH); std::wcout << L"HOMEPATH: " << buffer << std::endl; GetEnvironmentVariable(L"TMP", buffer, MAX_PATH); std::wcout << L"TMP: " << buffer << std::endl; GetEnvironmentVariable(L"TEMP", buffer, MAX_PATH); std::wcout << L"TEMP: " << buffer << std::endl; GetEnvironmentVariable(L"APPDATA", buffer, MAX_PATH); std::wcout << L"APPDATA: " << buffer << std::endl; GetEnvironmentVariable(L"PROGRAMFILES", buffer, MAX_PATH); std::wcout << L"PROGRAMFILES: " << buffer << std::endl; GetEnvironmentVariable(L"COMMONPROGRAMFILES", buffer, MAX_PATH); std::wcout << L"COMMONPROGRAMFILES: " << buffer << std::endl; GetEnvironmentVariable(L"USERNAME", buffer, MAX_PATH); std::wcout << L"USERNAME: " << buffer << std::endl; GetEnvironmentVariable(L"ALLUSERSPROFILE", buffer, MAX_PATH); std::wcout << L"ALLUSERSPROFILE: " << buffer << std::endl; GetEnvironmentVariable(L"OS", buffer, MAX_PATH); std::wcout << L"OS: " << buffer << std::endl; GetEnvironmentVariable(L"COMPUTERNAME", buffer, MAX_PATH); std::wcout << L"COMPUTERNAME: " << buffer << std::endl; GetEnvironmentVariable(L"NUMBER_OF_PROCESSORS", buffer, MAX_PATH); std::wcout << L"NUMBER_OF_PROCESSORS: " << buffer << std::endl; GetEnvironmentVariable(L"PROCESSOR_ARCHITECTURE", buffer, MAX_PATH); std::wcout << L"PROCESSOR_ARCHITECTURE: " << buffer << std::endl; GetEnvironmentVariable(L"PROCESSOR_LEVEL", buffer, MAX_PATH); std::wcout << L"PROCESSOR_LEVEL: " << buffer << std::endl; GetEnvironmentVariable(L"PROCESSOR_REVISION", buffer, MAX_PATH); std::wcout << L"PROCESSOR_REVISION: " << buffer << std::endl; GetEnvironmentVariable(L"USERDOMAIN", buffer, MAX_PATH); std::wcout << L"USERDOMAIN: " << buffer << std::endl; GetEnvironmentVariable(L"COMSPEC", buffer, MAX_PATH); std::wcout << L"COMSPEC: " << buffer << std::endl; GetEnvironmentVariable(L"PATHEXT", buffer, MAX_PATH); std::wcout << L"PATHEXT: " << buffer << std::endl; GetEnvironmentVariable(L"PATH", buffer, MAX_PATH); std::wcout << L"PATH: " << buffer << std::endl; system("pause"); return 0; }