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:

  1. Log out of the application.

  2. Log into the SYSPROG application.

  3. Add a row, (call it DLL_APICALLS_KERNEL32), with the first line as KERNEL32 and containing the declarations as shown below.

    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().

  4. Save  the row.
  5. Run Declare_FCNS at the System Editor Exec Line to create the declaration header, as shown below:

    RUN DECLARE_FCNS 'DLL_APICALLS_KERNEL32'
  6. Exit the editor.
  7. Log out of SYSPROG.
  8. Log into your application.
  9. Run the window.
  • No labels