Quick Search for:  in language:    
DOS,Using,Delphi,read,environment,variables,H
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Delphi Stats

 Code: 209,911. lines
 Jobs: 14. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Delphi.
Record From Microphone to Wave File
By Una-Rat on 11/25


List Manager
By james hill on 11/17


Click here to see a screenshot of this code!Skin app
By Jonathan Curry on 11/15

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

How to get a DOS environment variable

Print
Email
 

Submitted on: 7/9/2000 5:35:27 PM
By: Emiel Hollander  
Level: Intermediate
User Rating: By 2 Users
Compatibility:Delphi 5, Delphi 4, Pre Delphi 4

Users have accessed this article 13825 times.
 
 
     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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:David Ward
Yes, very good and correct in all respects, however, this method relies on
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Delphi Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.