TSE: Feature: Request: Set environment variable

13 views
Skip to first unread message

knud van eeden

unread,
Mar 23, 2023, 11:36:40 PM3/23/23
to SemWare TSE Pro Text Editor
Hello,

1. To get the value of a Microsoft Windows environment variable one can use the existing native TSE keyword:

 GetEnvStr()

E.g. this works and compiles OK in TSE:

 PROC Main()
  Warn( GetEnvStr( "COMPUTERNAME" ) )
 END

2. But is there / there is not already a TSE keyword to SET a Microsoft Windows environment variable?

E.g. something like:

 SetEnvStr( <name>, <value> )

E.g.

 SetEnvStr( "FOOBAR", "123" )

E.g. something like (but that does not work yet thus)

 PROC Main()
  SetEnvStr( "FOOBAR", "123" )
 END

Note: Use case e.g. when compiling passing environment variables (e.g. sc32.exe &fn& %TSE_MACRO_DIR% >  &output&;), e.g. passing a databasename, username, password, ... but then one first have to set these inside TSE.

Note: I know about the DLL method to set the environment variable, but having a single keyword like SetEnvStr() is / would be a much cleaner solution.

Thanks in advance
with friendly greetings
Knud van Eeden


knud van eeden

unread,
Mar 23, 2023, 11:41:41 PM3/23/23
to SemWare TSE Pro Text Editor
E.g. this is currently the only way to perform that:

dll "<kernel32.dll>"
 integer proc SetEnvironmentVariable(
 string name:cstrval,    // environment variable name
 string value:cstrval)   // value
 : "SetEnvironmentVariableA"
end
//
 PROC Main()
  SetEnvironmentVariable("a", "10")
  //
  Dos( "SET a" )
  //
 END


Jose Adriano Baltieri

unread,
Mar 24, 2023, 9:12:50 AM3/24/23
to sem...@googlegroups.com
Should you have 4NT/TCC/Take Cmd you can Run ( Dos() ) it's SET cmd. It allows you to set it externally to TSE.

But I know, it's not a direct method.

--

---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/semware/1238599319.75064.1679628994188%40mail.yahoo.com.


--


Grato,
Jose Adriano Baltieri

knud van eeden

unread,
Mar 24, 2023, 9:25:03 AM3/24/23
to sem...@googlegroups.com
Yes, that also came up afterwards as a possibility, even without JPSoft, like

Dos( "SET" + " " + name + "=" + value )

but as there thus already exists a GetVarStr() but no similar SetVarStr() it 
is thus a missing keyword e.g.

Jose Adriano Baltieri

unread,
Mar 24, 2023, 9:29:13 AM3/24/23
to sem...@googlegroups.com
On Fri, Mar 24, 2023 at 10:25 AM 'knud van eeden' via SemWare TSE Pro text editor <sem...@googlegroups.com> wrote:
Yes, that also came up afterwards as a possibility, even without JPSoft, like

Dos( "SET" + " " + name + "=" + value )


That would set only for that process individually. JpSoft's SET has a /S swith (system wide). You've got to be aware of that. 

but as there thus already exists a GetVarStr() but no similar SetVarStr() it 
is thus a missing keyword e.g.

I perfectly agree with you. Let's hope Sammy can reciprocate that function ! 

H P

unread,
Mar 24, 2023, 9:32:26 AM3/24/23
to sem...@googlegroups.com
Knud, you can use the dos command with setx or setlocal to set an environmentvariable see SetX - Set environment variables - Windows CMD - SS64.com and Setlocal - Local variables - Windows CMD - SS64.com.

Met vriendelijke groet,
With kind regards,
Muy atentamente,
Mit Freundliche Gruß,
Sinceramente,


H. Pikaar

Henri...@gmail.com



Op vr 24 mrt 2023 om 14:12 schreef Jose Adriano Baltieri <jaba...@gmail.com>:

knud van eeden

unread,
Mar 24, 2023, 9:34:46 AM3/24/23
to sem...@googlegroups.com
On Friday, March 24, 2023 at 02:25:00 PM GMT+1, knud van eeden <knud_va...@yahoo.com> wrote:


Yes, that also came up afterwards as a possibility, even without JPSoft, like

Dos( "SET" + " " + name + "=" + value )

but as there thus already exists a GetEnvStr() but no similar SetEnvStr() it 

