Important alert: (current site time 7/16/2013 5:53:51 AM EDT)
 

article

How to get a DOS environment variable

Email
Submitted on: 7/9/2000 5:35:27 PM
By: Emiel Hollander  
Level: Intermediate
User Rating: By 3 Users
Compatibility: Delphi 5, Delphi 4, Pre Delphi 4
Views: 40650
 
     Using Delphi, you can read DOS environment variables. Here's how:

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

In the old DOS days, you could use environment variables to make certain settings available throughout the entire system. These variables are still used, but you almost can't notice it. Go to a DOS prompt and type "set" (without the quotes), then you get to see all the environment variables that have been set on your system. Examples are PATH, which contains all the paths DOS should look in to find a certain file, PROMPT, which determines how to display your DOS-prompt, and so on.

Using Delphi, you can of course read those environment variables. You do this via the Windows API function GetEnvironmentVariable. This function takes three parameters, and returns an integer. To put the environment variable PATH into the PChar EnvVar, you can use the following:

var
EnvVar: PChar;
i: integer;
begin
EnvVar := PChar(''); // initialize the PChar, otherwise you get an access
// violation error when using the
// GetEnvironmentVariable function
i := GetEnvironmentVariable(PChar('path'), EnvVar, 255);
end;

The variable EnvVar will now contain the contents of the environment variable PATH. The first parameter is, obviously, the name of the environment variable you want to get. Make sure it's a PChar, because Windows API functions can't work with strings. The second parameter is the variable you want to have the contents go to. This also has to be a PChar. And the last value is the number of characters you want copied into the variable set in the second parameter.

The integer i now contains the number of characters the function has copied into EnvVar. If you make the number of characters you want to copy too low, so that the length of the environment variable is higher than the number of characters you had told the function to copy, it copies nothing and just returns the length of the variable. So if you want to be sure that you get the entire string, you could change the function to this:

var
EnvVar: PChar;
i: integer;
begin
EnvVar := PChar('');
i := GetEnvironmentVariable(PChar('path'), EnvVar, 0);
GetEnvironmentVariable(Pchar('path'), EnvVar, i);
end;


Other 14 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/30/2001 7:45:59 AMDavid Ward

Yes, very good and correct in all respects, however, this method relies on
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.