获取环境变量,0x000000cb 操作系统找不到已输入的环境选项
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:\Users\vv\AppData\Local\Temp" 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\r\n",appPath); wchar_t buffer[MAX_PATH]; DWORD code = GetEnvironmentVariable(L"WINDIR", buffer, MAX_PATH); std::wcout << L"WINDIR:\t\t\t" << buffer << std::endl; GetEnvironmentVariable(L"SYSTEMROOT", buffer, MAX_PATH); std::wcout << L"SYSTEMROOT:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, MAX_PATH); std::wcout << L"SYSTEMDRIVE:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"HOMEDRIVE", buffer, MAX_PATH); std::wcout << L"HOMEDRIVE:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"USERPROFILE", buffer, MAX_PATH); std::wcout << L"USERPROFILE:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"HOMEPATH", buffer, MAX_PATH); std::wcout << L"HOMEPATH:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"TMP", buffer, MAX_PATH); std::wcout << L"TMP:\t\t\t" << buffer << std::endl; GetEnvironmentVariable(L"TEMP", buffer, MAX_PATH); std::wcout << L"TEMP:\t\t\t" << buffer << std::endl; GetEnvironmentVariable(L"APPDATA", buffer, MAX_PATH); std::wcout << L"APPDATA:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"PROGRAMFILES", buffer, MAX_PATH); std::wcout << L"PROGRAMFILES:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"COMMONPROGRAMFILES", buffer, MAX_PATH); std::wcout << L"COMMONPROGRAMFILES:\t" << buffer << std::endl; GetEnvironmentVariable(L"USERNAME", buffer, MAX_PATH); std::wcout << L"USERNAME:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"ALLUSERSPROFILE", buffer, MAX_PATH); std::wcout << L"ALLUSERSPROFILE:\t" << buffer << std::endl; GetEnvironmentVariable(L"OS", buffer, MAX_PATH); std::wcout << L"OS:\t\t\t" << buffer << std::endl; GetEnvironmentVariable(L"COMPUTERNAME", buffer, MAX_PATH); std::wcout << L"COMPUTERNAME:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"NUMBER_OF_PROCESSORS", buffer, MAX_PATH); std::wcout << L"NUMBER_OF_PROCESSORS:\t" << buffer << std::endl; GetEnvironmentVariable(L"PROCESSOR_ARCHITECTURE", buffer, MAX_PATH); std::wcout << L"PROCESSOR_ARCHITECTURE:\t" << buffer << std::endl; GetEnvironmentVariable(L"PROCESSOR_LEVEL", buffer, MAX_PATH); std::wcout << L"PROCESSOR_LEVEL:\t" << buffer << std::endl; GetEnvironmentVariable(L"PROCESSOR_REVISION", buffer, MAX_PATH); std::wcout << L"PROCESSOR_REVISION:\t" << buffer << std::endl; GetEnvironmentVariable(L"USERDOMAIN", buffer, MAX_PATH); std::wcout << L"USERDOMAIN:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"COMSPEC", buffer, MAX_PATH); std::wcout << L"COMSPEC:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"PATHEXT", buffer, MAX_PATH); std::wcout << L"PATHEXT:\t\t" << buffer << std::endl; GetEnvironmentVariable(L"PATH", buffer, MAX_PATH); std::wcout << L"PATH:\t\t" << buffer << std::endl; system("pause"); return 0; }