GetEnvironmentVariable 获取系统环境变量

2018年5月4日08:25:43
  • A+
所属分类:Delphi
  1. WINDIR:                 //系统目录 - C:\WINDOWS
  2. SYSTEMROOT:             //系统目录 - C:\WINDOWS
  3. SYSTEMDRIVE:            //系统根目录 - C:
  4. HOMEDRIVE:              //当前用户根目录 - C:
  5. USERPROFILE:            //当前用户目录 - C:\Users\Kandy
  6. HOMEPATH:               //当前用户路径 - \Users\Kandy
  7. TMP:                    //当前用户临时文件夹 - C:\Users\Kandy\AppData\Local\Temp
  8. TEMP:                   //当前用户临时文件夹 - C:\Users\Kandy\AppData\Local\Temp
  9. APPDATA:                //当前用户数据文件夹 - C:\Users\Kandy\AppData\Roaming
  10. PROGRAMFILES:           //程序默认安装目录 - C:\Program Files (x86)
  11. COMMONPROGRAMFILES:     //文件通用目录 - C:\Program Files (x86)\Common Files
  12. USERNAME:               //当前用户名 - Kandy
  13. ALLUSERSPROFILE:        //所有用户文件目录 - C:\ProgramData
  14. OS:                     //操作系统名 - Windows_NT
  15. COMPUTERNAME:           //计算机名 - KANDY-PC
  16. NUMBER_OF_PROCESSORS:   //处理器个数 - 4
  17. PROCESSOR_ARCHITECTURE: //处理器芯片架构 - x86
  18. PROCESSOR_LEVEL:        //处理器型号 - 6
  19. PROCESSOR_REVISION:     //处理器修订号 - 3c03
  20. USERDOMAIN:             //包含用户帐号的域 - KANDY-PC
  21. COMSPEC:                //C:\WINDOWS\system32\cmd.exe
  22. PATHEXT:                //执行文件类型 - .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
  23. PATH:                   //搜索路径

使用 GetEnvironmentVariable 来获取环境变量:

  1. #include <Windows.h>
  2. #include <iostream>
  3. int main()
  4. {
  5.     wchar_t buffer[MAX_PATH];
  6.     DWORD code = GetEnvironmentVariable(L"WINDIR", buffer, MAX_PATH);
  7.     std::wcout << L"WINDIR:\t\t\t" << buffer << std::endl;
  8.     GetEnvironmentVariable(L"SYSTEMROOT", buffer, MAX_PATH);
  9.     std::wcout << L"SYSTEMROOT:\t\t" << buffer << std::endl;
  10.     GetEnvironmentVariable(L"SYSTEMDRIVE", buffer, MAX_PATH);
  11.     std::wcout << L"SYSTEMDRIVE:\t\t" << buffer << std::endl;
  12.     GetEnvironmentVariable(L"HOMEDRIVE", buffer, MAX_PATH);
  13.     std::wcout << L"HOMEDRIVE:\t\t" << buffer << std::endl;
  14.     GetEnvironmentVariable(L"USERPROFILE", buffer, MAX_PATH);
  15.     std::wcout << L"USERPROFILE:\t\t" << buffer << std::endl;
  16.     GetEnvironmentVariable(L"HOMEPATH", buffer, MAX_PATH);
  17.     std::wcout << L"HOMEPATH:\t\t" << buffer << std::endl;
  18.     GetEnvironmentVariable(L"TMP", buffer, MAX_PATH);
  19.     std::wcout << L"TMP:\t\t\t" << buffer << std::endl;
  20.     GetEnvironmentVariable(L"TEMP", buffer, MAX_PATH);
  21.     std::wcout << L"TEMP:\t\t\t" << buffer << std::endl;
  22.     GetEnvironmentVariable(L"APPDATA", buffer, MAX_PATH);
  23.     std::wcout << L"APPDATA:\t\t" << buffer << std::endl;
  24.     GetEnvironmentVariable(L"PROGRAMFILES", buffer, MAX_PATH);
  25.     std::wcout << L"PROGRAMFILES:\t\t" << buffer << std::endl;
  26.     GetEnvironmentVariable(L"COMMONPROGRAMFILES", buffer, MAX_PATH);
  27.     std::wcout << L"COMMONPROGRAMFILES:\t" << buffer << std::endl;
  28.     GetEnvironmentVariable(L"USERNAME", buffer, MAX_PATH);
  29.     std::wcout << L"USERNAME:\t\t" << buffer << std::endl;
  30.     GetEnvironmentVariable(L"ALLUSERSPROFILE", buffer, MAX_PATH);
  31.     std::wcout << L"ALLUSERSPROFILE:\t" << buffer << std::endl;
  32.     GetEnvironmentVariable(L"OS", buffer, MAX_PATH);
  33.     std::wcout << L"OS:\t\t\t" << buffer << std::endl;
  34.     GetEnvironmentVariable(L"COMPUTERNAME", buffer, MAX_PATH);
  35.     std::wcout << L"COMPUTERNAME:\t\t" << buffer << std::endl;
  36.     GetEnvironmentVariable(L"NUMBER_OF_PROCESSORS", buffer, MAX_PATH);
  37.     std::wcout << L"NUMBER_OF_PROCESSORS:\t" << buffer << std::endl;
  38.     GetEnvironmentVariable(L"PROCESSOR_ARCHITECTURE", buffer, MAX_PATH);
  39.     std::wcout << L"PROCESSOR_ARCHITECTURE:\t" << buffer << std::endl;
  40.     GetEnvironmentVariable(L"PROCESSOR_LEVEL", buffer, MAX_PATH);
  41.     std::wcout << L"PROCESSOR_LEVEL:\t" << buffer << std::endl;
  42.     GetEnvironmentVariable(L"PROCESSOR_REVISION", buffer, MAX_PATH);
  43.     std::wcout << L"PROCESSOR_REVISION:\t" << buffer << std::endl;
  44.     GetEnvironmentVariable(L"USERDOMAIN", buffer, MAX_PATH);
  45.     std::wcout << L"USERDOMAIN:\t\t" << buffer << std::endl;
  46.     GetEnvironmentVariable(L"COMSPEC", buffer, MAX_PATH);
  47.     std::wcout << L"COMSPEC:\t\t" << buffer << std::endl;
  48.     GetEnvironmentVariable(L"PATHEXT", buffer, MAX_PATH);
  49.     std::wcout << L"PATHEXT:\t\t" << buffer << std::endl;
  50.     GetEnvironmentVariable(L"PATH", buffer, MAX_PATH);
  51.     std::wcout << L"PATH:\t\t" << buffer << std::endl;
  52.     system("pause");
  53.     return 0;
  54. }
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: