Page 1 of 2

Set/GetEnvironmentVariable()

Posted: Mon Feb 10, 2014 4:25 pm
by UUICEO
My Question is can a program that is run by the RunProgram() command use the SetEnvironmentVariable() command to return information back to the program that originally ran it? I'm using the following code which does not seem to work. The first code I'm running in the IDE the second code I have compiled to .exe and run using the first code.

Code: Select all

RunProgram("E:\Old Computer\PureBasic\_backup\ClockToc.exe")
SetEnvironmentVariable("CLOCKGOES", "TIC")
Debug GetEnvironmentVariable("CLOCKGOES")

Repeat
	If GetEnvironmentVariable("CLOCKGOES") = "TOC"
		Debug GetEnvironmentVariable("CLOCKGOES")
		SetEnvironmentVariable("CLOCKGOES", "TIC")
	EndIf
ForEver

Code: Select all

Repeat
	If GetEnvironmentVariable("CLOCKGOES") = "TIC"
		SetEnvironmentVariable("CLOCKGOES", "TOC")
	EndIf
ForEver
The response I get in debugger while running the first code is 'TIC' and thats it.

Re: Set/GetEnvironmentVariable()

Posted: Mon Feb 10, 2014 4:30 pm
by infratec
I think that's not possible, since RunProgramm() starts an own instance of the command processor,
which has it's own environment variables.

Bernd

Re: Set/GetEnvironmentVariable()

Posted: Mon Feb 10, 2014 4:32 pm
by UUICEO
infratec wrote:I think that's not possible, since RunProgramm() starts an own instance of the command processor,
which has it's own environmentvariables.

Bernd
But doesn't the executed program and the executing program share the same environment since the running program is able to write data to the called programs environment the called program should be able to do the same and have the executing program get the new value.

Re: Set/GetEnvironmentVariable()

Posted: Mon Feb 10, 2014 4:49 pm
by ts-soft
Every begun process inherits environmentvariables the starting process,
but only those which were already placed at the time of the start!
No communications between the processes and no shared memory.

Re: Set/GetEnvironmentVariable()

Posted: Mon Feb 10, 2014 5:04 pm
by UUICEO
Alright. I will work on a workaround then. Thank you for the help.

Re: Set/GetEnvironmentVariable()

Posted: Mon Feb 10, 2014 7:09 pm
by Michael Vogel
Haven't tried it, but it may work using the SETX command to define environment variables.

Re: Set/GetEnvironmentVariable()

Posted: Mon Feb 10, 2014 7:19 pm
by ts-soft
http://technet.microsoft.com/de-de/library/cc755104%28v=WS.10%29.aspx wrote:Setx writes variables to the master environment in the registry. Variables set with setx variables are available in future command windows only, not in the current command window.
and not in current processes :wink:

Re: Set/GetEnvironmentVariable()

Posted: Wed Feb 12, 2014 11:23 am
by Michael Vogel
Have tried this on a Win 8.1 machine as administrator ans surprisingly it worked with the code below. But as ts-soft says, there's no chance to find a useful solution which works in general.

Code: Select all

#Q=#DOUBLEQUOTE$

Procedure Execute(title.s,command.s)
	
	Protected handle
	Protected output.s
	Protected line.s

	handle=RunProgram("cmd",command,"",#PB_Program_Open|#PB_Program_Read)
	
	If handle
		While ProgramRunning(handle)
			line=ReadProgramString(handle)
			If Trim(line)
				output+line+#CR$
			EndIf
		Wend
		CloseProgram(handle)
		output+#CR$
	EndIf
	
	MessageRequester(title,output)

EndProcedure

Execute("Test", "/c setx z xxxxxx")
Execute("Test", "/c set|find "+#Q+"z"+#Q)

Re: Set/GetEnvironmentVariable()

Posted: Sat May 17, 2014 5:45 pm
by Tenaja
UUICEO wrote:Alright. I will work on a workaround then. Thank you for the help.
Did you ever find a solution to this? I am looking for something similar.

Thanks.

Re: Set/GetEnvironmentVariable()

Posted: Fri Apr 03, 2015 8:54 pm
by Blue
So, UUICEO, what workable solution have you found ?

Or maybe Tenaja, you did ??

I was thinking that you could simply use a plain old file, that interested programs/processes would be aware of, and could read/write at will. Wouldn't that be almost the same as using an environment variable, but with more space available to work with ?

Or, in Windows, maybe using the registry, creating/modifying a key/value pair that interested programs/processes would be aware of. Which is practically the same as using a file, isn't it ? Neither faster, nor more convenient.

Re: Set/GetEnvironmentVariable()

Posted: Sat Apr 04, 2015 5:22 am
by Joris
I'm not sure about what is needed and it's still early in the morning, but ... maybe you can pass an address via the clipboard to catch 'common data' once at the program start.

Re: Set/GetEnvironmentVariable()

Posted: Sat Apr 04, 2015 1:12 pm
by Blue
Joris wrote:[...] maybe you can pass an address via the clipboard to catch 'common data' once at the program start.
Sometimes we just can't see the most obvious ! (particularly true when debugging)
So it never occured to me to use the clipboard to pass information from a program to another... but i tried, and, of course, it worked like a charm.
Simple , effective. All one had to do was think of it. Your suggestion of using the clipboard woke me up.
Thank you Joris.

Next thing i now want to try, is to pass the address of a common memory area via the clipboard. I think i've read somewhere on this forum that it's not possible to share memory this way, but i'll look into it.

Re: Set/GetEnvironmentVariable()

Posted: Sat Apr 04, 2015 2:20 pm
by Tenaja
Joris wrote:I'm not sure about what is needed and it's still early in the morning, but ... maybe you can pass an address via the clipboard to catch 'common data' once at the program start.
Then you will start getting bug reports, because your programs unexpectedly destroy the clipboard...

Re: Set/GetEnvironmentVariable()

Posted: Sat Apr 04, 2015 8:29 pm
by Blue
Hello Tenaja
Tenaja wrote:Then you will start getting bug reports, because your programs unexpectedly destroy the clipboard...
You've experienced that ??? :shock:

I have implemented a solution using the Clipboard to pass information (one way only ) from appA to appB, and, so far, the only Clipboard data destruction i've encountered had to with my bad programming. :oops:
Once i corrected my errors, everything went fine and has remained so.

I guess it also depends on the amount of data exchanged : i'm only sending very short strings of information from A to B, and, so far, no fail, after extensive tests. Maybe with larger amounts of data, things would get iffy.

So, maybe such a solution should come with a caveat, and only be seen as a simple solution to simple problems.
However, I can assure you that as a simple solution to my simple problems, it has proven to be magic... so far :wink:
I'm just puzzled that i didn't think of it before.

Re: Set/GetEnvironmentVariable()

Posted: Sat Apr 04, 2015 8:53 pm
by Danilo
- MSDN: About the Clipboard
About the Clipboard

The clipboard is a set of functions and messages that enable applications to transfer data.
Because all applications have access to the clipboard, data can be easily transferred between applications or within an application.

The clipboard is user-driven. A window should transfer data to or from the clipboard only in response to a command from the user.
A window must not use the clipboard to transfer data without the user's knowledge.