I need to be able to set and read DOS or Windows environment variables
using Delphi. I am using Delphi 5.0 Client Server with update pack 1
installed. Can somebody help?
Thanks in advance,
Nick
Nick Tentomas a ιcrit dans le message
<399AA283...@CSBFACMGT.CSBFM.DAL.CA>...
"Fred B" <dec...@paprika.net> wrote in message
news:8neds4$pt...@bornews.borland.com...
Thanks for the answer. I have tryied to set an environment variable and it
seems that it set OK but when I go to a DOS window and type "SET" I do not
see it. Do you know why is this happening?
Thanks again,
Nick
Fred B wrote:
> Have a look to getenvironmentvariable
>
> Nick Tentomas a ΞΉcrit dans le message
There is a master environment that is set at startup (e.g. from your
autoexec.bat) that is available to all processes and Virtual Machines. When
you start a new process, it inherits a copy of that master environment.
However, any changes made to that inherited (child) copy are not reflected
in the master environment, but are only available to the current process.
Per the SDK doc:
The SetEnvironmentVariable function sets the value of an environment
variable for the current process. This function has no effect on the
system environment variables or the environment variables of other
processes.
It is possible, but extremely complex, to alter the master environment.
Microsoft provides a utility called Winset.exe with the Win 95/98 resource
kits that can be used to alter the master environment. (I believe it works
with NT also.) One possible solution for you might be to use that utility
with a CreateProcess call from your Delphi program.
Here's what winset /? displays for your further info:
>winset /?
Sets or removes Windows master environment variables.
WINSET [variable=[string]]
variable Specifies the environment-variable name.
string Specifies a series of characters to assign to the variable.
regards,
--
Greg Sinopoli
GTS Software Development Services
http://www.gts-soft.com
Nick Tentomas <Nten...@CSBFACMGT.CSBFM.DAL.CA> wrote in message
news:399BC9C7...@CSBFACMGT.CSBFM.DAL.CA...
> Hi Fred,
>
> Thanks for the answer. I have tryied to set an environment variable and it
> seems that it set OK but when I go to a DOS window and type "SET" I do not
> see it. Do you know why is this happening?
>
> Thanks again,
>
> Nick
>
> Fred B wrote:
>
> > Have a look to getenvironmentvariable
> >
> > Nick Tentomas a ιcrit dans le message
--
Greg Sinopoli
GTS Software Development Services
http://www.gts-soft.com
Greg Sinopoli <x...@nospam.com> wrote in message
news:8nhhv2$op...@bornews.borland.com...
The only drawback is if you set an environment variable in a program
run from a command prompt, other programs run from the same command
prompt won't be able to see the new variable. For instance if you
write a console application called 'SetVar' that uses the program, and
you try to call it from a batch file
@echo off
SetVar x xyzzy
echo %x%
.. it won't echo correctly.
I discovered a sort of way round this the other day, though you can run
a second batch file, immediately after you've run SetVar from the first
batch file, using start /I
//--------b1.cmd
@echo off
SetVar x xyzzy
start /I b2.cmd
//--------b2.cmd
@echo off
echo %x%
.. will echo correctly
procedure SetSystemEnvironmentVariable (const name, value : string);
var
rv : DWORD;
begin
with TRegistry.Create (KEY_READ or KEY_WRITE) do
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey ('SYSTEM\CurrentControlSet\Control\Session
Manager\Environment', False);
if value <> '' then
WriteExpandString (name, value)
else
if ValueExists (name) then
DeleteValue (name);
SendMessageTimeout (HWND_BROADCAST, WM_SETTINGCHANGE, 0, LParam
(PChar ('Environment')), SMTO_ABORTIFHUNG,
5000, rv);
finally
Free
end;
if not SetEnvironmentVariable (PChar (name), PChar (value)) then
RaiseLastWin32Error
end;
Colin
e-mail :co...@wilsonc.demon.co.uk
web: http://www.wilsonc.demon.co.uk/delphi.htm