1. Gimmy Susan
  2. PowerBuilder
  3. Monday, 11 July 2022 12:26 PM UTC

Good morning community
I need to set an environment variable and read it later.


I found: www://https://codeverge.com/sybase.powerbuilder.general/creating-setting-environment-vari/954332

 

I try to set it:

FUNCTION boolean SetEnvironmentVariableA (string szName, string szValue) LIBRARY "kernel32.dll" boolean lb_ret
lb_ret = SetEnvironmentVariableA ("Key", "Value")

 

If I try to read it:

ContextKeyword lcxk_base
string ls_Path
string ls_values ​​[]
GetContextService ("Keyword", lcxk_base)
GetContextService ("ContextKeyword", lcxk_base)
lcxk_base.GetContextKeywords ("Keyword", ls_values)
IF Upperbound (ls_values)> 0 THEN
ls_Path = ls_values ​​[1]
ELSE
ls_Path = "* UNDEFINED *"
END IF

 

The result is always "* UNDEFINED *"


Any suggestions ?

Thanks in advance for the help.

Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Monday, 11 July 2022 12:57 PM UTC
  2. PowerBuilder
  3. # Permalink

Seems that context keyword service can't read the environment variables your write with SetEnvironmentVariable.

In documentation for SetEnvironmentVariable you will find: "This function has no effect on the system environment variables or the environment variables of other processes." Maybe this is the reason? I don't know.

What will work:

 

FUNCTION boolean SetEnvironmentVariableW (string szName, string szValue) LIBRARY "kernel32.dll"
FUNCTION long GetEnvironmentVariableW (string szName, ref string szValue, long size) LIBRARY "kernel32.dll"

// set a value
SetEnvironmentVariableW ("Key", "Value")

// read a value
ls_value = Space (1000)
GetEnvironmentVariableW ("Key", ls_value, 1000)

Comment
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Tuesday, 12 July 2022 09:34 AM UTC
  2. PowerBuilder
  3. # 1

If you want to set an environment variable that "sticks" on the system, not just for the session of your running app, then you might want to consider doing a 

Run("SETX ......).

regards,

MiguelL

Comment
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Monday, 11 July 2022 16:23 PM UTC
  2. PowerBuilder
  3. # 2

Also of note, SetEnvironmentVariable only updates the process' copy of the environment. It does not update the system environment.

Comment
There are no comments made yet.
René Ullrich Accepted Answer Pending Moderation
  1. Monday, 11 July 2022 12:40 PM UTC
  2. PowerBuilder
  3. # 3

Hi Gimmy,

SetEnvironmentVariableA ist for Ascii, you should better user SetEnvironmentVariableW.

in your example you set the Variable "Key" but read the variable "Keyword"!

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.