Writing an Environment Variable

The function to write an environment variable is SetEnvironmentVariable(). The code below will create an environment variable called DataPath, representing the path to the application's data. The function will set it to C:\Data\. Paste the code below in the CLICK Event of a button in a window:

declare function SetEnvironmentVariable

key = 'DataPath'

value = 'C:\Data\'

rv = SetEnvironmentVariable ( key , value)

The values of the key and value are, of course, known before the call, so there is no need to predetermine the buffer size. Only one call is necessary.

The Windows API Declaration

The code above will not run until the declaration for SetEnvironmentVariable() has been added. To add the declarations, do the following:

KERNEL32
LONG STDCALL SetEnvironmentVariableA(LPCHAR, LPCHAR) As SetEnvironmentVariable
//....add any other declarations in KERNEL32 here.....

The function is aliased to its the ANSI version SetEnvironmentVariableA().

RUN DECLARE_FCNS 'DLL_APICALLS_KERNEL32'