🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.borland-delphi
4 messages
4 total messages Started by 0BIT Sat, 04 Jan 2003 11:59
System Variable PATH
#1722
Author: 0BIT
Date: Sat, 04 Jan 2003 11:59
145 lines
4646 bytes
I've written a program, but other programs don't update the PATH variable.

I've tried both SendMessageTimeOut and SendMessage.
After a restart of the computer the settings are processed good.
But not after the SendMessageTimeOut command.

What parameters should I pass to the SendMessageTimeOut function to notify
all programs the PATH has changed ?
I can't understand the help from the MSDN library.  : /

What is wrong ?

Happy newyear,
0BIT



program add_path;

{
Written for Delphi 7
}

uses
  Windows, Registry, Messages;

{$APPTYPE CONSOLE}
{$R *.res}

const
 pth_key = '\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\';
 pth_key_name = 'Path';

var
 pth_value : string;
 newpth : string;
 r : string;
 t : integer;
 alreadyadded : boolean;
 result : cardinal;

function GetRegistryValue(KeyName: string; Key : string): string;
 var
  Registry: TRegistry;
 begin
  Registry := TRegistry.Create(KEY_READ);
  try
   Registry.RootKey := HKEY_LOCAL_MACHINE;
   // False because we do not want to create it if it doesn't exist
   Registry.OpenKey(KeyName, False);
   Result := Registry.ReadString(Key);
  finally
   Registry.Free;
  end;
 end;

Procedure SetRegistryValue(KeyName: string; Key : string; Value : string);
 var
  Registry: TRegistry;
 begin
  Registry := TRegistry.Create(KEY_WRITE);
  try
   Registry.RootKey := HKEY_LOCAL_MACHINE;
   // False because we do not want to create it if it doesn't exist
   Registry.OpenKey(KeyName, True{False});
   Registry.WriteString(Key,Value);
   Registry.CloseKey;
  finally
   Registry.Free;
  end;
 end;

Function OnePath(p : string; nr : integer; var r : string) : boolean;
 var
  t,t2 : integer;
 begin
  t:=1;
  t2:=1;
  r:='';
  while (t2<nr) and (t<=Length(p)) do begin
   if p[t]=';' then
    inc(t2);
   inc(t);
  end;
  if t2=nr then begin
   OnePath:=True;
   while (t<=Length(p)) and (p[t]<>';') do begin
    r:=r+p[t];
    inc(t);
   end;
  end else
   OnePath:=False;
 end;

begin
 SetLength(pth_value,5000);
 SetLength(r,5000);
 SetLength(newpth,5000);

 {SetRegistryValue(pth_key,'temptest','Hallo2');}

{ WriteLn(ParamStr(1));}
 if Length(ParamStr(1))<>0 then begin
  pth_value:=GetRegistryValue(pth_key,pth_key_name);
{  WriteLn(pth_value);}

  t:=1;
  newpth:='';
  alreadyadded:=false;
  while OnePath(pth_value,t,r) do begin
   if r=ParamStr(1) then
    alreadyadded:=true;
   if t>=2 then
    newpth:=newpth+';';
   newpth:=newpth+r;
{   WriteLn(r);}
   inc(t);
  end;

  if not alreadyadded then begin
   if newpth='' then
    newpth:=ParamStr(1)
   else
    newpth:=ParamStr(1)+';'+newpth;
   SetRegistryValue(pth_key,pth_key_name,newpth);
   WriteLn('New path: ',newpth);
  end else begin
   WriteLn('No changes made to registry');
   WriteLn('Path registry key: ',pth_value);
  end;

   {Send message...to update programs}
   Result:=0;

WriteLn(SendMessageTimeOut(HWND_BROADCAST,WM_SETTINGCHANGE,0,0,0,15000000,Re
sult));
   WriteLn(Result);
{   WriteLn(SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0));}

 end else begin
  WriteLn('Usage: add_path "directory"');
 end;

  ReadLn;
end.
Re: System Variable PATH
#1723
Author: Kelly Leahy
Date: Sat, 04 Jan 2003 20:09
163 lines
5824 bytes
What programs don't update?

I don't think any programs typically update their path when a
WM_SETTINGSCHANGE message is received.  I've set my path in WinNT / 2000 /
XP a number of times (through the Windows UI) while a CMD window was open,
and it certainly doesn't update it's path.  I always have to close the CMD
window and reopen it if I want the change to take effect.  I would say that
if CMD.EXE doesn't update it's window, I wouldn't expect many other programs
to.

Kelly

