Recent

Author Topic: Using Environment Variables (preferably Windows)  (Read 21182 times)

WickedDum

  • Full Member
  • ***
  • Posts: 211
Using Environment Variables (preferably Windows)
« on: October 27, 2016, 01:32:05 am »
Greetings!

Being told to use the directories that Windows dictates, sent me on a journey.  (Due credit to Leonard Nimoy) I went In Search of... how to use the variables and realized I should know what all of the variables are to avail myself of them in the future.  I have compiled a list of Environment Variables for Windows and flavors of UNIX.  I would like to get it checked for accuracy/completeness.  How might I accomplish that?

I believe the list is fairly complete, BUT:  I still don't know how to use them!!

I have found multiple discussions/examples if I were using the command line, but I have resigned myself to using the GUI.

Any and all assistance would be GREATLY appreciated!!!

Thank you!

« Last Edit: November 08, 2016, 02:38:10 am by WickedDum »
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Using Environment Variables (preferably Windows)
« Reply #1 on: October 27, 2016, 01:39:54 am »
To see what environment variables are defined on any system, open a command prompt window (Windows) or terminal window (OS X, Linux) and enter set. That will display PATH, etc. settings.

For example, to get the PATH in order to do something with it in your program, use GetEnvironmentVariable('PATH') .

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Using Environment Variables (preferably Windows)
« Reply #2 on: October 27, 2016, 02:05:40 am »
How long is a piece of string?
Any user anywhere can define his/her own environment variable.

To see the environment on your machine, Lazarus provides a simple listing.
View -> IDE Internals -> About IDE, the General tab has a section headed
  Environment variables:

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Using Environment Variables (preferably Windows)
« Reply #3 on: October 27, 2016, 02:48:58 am »
Being told to use the directories that Windows dictates, sent me on a journey.

When dealing with Microsoft-dictated folders, you should be using Win32 APIs that are specialized to return those specific paths directly (SHGetFolderPath(), SHGetKnownFolderPath(), etc).  Don't use environment variables for them.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Using Environment Variables (preferably Windows)
« Reply #4 on: October 27, 2016, 05:49:31 am »
Thank you!

@Phil - I didn't realize that all of the environment variables were set by default.

@howardpc - About this long:
GetEnvironmentVariable('PATH') is what I was looking for!!  Thanks!

@Remy Lebeau - Win32 APIs...even on a 64bit system?  How do you use them (SHGetFolderPath(), SHGetKnownFolderPath(), etc)?

Thank you for your input!!!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Using Environment Variables (preferably Windows)
« Reply #5 on: October 27, 2016, 06:11:38 am »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Using Environment Variables (preferably Windows)
« Reply #6 on: October 27, 2016, 03:24:04 pm »
Good morning!

I read some of the documentation.  (There are a couple of links you missed... ;)).  Because the examples are in C++, I asked, "How do you use them..."  I probably should have said, "How are they translated into FPC?"  I just figured I would stick with Pascal...one language at a time.  %)

Besides, if I try to use generic terms to assist in cross-compiling, those Windows functions wouldn't work, would they?

I thought FPC would have the capability.  And...it does! GetWindowsSpecialDir().  Except...I still have to resort to Windows-specific input for that function.

Thanks!

« Last Edit: October 27, 2016, 03:34:33 pm by WickedDum »
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Using Environment Variables (preferably Windows)
« Reply #7 on: October 27, 2016, 03:52:38 pm »
The languages are so similar that the msdn documentation (very basic) should be enough. We all - well, all reasonably experienced programmers, do that. Both ways ;)
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Using Environment Variables (preferably Windows)
« Reply #8 on: October 27, 2016, 04:22:14 pm »
The RTL has several cross-platform functions related to environment variables including these:
  • GetEnvironmentVariableCount
  • GetTempDir
  • GetTempFileName
  • GetAppConfigDir
  • GetUserDir

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Using Environment Variables (preferably Windows)
« Reply #9 on: October 27, 2016, 06:12:33 pm »
Thanks Guys!

During my readings, I ran across an MSDN page that showed hex values for the CSLID calls.  Now, when I need it, I can't find it... :(   Anyone have that info?  It would tell me what works and what doesn't.   %)

@howardpc - Just what I needed.  I am playing around with GetWindowsSpecialDir() and GetEnvironmentVariable() right now - trying to learn which does what.

@Thaddy - Well...  :D if I was a "reasonably experienced programmer", I wouldn't be posting in a Beginners forum...  ;D 

On a personal note:  I've never know MS to do anything basic.  Not even with DOS1.1!!

It's so nice that Microsoft keeps changing everything, isn't it?   :D

Thanks, again!!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Using Environment Variables (preferably Windows)
« Reply #10 on: October 27, 2016, 10:18:36 pm »
It's so nice that Microsoft keeps changing everything, isn't it?   :D

You mean the changes they made 20 years ago, right?


WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Using Environment Variables (preferably Windows)
« Reply #11 on: October 28, 2016, 07:02:27 am »
 :P :P :P :P :P :P :P :P :P

They are still changing things:  Look and feel of their OS's and apps, such as Office, Visual Studio, Serverware, etc., etc.    :D

Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Using Environment Variables (preferably Windows)
« Reply #12 on: October 28, 2016, 10:00:06 am »
Quote
@Thaddy - Well...  :D if I was a "reasonably experienced programmer", I wouldn't be posting in a Beginners forum...  ;D
My comment is meant to encourage you to try to gain the needed experience....
msdn C examples are really easy to follow.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Using Environment Variables (preferably Windows)
« Reply #13 on: October 28, 2016, 10:48:13 pm »
My comment is meant to encourage you to try to gain the needed experience....
msdn C examples are really easy to follow.

And C is very easy to translate to Pascal.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Using Environment Variables (preferably Windows)
« Reply #14 on: October 31, 2016, 04:55:19 am »
This post should probably change from ENVIRONMENT variables to CSIDL constants...  But they both fall under locating 'system independent' values.  :D

If it is recommended not to use the CSIDL constants, then why are they required as an input to GetKnownFolderPath or SHGetSpecialFolderPath or some of the other API routines?  %)

What am I missing here:? :-[
Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., SHFolder, ShlObj, Windows,  ...;
  3.  
  4. function GetKnownFolderPath(const folder : KNOWNFOLDERID) : string;
  5.  
  6. var
  7.   path: array [0..MAX_PATH] of char;
  8.  
  9. begin
  10.   if SUCCEEDED(SHGetKnownFolderPath(folder, 0, 0, path)) then
  11.   begin
  12.     try
  13.       Result := path;
  14.     finally
  15.       CoTaskMemFree(path);
  16.     end;
  17.   end else
  18.     Result := '';
  19. end;
  20.  
  21. dir_programdata:= GetKnownFolderPath(FOLDERID_RoamingAppData);

I try to correct something that won't compile and something else jumps up...

Thanks!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

 

TinyPortal © 2005-2018