Set/GetEnvironmentVariable()

Just starting out? Need help? Post your questions and find answers here.
UUICEO
User
User
Posts: 57
Joined: Tue Mar 10, 2009 9:09 pm
Location: Shakopee, Minnesota. USA

Set/GetEnvironmentVariable()

Post 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.
Windows 7 Ultimate x64 / PureBasic 5.21 LTS / ProGUI Platinum / PureVision / http://www.linkedin.com/groups/PureBasi ... =&trk=tyah
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Set/GetEnvironmentVariable()

Post 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
UUICEO
User
User
Posts: 57
Joined: Tue Mar 10, 2009 9:09 pm
Location: Shakopee, Minnesota. USA

Re: Set/GetEnvironmentVariable()

Post 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.
Windows 7 Ultimate x64 / PureBasic 5.21 LTS / ProGUI Platinum / PureVision / http://www.linkedin.com/groups/PureBasi ... =&trk=tyah
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Set/GetEnvironmentVariable()

Post 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.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
UUICEO
User
User
Posts: 57
Joined: Tue Mar 10, 2009 9:09 pm
Location: Shakopee, Minnesota. USA

Re: Set/GetEnvironmentVariable()

Post by UUICEO »

Alright. I will work on a workaround then. Thank you for the help.
Windows 7 Ultimate x64 / PureBasic 5.21 LTS / ProGUI Platinum / PureVision / http://www.linkedin.com/groups/PureBasi ... =&trk=tyah
User avatar
Michael Vogel
Addict
Addict
Posts: 2808
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Set/GetEnvironmentVariable()

Post by Michael Vogel »

Haven't tried it, but it may work using the SETX command to define environment variables.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Set/GetEnvironmentVariable()

Post 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:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Michael Vogel
Addict
Addict
Posts: 2808
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Set/GetEnvironmentVariable()

Post 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)
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Set/GetEnvironmentVariable()

Post 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.
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set/GetEnvironmentVariable()

Post 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.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Set/GetEnvironmentVariable()

Post 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.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set/GetEnvironmentVariable()

Post 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.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Set/GetEnvironmentVariable()

Post 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...
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set/GetEnvironmentVariable()

Post 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.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Set/GetEnvironmentVariable()

Post 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.
Post Reply