"0BIT" <someone@123.com> wrote in message
news:x0AR9.199$JA5.101379@amsnews02.chello.com...
> I've written a program, but other programs don't update the PATH variable.
>
> I've tried both SendMessageTimeOut and SendMessage.
> After a restart of the computer the settings are processed good.
> But not after the SendMessageTimeOut command.
>
> What parameters should I pass to the SendMessageTimeOut function to notify
> all programs the PATH has changed ?
> I can't understand the help from the MSDN library.  : /
>
> What is wrong ?
>
> Happy newyear,
> 0BIT
>
>
>
> program add_path;
>
> {
> Written for Delphi 7
> }
>
> uses
>   Windows, Registry, Messages;
>
> {$APPTYPE CONSOLE}
> {$R *.res}
>
> const
>  pth_key = '\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment\';
>  pth_key_name = 'Path';
>
> var
>  pth_value : string;
>  newpth : string;
>  r : string;
>  t : integer;
>  alreadyadded : boolean;
>  result : cardinal;
>
> function GetRegistryValue(KeyName: string; Key : string): string;
>  var
>   Registry: TRegistry;
>  begin
>   Registry := TRegistry.Create(KEY_READ);
>   try
>    Registry.RootKey := HKEY_LOCAL_MACHINE;
>    // False because we do not want to create it if it doesn't exist
>    Registry.OpenKey(KeyName, False);
>    Result := Registry.ReadString(Key);
>   finally
>    Registry.Free;
>   end;
>  end;
>
> Procedure SetRegistryValue(KeyName: string; Key : string; Value : string);
>  var
>   Registry: TRegistry;
>  begin
>   Registry := TRegistry.Create(KEY_WRITE);
>   try
>    Registry.RootKey := HKEY_LOCAL_MACHINE;
>    // False because we do not want to create it if it doesn't exist
>    Registry.OpenKey(KeyName, True{False});
>    Registry.WriteString(Key,Value);
>    Registry.CloseKey;
>   finally
>    Registry.Free;
>   end;
>  end;
>
> Function OnePath(p : string; nr : integer; var r : string) : boolean;
>  var
>   t,t2 : integer;
>  begin
>   t:=1;
>   t2:=1;
>   r:='';
>   while (t2<nr) and (t<=Length(p)) do begin
>    if p[t]=';' then
>     inc(t2);
>    inc(t);
>   end;
>   if t2=nr then begin
>    OnePath:=True;
>    while (t<=Length(p)) and (p[t]<>';') do begin
>     r:=r+p[t];
>     inc(t);
>    end;
>   end else
>    OnePath:=False;
>  end;
>
> begin
>  SetLength(pth_value,5000);
>  SetLength(r,5000);
>  SetLength(newpth,5000);
>
>  {SetRegistryValue(pth_key,'temptest','Hallo2');}
>
> { WriteLn(ParamStr(1));}
>  if Length(ParamStr(1))<>0 then begin
>   pth_value:=GetRegistryValue(pth_key,pth_key_name);
> {  WriteLn(pth_value);}
>
>   t:=1;
>   newpth:='';
>   alreadyadded:=false;
>   while OnePath(pth_value,t,r) do begin
>    if r=ParamStr(1) then
>     alreadyadded:=true;
>    if t>=2 then
>     newpth:=newpth+';';
>    newpth:=newpth+r;
> {   WriteLn(r);}
>    inc(t);
>   end;
>
>   if not alreadyadded then begin
>    if newpth='' then
>     newpth:=ParamStr(1)
>    else
>     newpth:=ParamStr(1)+';'+newpth;
>    SetRegistryValue(pth_key,pth_key_name,newpth);
>    WriteLn('New path: ',newpth);
>   end else begin
>    WriteLn('No changes made to registry');
>    WriteLn('Path registry key: ',pth_value);
>   end;
>
>    {Send message...to update programs}
>    Result:=0;
>
>
WriteLn(SendMessageTimeOut(HWND_BROADCAST,WM_SETTINGCHANGE,0,0,0,15000000,Re
> sult));
>    WriteLn(Result);
> {   WriteLn(SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0));}
>
>  end else begin
>   WriteLn('Usage: add_path "directory"');
>  end;
>
>   ReadLn;
> end.
>
>
Re: System Variable PATH
#1724
Author: Kelly Leahy
Date: Sat, 04 Jan 2003 20:10
158 lines
5515 bytes
You are using HWND_BROADCAST as the first parameter aren't you?

You might want to use PostMessage if you don't want to wait for the
top-level windows to process the message.

Kelly

