Arno Garrels wrote:
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.
Do not use the registry to fetch information that is exposed by API
functions. For one thing, it might not be up to date; calls to
SetEnvironmentVariable don't affect the global environment, only the
environment of the current process.
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."
MSDN says this:
"The GetEnvironmentStrings function returns a pointer to a block of
memory that contains the environment variables of the calling process.
Treat this memory as read-only; do not modify it directly. To add or
change an environment variable, use the GetEnvironmentVariable and
SetEnvironmentVariable functions.
"When the block returned by GetEnvironmentStrings is no longer needed,
it should be freed by calling the FreeEnvironmentStrings function."
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?
GetEnvironmentStrings is exactly what you're supposed to use to get the
list of all the environment variables. You're just not supposed to use
it to *set* their values. And if you want to get the value of a
particular variable, it's easier to call GetEnvironmentVariable than it
is to parse the whole environment block.
It's the perfect thing to call when you need to pass something to
CreateProcess.
--
Rob
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi