Hi
If I understand you correcttly then these Two API functions will serve
you:

<snip>
SetEnvironment and GetEnvironment.

Both are used in the context of GDI functions and were presumably built
for
purposes of working with printer drivers, but it turns out they can
easily
be twisted to the above needs as well:

Neither of them are well documented in the online windows API help, but
both
are poorly mentioned in the Windows programming reference....
Essentially they take the same parameter list:

SetEnvironment(PChar,PChar,Integer)
GetEnvironment(PChar,PChar,Integer)

Use SetEnvironment to place an arbitrary "key" into the environment.
Much
like a key value in the DOS environment or INI file - MYKEY=SOMEVALUE
would
be written as:

            SetEnvironment('MYKEY','SOMEVALUE',Length('SOMEVALUE'));

GetEnvironment retrieves the value associated with the Key, but you need
to
know the length of the value before you retrieve it.  So, you can first
call
GetEnvironment with a Null in the value parameter position and it
returns
the length:

        BufLen := GetEnvironment('MYKEY',NULL,SOMELARGENUMBER);
        MyBuffer := StrAlloc(BufLen +1);
        GetEnvironment('MYKEY',MyBuffer,BufLen+1);

SOMELARGENUMBER is used above because (at least in my early trials, I
think,
and here is where I would need to go back and check it) when I used 0
there,
GetEnvironment always returned 0.  SOMELARGENUMBER (something larger
than
your largest anticipated value) for sure will allow the first call to
return
a proper length.

</snip>

Cheers
Johan Fourie

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Arno Garrels
Sent: 09 May 2005 08:07
To: Borland's Delphi Discussion List
Subject: How to fetch environment variables correctly 

Hello,

over the past years I fetched the environment variables from the
registry keys
System: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment\
User: HKEY_CURRENT_USER\Environment\
However not all variables are retrieved i.e. SystemRoot is missing.
GetEnvironmentStrings() appears to be the right win-function to call,
however the
comment confuses me:

"Do not use the return value of GetEnvironmentStrings to get or set
environment variables. Instead, use the GetEnvironmentVariable and
SetEnvironmentVariable functions to access the environment variables
within this block. When the block is no longer needed, it should be
freed by calling FreeEnvironmentStrings."

How can I get all environment variables reliable? I need to store them
temporarily and pass them later on as environment block to
CreateProcess(). Is it save to call GetEnvironmentVariable() on each
returned name?

Thanks in advance,

Arno Garrels



_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi



NOTICE: Please note that this eMail, and the contents thereof, is subject to 
the standard arivia.kom email disclaimer which may be found at:  
http://www.arivia.co.za/internet/disclaimer.htm.  If you cannot access the 
disclaimer through the URL attached, and you wish to receive a copy thereof, 
please send an eMail to [EMAIL PROTECTED] or call (011) 233-0800. You will 
receive the disclaimer by return email or fax.




_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to