"0BIT" <someone@123.com> wrote in message
news:x0AR9.199$JA5.101379@amsnews02.chello.com...
> I've written a program, but other programs don't update the PATH variable.
>
> I've tried both SendMessageTimeOut and SendMessage.
> After a restart of the computer the settings are processed good.
> But not after the SendMessageTimeOut command.
>
> What parameters should I pass to the SendMessageTimeOut function to notify
> all programs the PATH has changed ?
> I can't understand the help from the MSDN library.  : /
>
> What is wrong ?
>
> Happy newyear,
> 0BIT
>
>
>
> program add_path;
>
> {
> Written for Delphi 7
> }
>
> uses
>   Windows, Registry, Messages;
>
> {$APPTYPE CONSOLE}
> {$R *.res}
>
> const
>  pth_key = '\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment\';
>  pth_key_name = 'Path';
>
> var
>  pth_value : string;
>  newpth : string;
>  r : string;
>  t : integer;
>  alreadyadded : boolean;
>  result : cardinal;
>
> function GetRegistryValue(KeyName: string; Key : string): string;
>  var
>   Registry: TRegistry;
>  begin
>   Registry := TRegistry.Create(KEY_READ);
>   try
>    Registry.RootKey := HKEY_LOCAL_MACHINE;
>    // False because we do not want to create it if it doesn't exist
>    Registry.OpenKey(KeyName, False);
>    Result := Registry.ReadString(Key);
>   finally
>    Registry.Free;
>   end;
>  end;
>
> Procedure SetRegistryValue(KeyName: string; Key : string; Value : string);
>  var
>   Registry: TRegistry;
>  begin
>   Registry := TRegistry.Create(KEY_WRITE);
>   try
>    Registry.RootKey := HKEY_LOCAL_MACHINE;
>    // False because we do not want to create it if it doesn't exist
>    Registry.OpenKey(KeyName, True{False});
>    Registry.WriteString(Key,Value);
>    Registry.CloseKey;
>   finally
>    Registry.Free;
>   end;
>  end;
>
> Function OnePath(p : string; nr : integer; var r : string) : boolean;
>  var
>   t,t2 : integer;
>  begin
>   t:=1;
>   t2:=1;
>   r:='';
>   while (t2<nr) and (t<=Length(p)) do begin
>    if p[t]=';' then
>     inc(t2);
>    inc(t);
>   end;
>   if t2=nr then begin
>    OnePath:=True;
>    while (t<=Length(p)) and (p[t]<>';') do begin
>     r:=r+p[t];
>     inc(t);
>    end;
>   end else
>    OnePath:=False;
>  end;
>
> begin
>  SetLength(pth_value,5000);
>  SetLength(r,5000);
>  SetLength(newpth,5000);
>
>  {SetRegistryValue(pth_key,'temptest','Hallo2');}
>
> { WriteLn(ParamStr(1));}
>  if Length(ParamStr(1))<>0 then begin
>   pth_value:=GetRegistryValue(pth_key,pth_key_name);
> {  WriteLn(pth_value);}
>
>   t:=1;
>   newpth:='';
>   alreadyadded:=false;
>   while OnePath(pth_value,t,r) do begin
>    if r=ParamStr(1) then
>     alreadyadded:=true;
>    if t>=2 then
>     newpth:=newpth+';';
>    newpth:=newpth+r;
> {   WriteLn(r);}
>    inc(t);
>   end;
>
>   if not alreadyadded then begin
>    if newpth='' then
>     newpth:=ParamStr(1)
>    else
>     newpth:=ParamStr(1)+';'+newpth;
>    SetRegistryValue(pth_key,pth_key_name,newpth);
>    WriteLn('New path: ',newpth);
>   end else begin
>    WriteLn('No changes made to registry');
>    WriteLn('Path registry key: ',pth_value);
>   end;
>
>    {Send message...to update programs}
>    Result:=0;
>
>
WriteLn(SendMessageTimeOut(HWND_BROADCAST,WM_SETTINGCHANGE,0,0,0,15000000,Re
> sult));
>    WriteLn(Result);
> {   WriteLn(SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0));}
>
>  end else begin
>   WriteLn('Usage: add_path "directory"');
>  end;
>
>   ReadLn;
> end.
>
>
Re: System Variable PATH
#1725
Author: 0BIT
Date: Sun, 05 Jan 2003 12:25
185 lines
6316 bytes
"Kelly Leahy" <kellyleahy@nospam.swbell.net> wrote in message
news:kdHR9.133$n34.131989714@newssvr11.news.prodigy.com...
> You are using HWND_BROADCAST as the first parameter aren't you?

Yes

SendMessageTimeOut(HWND_BROADCAST,WM_SETTINGCHANGE,0,0,0,15000000,Result)


I want to change the path, and I looked at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas
e/environment_variables.asp

