//
// ENVIR. This sample shows how to get process environment strings and
// values of individual environment variables. Uses GetEnvironmentStrings
// and GetEnvironmentVariable functions from WIN32 API
//
unit EnvUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit2: TEdit;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
DosEnv : PChar;
I : Integer;
SignPos : Integer;
begin
//
// GetEnvironmentStrings = 32-bit version of GetDosEnvironment
//
Memo1.Lines.Clear;
// Get all environment variables
DosEnv := GetEnvironmentStrings;
While DosEnv^ <> #0 do
begin
// Add one by one into memo
Memo1.Lines.Add(StrPas(DosEnv));
// Get next one
Inc(DosEnv, StrLen(DosEnv)+1);
end;
// Fill in ComboBox
With Memo1 do
begin
for I := 0 to Lines.Count-1 do
begin
SignPos := Pos('=',Lines[I]);
ComboBox1.Items.Add(Copy(Lines[I], 1, SignPos-1));
end;
end;
// Show 1st element
ComboBox1.ItemIndex := 0;
ComboBox1.OnChange(Self);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Name, Value : PChar;
begin
// Get memory
Name := StrAlloc(255); Value:= StrAlloc(255);
// Convert to PChar
StrPCopy(Name, ComboBox1.Items[ComboBox1.ItemIndex]);
// Get variable value
GetEnvironmentVariable(Name, Value, 255);
// Show it as Pascal string
Edit2.Text := StrPas(Value);
// Free memory
StrDispose(Name); StrDispose(Value);
end;
end.
|