How to retrieve Windows Environment variables in ATEasy

Knowledge Base Article # Q200160

Read Prior Article Read Next Article
Summary Using the .NET Environment class and the GetEnvironmentVariableA function to retrieve Windows Environment variables.
There are several methods that can be used for retrieving Windows environment variables
1) Using the .NET Framework Environment class and
2) Using the Windows API GetEnvironmentVariableA() function

1) Using the .NET Framework Environment Class:

Note: If you do not have the .NET Framework installed on your computer it can be downloaded and installed for free from the Microsoft web site or by using Windows update.

To use the Environment class, first right click on the Libraries submodule in your program and select Insert Library Below. Click on the .NET Assemblies tab and scroll down to mscorlib. Expand mscorlib and scroll down to System. Expand System and scroll down to Classes. Expand Classes and check the box beside Environment. Then select Insert. ATEasy will automatically insert the Environment Class into your program. Now define a string variable (called sEnvironVar in the example below) that will be used to store the Environment variable.

! The following example will find the value of the Windows Environment variable COMPUTERNAME.
! First declare sEnvironVar as a String variable
! Get the Windows Environment variable COMPUTERNAME
sEnvironVar = Environment.GetEnvironmentVariable("COMPUTERNAME")
Print sEnvironVar   !Print COMPUTERNAME


2) Using the Windows API  GetEnvironmentVariableA() function

To use this function, first right click on the Libraries submodule in your program and select Insert Library Below. Click on the DLL tab. For the DLL Filename select kernel32.dll from the Windows\System32 folder and select Insert. ATEasy will insert the kernel32 library into your program. Then under the library procedures define a procedure:
GetEnvironmentVariableA(sName: Val String, psBuffer: Var String, dwSize: Val DWord) : Public DWord.  

When you call this function make sure to allocate the psBuffer string parameter (use SetLen).

! This example will find the value of the Windows Environment variable COMPUTERNAME
! First declare sEnvironVar as a String variable
! Allocate an appropiate amount of memory to the sEnvironVar string parameter
setlen(sEnvironVar,512)
! Get the Windows Environment variable COMPUTERNAME
GetEnvironmentVariableA("COMPUTERNAME", sEnvironVar, 512)
Print sEnvironVar    ! Print COMPUTERNAME

Article Date 4/28/2009 11:34:17 AM
Keywords ATEasy, WIndows, Environment, GetEnvironmentVariable, GetEnvironmentVariableA

0 ratings | 0 out of 5
Read Prior Article Read Next Article