GetEnvironmentVariable是一个从调用该函数的进程的环境变量中返回指定的变量名值的函数,主要参数有lpName、lpBuffer等。
DWORD GetEnvironmentVariable(
LPCTSTR lpName, // address of environment variable name
LPTSTR lpBuffer, // address of buffer for variable value
DWORD nSize // size of buffer, in characters
);
参数:
lpName:要获取值的变量名字符串指针.
lpBuffer:接收变量值的字符串指针
nSize:接收变量值的内存大小.
返回值:
如果函数成功执行,则返回值是写入字符缓冲区的字符数量,但不包含'\0'字符.
如果没有找到所指的变量,则返回零.
如果字符缓冲区的大小小于变量值的长度,返回值为缓冲区的大小.
SysUtils
function GetEnvironmentVariable(const Name: string): string;
-------------------------------------------------------------------------------------------
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Clear; Memo1.Lines.Add('WIN目录: ' + GetEnvironmentVariable('WINDIR')); Memo1.Lines.Add('系统目录: ' + GetEnvironmentVariable('SYSTEMROOT')); Memo1.Lines.Add('系统根目录: ' + GetEnvironmentVariable('SYSTEMDRIVE')); Memo1.Lines.Add('当前用户根目录: ' + GetEnvironmentVariable('HOMEDRIVE')); Memo1.Lines.Add('当前用户目录: ' + GetEnvironmentVariable('USERPROFILE')); Memo1.Lines.Add('当前用户路径: ' + GetEnvironmentVariable('HOMEPATH')); Memo1.Lines.Add('当前用户临时文件夹: ' + GetEnvironmentVariable('TMP')); Memo1.Lines.Add('当前用户临时文件夹: ' + GetEnvironmentVariable('TEMP')); Memo1.Lines.Add('当前用户数据文件夹: ' + GetEnvironmentVariable('APPDATA')); Memo1.Lines.Add('程序默认安装目录: ' + GetEnvironmentVariable('PROGRAMFILES')); Memo1.Lines.Add('文件通用目录: ' + GetEnvironmentVariable('COMMONPROGRAMFILES')); Memo1.Lines.Add('当前用户名: ' + GetEnvironmentVariable('USERNAME')); Memo1.Lines.Add('所有用户文件目录: ' + GetEnvironmentVariable('ALLUSERSPROFILE')); Memo1.Lines.Add('操作系统名: ' + GetEnvironmentVariable('OS')); Memo1.Lines.Add('计算机名: ' + GetEnvironmentVariable('COMPUTERNAME')); Memo1.Lines.Add('处理器个数: ' + GetEnvironmentVariable('NUMBER_OF_PROCESSORS')); Memo1.Lines.Add('处理器芯片架构: ' + GetEnvironmentVariable('PROCESSOR_ARCHITECTURE')); Memo1.Lines.Add('处理器型号: ' + GetEnvironmentVariable('PROCESSOR_LEVEL')); Memo1.Lines.Add('处理器修订号: ' + GetEnvironmentVariable('PROCESSOR_REVISION')); Memo1.Lines.Add('包含用户帐号的域: ' + GetEnvironmentVariable('USERDOMAIN')); Memo1.Lines.Add('COMSPEC: ' + GetEnvironmentVariable('COMSPEC')); Memo1.Lines.Add('执行文件类型: ' + GetEnvironmentVariable('PATHEXT')); Memo1.Lines.Add('搜索路径: ' + GetEnvironmentVariable('PATH')); end; end.
-------------------------------------------------------------------------------------------------
object Form1: TForm1 Left = 288 Top = 184 Width = 882 Height = 675 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object Memo1: TMemo Left = 0 Top = 0 Width = 866 Height = 636 Align = alClient Lines.Strings = ( 'Memo1') ScrollBars = ssVertical TabOrder = 0 end end