In the old DOS days, you could use environment variables to make certain
settings available throughout the entire system. These variables are still used,
but you almost can't notice it. Go to a DOS prompt and type "set"
(without the quotes), then you get to see all the environment variables that
have been set on your system. Examples are PATH, which contains all the paths
DOS should look in to find a certain file, PROMPT, which determines how to
display your DOS-prompt, and so on.
Using Delphi, you can of course read those environment variables. You do this
via the Windows API function GetEnvironmentVariable. This function takes three
parameters, and returns an integer. To put the environment variable PATH into
the PChar EnvVar, you can use the following:
var
EnvVar: PChar;
i: integer;
begin
EnvVar := PChar(''); // initialize the PChar, otherwise you get an
access
//
violation error when using the
//
GetEnvironmentVariable function
i := GetEnvironmentVariable(PChar('path'), EnvVar, 255);
end;
The variable EnvVar will now contain the contents of the environment variable
PATH. The first parameter is, obviously, the name of the environment variable
you want to get. Make sure it's a PChar, because Windows API functions can't
work with strings. The second parameter is the variable you want to have the
contents go to. This also has to be a PChar. And the last value is the number of
characters you want copied into the variable set in the second parameter.
The integer i now contains the number of characters the function has copied
into EnvVar. If you make the number of characters you want to copy too low, so
that the length of the environment variable is higher than the number of
characters you had told the function to copy, it copies nothing and just returns
the length of the variable. So if you want to be sure that you get the entire
string, you could change the function to this:
var
EnvVar: PChar;
i: integer;
begin
EnvVar := PChar('');
i := GetEnvironmentVariable(PChar('path'), EnvVar, 0);
GetEnvironmentVariable(Pchar('path'), EnvVar, i);
end;
|