Recent

Author Topic: Adding new environment variables to windows  (Read 7482 times)

Peiman

  • New Member
  • *
  • Posts: 41
Adding new environment variables to windows
« on: December 25, 2010, 08:46:34 am »
Hi!

It is very easy to add env vars manually in windows. But in programming, I don't know how to do this. APIs like ExpandEnvironmentStrings only works with existing variables. How can I make new variable? Do I need working with TRegistry?

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: Adding new environment variables to windows
« Reply #1 on: December 25, 2010, 11:05:36 am »
Quote
Do I need working with TRegistry?
Yes. The user variables are stored in HKCU\Environment and the system variables in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Gr.
Dirk.

Peiman

  • New Member
  • *
  • Posts: 41
Re: Adding new environment variables to windows
« Reply #2 on: December 25, 2010, 01:52:50 pm »
Thanks,

How should I use TRegistry? I used the code below, but nothing happens (because the openkey always returns false)

Code: [Select]
const Env = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment\';
// I used different values for this string with and without "\" in the beginning and end. 

...

  try
  myreg := TRegistry.Create;
  myReg.RootKey := HKEY_LOCAL_MACHINE;
  if myReg.OpenKey(Env, false) then 
    begin
    myReg.WriteInteger('hundred',100);
    myReg.WriteInteger('thousand',1000);
    end;
  finally
    myReg.Free;
  end;         
...


typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Adding new environment variables to windows
« Reply #3 on: December 25, 2010, 02:02:29 pm »
It returns false because the key does not exist. To create the key if it does not exist, use:

Code: [Select]
if myReg.OpenKey(Env, True) then

Peiman

  • New Member
  • *
  • Posts: 41
Re: Adding new environment variables to windows
« Reply #4 on: December 26, 2010, 06:04:06 pm »
I forget to say that there is no difference in the result if I use "true" in openkey. In addition, the createkey method raise an "unknown" error. Anyway, the key (in the Env string) exists for sure! So the problem is not related to it existence. I think there is something missing in my code, or in the fpc which denies access to registry. Or is it a bug of fpc 2.4.3?

 

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Adding new environment variables to windows
« Reply #5 on: December 26, 2010, 06:44:12 pm »
This code should work:

Code: [Select]
var
   Registry :TRegistry;
begin
   Registry := TRegistry.Create;
   try
     Registry.RootKey := HKEY_LOCAL_MACHINE;
     if Registry.OpenKey('Software\Lazarus\Test', True) then
       ShowMessage('OK');
    finally
      Registry.Free;
    end;
end;   

Lazarus 0.9.29 r28656 FPC 2.5.1 i386-win32-win32/win64

Peiman

  • New Member
  • *
  • Posts: 41
Re: Adding new environment variables to windows
« Reply #6 on: December 27, 2010, 10:38:35 am »
Sorry! typo.

It still doesn't work (using 0.9.31 with fpc 2.5.1 win64). openkey returns false regardless of what its second argument be. I think it is related to access permissions. because it worked with HKEY_CURRENT_USER.

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: Adding new environment variables to windows
« Reply #7 on: December 27, 2010, 10:42:36 am »
Quote
I think it is related to access permissions. because it worked with HKEY_CURRENT_USER.
That's easy to test: just run your program as administrator.

Gr.
Dirk.

 

TinyPortal © 2005-2018