knud van eeden

unread,
Mar 24, 2023, 9:50:28 AM3/24/23
to sem...@googlegroups.com
> Jose Adriano wrote:
> JpSoft's SET has a /S swith (system wide).

That is certainly very interesting to know, thus if 

1. no '/S' it is a temporary setting (assumed to be applied in most cases)

2. and if really having to set it permanently thus using '/S'


--

---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.

knud van eeden

unread,
Mar 24, 2023, 9:55:55 AM3/24/23
to sem...@googlegroups.com
Thanks for that. Interesting.

Carlo Hogeveen

unread,
Mar 25, 2023, 5:38:42 AM3/25/23
to sem...@googlegroups.com

 

@Jose,

Using the Dos(“set …”) command from TSE can set an environment variable in that Dos child process, but the environment variable will only exist in that child process, and not be available in the parent TSE process or other child processes that the TSE process starts.

 

@HP,

According to its documentation, “setx” can set environment variables in future command windows, but a simple test shows that that does not include Dos() child processes prom TSE.

 

The way Knud’s request reads, is that per his example he wants

to set a new environment variable (value) from TSE,

to use TSE’s Dos() function to start a child compilation process from TSE

and reference the new environment variable (value) in the child process.

 

Currently the referenced Windows API is one way to do that.

 

Carlo

 

 

Jose Adriano Baltieri

unread,
Mar 25, 2023, 7:44:58 AM3/25/23
to sem...@googlegroups.com
On Sat, Mar 25, 2023 at 6:38 AM Carlo Hogeveen <t...@ecarlo.nl> wrote:

 

@Jose,

Using the Dos(“set …”) command from TSE can set an environment variable in that Dos child process, but the environment variable will only exist in that child process, and not be available in the parent TSE process or other child processes that the TSE process starts.


I'm talking about calling JpSoft (4NT/TCC/Take Command), since that command line processor DOES HAVE a flag for setting external (system) environment variables. This flag is "/S" for System of course.

Follow a little macro. I've compiled and runned it inside Tessie. Later on I opened another TCC Window (process) and the setting was there.

Of course this method is indirect and depends on having TCC installed. Best would be to have an internal function, as the original post requests.

Images show :

The Macro:

 
ScreenClip_1 [2].png

Another Window (process) opened right after. You can see the Settings effectively there.

ScreenClip_2 [3].png

 

 

@HP,

According to its documentation, “setx” can set environment variables in future command windows, but a simple test shows that that does not include Dos() child processes prom TSE.

 

The way Knud’s request reads, is that per his example he wants

to set a new environment variable (value) from TSE,

to use TSE’s Dos() function to start a child compilation process from TSE

and reference the new environment variable (value) in the child process.

 

Currently the referenced Windows API is one way to do that.

 

Carlo

 

 

--

---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.

Carlo Hogeveen

unread,
Mar 25, 2023, 8:21:09 AM3/25/23
to sem...@googlegroups.com

Jose,

You might be right about TCC’s capabilities, but your example is incomplete.
The request was for a/another child process of the same TSE session to be able to use it.
When you run the macro below, does the Dos() function AFTER the warning and message display the same value as the warning and message?

proc Main()
integer n = Random(1, 1000)
// Dos("Set OUTSIDE=VALUE" + Str(n))
Dos("C:\TCC\TCC.exe /c set /s OUTSIDE=" + Str(n))
Message('OUTSIDE=', n)
Warn('OUTSIDE=', n)
Dos("set OUTSIDE")
PurgeMacro(CurrMacroFilename())
end

Aside, just to be extra safe I would not use a system-wide "global" variable to communicate between a parent and child process, because then processes running at the same time might overwrite each other's variable values.

Carlo



Jose Adriano Baltieri

unread,
Mar 25, 2023, 9:39:05 AM3/25/23
to sem...@googlegroups.com
On Sat, Mar 25, 2023 at 9:21 AM Carlo Hogeveen <t...@ecarlo.nl> wrote:

Jose,

You might be right about TCC’s capabilities, but your example is incomplete.
The request was for a/another child process of the same TSE session to be able to use it.

Sorry I bypassed that requirement.
 
When you run the macro below, does the Dos() function AFTER the warning and message display the same value as the warning and message?

