Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing environment variables causing problems

Status
Not open for further replies.

Nordlund

Programmer
Jul 17, 2001
458
SE
Hi.
My application does some changes in the systems PATH variable using code show below, then i call BroadCastChanges. All seems to work fine. THe newly added path can be accessed from anywhere, but...

Ping, WinHlp32 etc. cannot be accessed.
It seems like the %system% variables are placed out of order.

Has anyone a solution?





Change PATH:
Code:
  with TRegistry.Create do
  try
    RootKey :=  HKEY_LOCAL_MACHINE;
    if OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment\', False) then
    begin
      if ValueExists('Path') then
      begin
        s := ReadString('Path');
          WriteString('Path', s+';'+MyNewDir);
      end;
    end;
  finally
    CloseKey;
    Free;
  end;

Broadcast Changes:
Code:
procedure BroadcastChange;
var
  lParam, wParam :Integer;
  Buf: Array[0..10] of Char;
  aResult : Cardinal;
begin
  Buf := 'Environment';
  wParam := 0;
  lParam := Integer(@Buf[0]);

  SendMessageTimeout(HWND_BROADCAST,
                     WM_SETTINGCHANGE,
                     wParam,
                     lParam,
                     SMTO_NORMAL,
                     4000,
                     aResult);

  if aResult <> 0 then
    SysErrorMessage(aResult);
end;


//Nordlund
 
Hi,

check env variables with SET command (in dos box) before and after your program.

--------------------------------------
What You See Is What You Get
 
Yeps.. The newly added variable is there, and it's working...
All other "passive" variables works. The only paths not working is the one with variables...

For example:
%SystemRoot%;
%SystemRoot%\system32;
%SystemRoot%\System32\Wbem;
does not work.

The following paths works fine:
C:\Program\Borland\Delphi7\Bin;
C:\Program\Borland\Delphi7\Projects\Bpl\;

I have to edit the path by opening the environment variable window and close it by clicking OK. Then the %SystemRoot% variables start to work...



//Nordlund
 
Hi.
Here's a (not) working example:
(After you clicked the button, start a command prompt, and try to access ping och calc).


Code:
uses
  Registry;

procedure TForm1.Button1Click(Sender: TObject);
const
  cs: PChar = 'Environment';
var
  s: String;
  lParam, wParam: Integer;
  aResult: Cardinal;
begin
  with TRegistry.Create do
  try
    RootKey :=  HKEY_LOCAL_MACHINE;
    if OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment\', False) then
    begin
      if ValueExists('Path') then
      begin
        s := ReadString('Path');
        WriteString('Path', s+';C:\Temp');
      end;
    end;
  finally
    CloseKey;
    Free;
  end;

  wParam := 0;
  lParam := LongInt(cs);

  SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, wParam, lParam, SMTO_NORMAL, 4000, aResult);

  if aResult <> 0 then
    SysErrorMessage(aResult);

  ShowMessage('Added PATH (C:\Temp) and Broadcast changes!');
end;

//Nordlund
 
weird indeed,

and what if you play with the current user profile environment (HKEY_CURRENT_USER\Environment)?

--------------------------------------
What You See Is What You Get
 
I haven't tested that, yet anyway.
The path manipulation has to be systemwide due to the files I add at the same time.

//Nordlund
 
Hi. I've tried to add a PATH in user environment.
The path worked, but when I added a %systemroot% variable, the problem popped up.... :-/

//Nordlund
 
I think you can't use the %systemroot% variable in the envirmonment strings (shoot me if I'm wrong :))
Find a way to resolve the %systemroot% var to FQN path (like 'c:\winnt') and use this path in the user environment

--------------------------------------
What You See Is What You Get
 
Yep. I can resolve the %-variables. But I don't want to manipulate the PATH too much.
This application is being redstributed to approx 3000 users at the same time. If something weird happens, i'll the man who will be shot :)

//Nordlund
 
LOL [spin]

why the need to change environment vars anyway??

--------------------------------------
What You See Is What You Get
 
Hi.
I'm trying to create an integrated Sybase OleDB install to our application.

(We are changeing the Database client on our application, that's no easy task my friends) :)

//Nordlund
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top