功能:打印系统指定环境变量的值
来源:《Windows核心编程(第五版)》第4.1.4 进程的环境变量
在主程序中只要调用以下代码:
1 PrintEnvironmentVariable (TEXT("TEMP"));
1 void PrintEnvironmentVariable(PCTSTR pszVariableName) 2 { 3 PTSTR pszValue = NULL; 4 //Get the size of thebuffer that is required to store the value 5 DWORD dwRusult = GetEnvironmentVariable(pszVariableName, pszValue, 0); 6 if (dwRusult != 0) 7 { 8 //Allocate the buffer to store the environment variable value 9 DWORD size = dwRusult * sizeof(TCHAR); 10 pszValue = (PTSTR)malloc(size); 11 GetEnvironmentVariable(pszVariableName, pszValue, size); 12 _tprintf(TEXT("%s=%s\n"), pszVariableName, pszValue); 13 free (pszValue); 14 }else 15 { 16 _tprintf(TEXT("'%s'=<unknow value>\n"), pszVariableName); 17 } 18 }