No. It will work for newborn processes AFTER the set. Not for child ones as you guessed (it wouldn't).
 
proc Main()
  integer n = Random(1, 1000)
  // Dos("Set OUTSIDE=VALUE" + Str(n))
  Dos("C:\TCC\TCC.exe /c set /s OUTSIDE=" + Str(n))
  Message('OUTSIDE=', n)
  Warn('OUTSIDE=', n)
  Dos("set OUTSIDE")
  PurgeMacro(CurrMacroFilename())
end

Aside, just to be extra safe I would not use a system-wide "global" variable to communicate between a parent and child process, because then processes running at the same time might overwrite each other's variable values.


Agree with you. At least there should be some sort of Critical Section to ensure exclusive access to modify those global variables.

 

 



--

---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.

knud van eeden

unread,
Mar 25, 2023, 12:32:04 PM3/25/23
to sem...@googlegroups.com
I have done the test, and Carlo is fully correct.

Only the Microsoft Windows version works as expected and passes the variable:

---
dll "<kernel32.dll>"
 integer proc SetEnvironmentVariable(
 string name:cstrval,    // environment variable name
 string value:cstrval)   // value
 : "SetEnvironmentVariableA"
end
PROC Main()
 //
 SetEnvironmentVariable("DDDTEST", "10")
 // Dos( "SET DDDTEST=20" )
 // Dos( "SET /S DDDTEST=30" )
 //
END
---

1. I use JPSoft already in Dos(), so no need to explicitly mentioning it.

2. Only 'SET' does not pass the environment variable

3. Using 'SET /S' gives the error 'Access Denied' when run from TSE

4. Thus only using the DLL version 'SetEnvironmentVariable('  does work as expected.

5. Use case: It is thus used to pass environment variable values from within TSE to to compile programs (e.g. username+password (you might not want to expose your password for example) to e.g. databases like Microsoft SQL server, Oracle, MySQL, ... for which you compile their SQL programs from within TSE). 

6. But it can be generalized to passing environment variables to any program source code compiled from within TSE thus.

7. See also: optionally:
Help about TSE compiler options:

with friendly greetings
Knud van Eeden

knud van eeden

unread,
Mar 25, 2023, 12:46:31 PM3/25/23
to sem...@googlegroups.com
FYIO:

To fully reproduce:

1. First I compiled the below TSE program which sets the 'DDDTEST' environment variable to 10.

2. I added this test to the file 'COMDATABASETRANSACT.BAT' which connects to the database

@REM version 1.0.0.0.1
@REM [kn, ri, fr, 24-03-2023 03:03:39]
@REM pass the database name as a parameter: set your database via a Microsoft environment variable externally in TSE, e.g. SET databasetransactsql=master and replace here 'master' by '%databasetransactsql%'
SET DDDTEST
@PAUSE
@ECHO filename=%3 >%6
"c:\program files\microsoft sql server\client sdk\odbc\170\tools\binn\sqlcmd.exe" -S localhost -d master -U yourusername -P yourpassword -i %3 >>%6 2>&1
@ECHO.
"c:\program files\microsoft sql server\client sdk\odbc\170\tools\binn\sqlcmd.exe" -S localhost -d master -U yourusername -P yourpassword -i %3
@ECHO.
@PAUSE

3. This batch file is called via these compile settings

Extension=.sqltransact
Description=Database: Microsoft: Transactsql: Knud
Command=COMDATABASETRANSACT.BAT &name& &ext& &fn& &drive& &path& &output&
Output=Don't Clear Screen or Prompt After Shell
 -Error=^{.*\.}$
  Error: Options=
  Error: Extra line (default empty line)=
 -FileName=^[ ]*filename={.*}$
  FileName: Options=ib
  FileName: Tag=1
 -Line=^.* Line[ ]+{[0-9]#}$
  Line: Options=
  Line: Tag=1
 -Column=
  Column: Options=
  Column: Tag=
 -Message=^{.*\.}$
  Message: Options=
  Message: Tag=0
User Macro=
------------------------------------------------------------------------------

4. So when running this helloworld.sqltransact file (from within TSE) with text

PRINT 'Hello world';

it will thus call that batch file and show the value of DDDTEST. Only the DDDTEST=10 worked thus, thus only the DLL version worked as expected.


Reply all
Reply to author
Forward
0 new messages