It says I must send a WM_SETTINGCHANGE message. My program does change the
registry correctly.
But when my program is done and then start cmd.exe the path var is not
changed.
When in XP i change the environment variable path (My Computer | Properties
| Advanced | Environment Variables), it does in fact change the path when
cmd.exe is run. (Not when cmd is already opened but that doesn't matter)

>
> You might want to use PostMessage if you don't want to wait for the
> top-level windows to process the message.
>
> Kelly
>
> "0BIT" <someone@123.com> wrote in message
> news:x0AR9.199$JA5.101379@amsnews02.chello.com...
> > I've written a program, but other programs don't update the PATH
variable.
> >
> > I've tried both SendMessageTimeOut and SendMessage.
> > After a restart of the computer the settings are processed good.
> > But not after the SendMessageTimeOut command.
> >
> > What parameters should I pass to the SendMessageTimeOut function to
notify
> > all programs the PATH has changed ?
> > I can't understand the help from the MSDN library.  : /
> >
> > What is wrong ?
> >
> > Happy newyear,
> > 0BIT
> >
> >
> >
> > program add_path;
> >
> > {
> > Written for Delphi 7
> > }
> >
> > uses
> >   Windows, Registry, Messages;
> >
> > {$APPTYPE CONSOLE}
> > {$R *.res}
> >
> > const
> >  pth_key = '\SYSTEM\CurrentControlSet\Control\Session
> Manager\Environment\';
> >  pth_key_name = 'Path';
> >
> > var
> >  pth_value : string;
> >  newpth : string;
> >  r : string;
> >  t : integer;
> >  alreadyadded : boolean;
> >  result : cardinal;
> >
> > function GetRegistryValue(KeyName: string; Key : string): string;
> >  var
> >   Registry: TRegistry;
> >  begin
> >   Registry := TRegistry.Create(KEY_READ);
> >   try
> >    Registry.RootKey := HKEY_LOCAL_MACHINE;
> >    // False because we do not want to create it if it doesn't exist
> >    Registry.OpenKey(KeyName, False);
> >    Result := Registry.ReadString(Key);
> >   finally
> >    Registry.Free;
> >   end;
> >  end;
> >
> > Procedure SetRegistryValue(KeyName: string; Key : string; Value :
string);
> >  var
> >   Registry: TRegistry;
> >  begin
> >   Registry := TRegistry.Create(KEY_WRITE);
> >   try
> >    Registry.RootKey := HKEY_LOCAL_MACHINE;
> >    // False because we do not want to create it if it doesn't exist
> >    Registry.OpenKey(KeyName, True{False});
> >    Registry.WriteString(Key,Value);
> >    Registry.CloseKey;
> >   finally
> >    Registry.Free;
> >   end;
> >  end;
> >
> > Function OnePath(p : string; nr : integer; var r : string) : boolean;
> >  var
> >   t,t2 : integer;
> >  begin
> >   t:=1;
> >   t2:=1;
> >   r:='';
> >   while (t2<nr) and (t<=Length(p)) do begin
> >    if p[t]=';' then
> >     inc(t2);
> >    inc(t);
> >   end;
> >   if t2=nr then begin
> >    OnePath:=True;
> >    while (t<=Length(p)) and (p[t]<>';') do begin
> >     r:=r+p[t];
> >     inc(t);
> >    end;
> >   end else
> >    OnePath:=False;
> >  end;
> >
> > begin
> >  SetLength(pth_value,5000);
> >  SetLength(r,5000);
> >  SetLength(newpth,5000);
> >
> >  {SetRegistryValue(pth_key,'temptest','Hallo2');}
> >
> > { WriteLn(ParamStr(1));}
> >  if Length(ParamStr(1))<>0 then begin
> >   pth_value:=GetRegistryValue(pth_key,pth_key_name);
> > {  WriteLn(pth_value);}
> >
> >   t:=1;
> >   newpth:='';
> >   alreadyadded:=false;
> >   while OnePath(pth_value,t,r) do begin
> >    if r=ParamStr(1) then
> >     alreadyadded:=true;
> >    if t>=2 then
> >     newpth:=newpth+';';
> >    newpth:=newpth+r;
> > {   WriteLn(r);}
> >    inc(t);
> >   end;
> >
> >   if not alreadyadded then begin
> >    if newpth='' then
> >     newpth:=ParamStr(1)
> >    else
> >     newpth:=ParamStr(1)+';'+newpth;
> >    SetRegistryValue(pth_key,pth_key_name,newpth);
> >    WriteLn('New path: ',newpth);
> >   end else begin
> >    WriteLn('No changes made to registry');
> >    WriteLn('Path registry key: ',pth_value);
> >   end;
> >
> >    {Send message...to update programs}
> >    Result:=0;
> >
> >
>
WriteLn(SendMessageTimeOut(HWND_BROADCAST,WM_SETTINGCHANGE,0,0,0,15000000,Re
> > sult));
> >    WriteLn(Result);
> > {   WriteLn(SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0));}
> >
> >  end else begin
> >   WriteLn('Usage: add_path "directory"');
> >  end;
> >
> >   ReadLn;
> > end.
> >
> >
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads