Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (4)


Reading and Writing System-Wide Environment Variables.Format this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
System
Skill Level:
Scoring:
Last Update:
04/06/2004
Search Keys:
delphi delphi3000 article borland vcl code-snippet environment-variable registry GetEnvironmentVariable environment variable SetEnvironmentVariable
Times Scored:
5
Visits:
4543
Uploader: Richard Winston
Company:
Reference: N/A
 
Question/Problem/Abstract:
How do you set an environment variable that will apply outside the process that set the variable or those spawned by it?
Answer:



On Windows 2000, if you open the control panel and double click on
the system icon, the system properties dialog box will open.  On the
"Advanced" tab, you can click the "Environment Variables" tab to see
a list of the user and system environment variables. The procedures
and functions below allow you to read and write those variables.

It is worth mentioning that you can also use "GetEnvironmentVariable"
and "SetEnvironmentVariable" to read and write environment variables.  
However, if you set and environment variable with
"SetEnvironmentVariable", the value you set applies only to the
process that called "SetEnvironmentVariable" or are spawned by it.

The first two procedures read and write environment variables for the
current user.

function GetUserEnvironmentVariable(const name: string): string;
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_CURRENT_USER;
    OpenKey('Environment', False);
    result := ReadString(name);
  finally
    Free
  end
end;

procedure SetUserEnvironmentVariable(const name, value: string);
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_CURRENT_USER;
    OpenKey('Environment', False);
    WriteString(name, value);
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LParam
      (PChar('Environment')), SMTO_ABORTIFHUNG, 5000, rv);
  finally
    Free
  end
end;


The next two procedures read and write environment variables for the
system and thus affect all users.

function GetSystemEnvironmentVariable(const name: string): string;
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_LOCAL_MACHINE;
    OpenKey('SYSTEM\CurrentControlSet\Control\Session ' +
      'Manager\Environment', False);
    result := ReadString(name);
  finally
    Free
  end
end;

// Modified from
// http://www.delphiabc.com/TipNo.asp?ID=117
// The original article did not include the space in
// "Session Manager" which caused the procedure to fail.

procedure SetSystemEnvironmentVariable(const name, value: string);
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_LOCAL_MACHINE;
    OpenKey('SYSTEM\CurrentControlSet\Control\Session ' +
      'Manager\Environment', False);
    WriteString(name, value);
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LParam
      (PChar('Environment')), SMTO_ABORTIFHUNG, 5000, rv);
  finally
    Free
  end
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Oops
    Richard Winston (Apr 5 2004 5:28PM)

For some reason, when I edit the article, I can't include the last function.  Here it is:

// Modified from
// http://www.delphiabc.com/TipNo.asp?ID=117
// The original article did not include the space in
// "Session Manager" which caused the procedure to fail.

procedure SetSystemEnvironmentVariable(const name, value: string);
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_LOCAL_MACHINE;
    OpenKey('SYSTEM\CurrentControlSet\Control\Session ' +
      'Manager\Environment', False);
    WriteString(name, value);
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LParam
      (PChar('Environment')), SMTO_ABORTIFHUNG, 5000, rv);
  finally
    Free
  end
end;
Respond

expanding variables
    Gerald McNicholl (Apr 5 2004 1:10PM)

Hi,

Quick question. The functions above return the unmodified paths. ie. temp='%systemroot%\temp'. How can you expland the %systemroot% variable to it's full path, ie. C:\Winnt ?
Respond

RE: expanding variables
Richard Winston (Apr 5 2004 5:00PM)

Search the Delphi Super Page for "SystemRoot".

(http://delphi.icm.edu.pl/).
Respond

RE: expanding variables
Richard Winston (Apr 6 2004 4:07PM)

See also: http://www.delphi3000.com/articles/article_3942.asp#comments
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
R. Lefter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)