How can one run the app with specific environment variables?

In one of my projects I am calling into a DotNet DLL - which uses System.Net.Http - which allows setting a HTTP PROXY using an env called https_proxy - how can I set this ENV for my Clarion APP so my DLL get’s it too?

I tried creating an {APP_NAME}.env file according to env_file.htm [Clarion Community Help] but it doesn’t seem to do anything

Try the SetEnvironmentVariable Windows API to set environment variables for the current process.

Friedrich

How does one do that inside the Clarion Editor?

Hi,
In your global map, you need:

  MODULE('WIN32.LIB')
     mySetEnvironmentVariable(         |
                   *CString lpName,    |
                   *CString lpValue),  |
                   LONG, RAW, PASCAL, NAME('SetEnvironmentVariableA')
  END

Then you can add a function or method like this:

SetEnvVar                  PROCEDURE (STRING pEnvVar, STRING pValue)!!,UNSIGNED
!! See: https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablea?redirectedfrom=MSDN
En       CSTRING(SIZE(CLIP(pEnvVar))+1)
Rn       CSTRING(SIZE(CLIP(pValue))+1)
  CODE
  En = CLIP(pEnvVar)
  Rn = CLIP(pValue)
  RETURN(mySetEnvironmentVariable(En,Rn))

That should do it :slight_smile:

i did it like below instead - but was hoping for a way to do it without adding code to the project - just by setting them in the Clarion Editor which will pass it through to the project while running - I thought that was what the .env file could do - but sadly it cannot

SetEnvironmentVariable PROCEDURE(STRING lpName, <STRING lpValue>)!,BOOL
envName &CSTRING
envValue &CSTRING
result BOOL
    CODE
        envName &= NEW CSTRING(LEN(CLIP(lpName)) + 1);
        envName = lpName;
        
        IF OMITTED(lpValue)
            result = _SetEnvironmentVariableA(envName);
        ELSE
            envValue &= NEW CSTRING(LEN(CLIP(lpValue)) + 1);
            envValue = lpValue;
            result = _SetEnvironmentVariableA(envName, envValue);
        END
        
        DISPOSE(envName);
        IF NOT envValue &= NULL
            DISPOSE(envName);
        END
        
        RETURN result;

Without going into whether the code does what you want, you might want to revisit your DISPOSE() calls.

Thanks - missed that one :rofl:

You can avoid the New/Dispose like this:

SetEnvironmentVariable PROCEDURE(STRING lpName, <STRING lpValue>)!,BOOL
envName  CSTRING(size(lpName)+1)
envValue CSTRING(size(lpValue)+2)
result   BOOL
    CODE
        envName = Clip(lpName)  
        IF OMITTED(lpValue)
            result = _SetEnvironmentVariableA(envName);
        ELSE
            envValue = Clip(lpValue)
            result = _SetEnvironmentVariableA(envName, envValue);
        END
        RETURN CHOOSE(result)

The API said if it worked the result is Non-Zero. You declared to return BOOL so I return CHOOSE(result).