Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,497 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,361 people online right now. Registration is fast and FREE... Join Now!




Change environment variable in parent process

 
Reply to this topicStart new topic

Change environment variable in parent process, In Windows, how can I have a child process change the environment vari

cdk
20 Mar, 2007 - 01:07 PM
Post #1

New D.I.C Head
*

Joined: 17 Jan, 2007
Posts: 15


My Contributions
I need to change the value of an environment variable in a parent process in console-mode Windows. The parent process is a script (.cmd) that executes the program. Here's an example:

CODE
runit.cmd:
set xyz=test
changeXYZ
echo xyz=%xyz%


In runit.cmd, the xyz environment variable is set to "test". The changeXYZ program somehow changes the value of the xyz environment variable to something else, say "123xyz456". The echo statement shows that the xyz environment variable has been changed.

Here's a simple C++ program that sets the environment variable for "xyz" in the current process but the value of "xyz" reverts to its original value when the program terminates:

CODE
changeXYZ.cpp:
#include "stdafx.h"
int main(int, char**) {
    putenv("xyz=123xyz456");

    char *test = getenv("xyz");
    if (test != NULL) {
        printf("xyz=%s\n", test);
    }
    else {
        printf("test is null\n");
    }
    return 0;
}


When the above scipt is run with the above program, the output is:

CODE
> runit
set xyz=test
xyz=123xyz456
xyz=test


The first output line is from the script file.
The second output line is from the running program.
The third output line is from the script file.

I want the script file, after the program runs, to show xyz=123xyz456.

Does anybody know how to do this?

Thanks.

cdk


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Change Environment Variable In Parent Process
20 Mar, 2007 - 08:05 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well I could be wrong here but I belive that you use putenv() and getenv(). Here is one example and here is microsoft's take on it.

hope this helps.
User is offlineProfile CardPM
+Quote Post

cdk
RE: Change Environment Variable In Parent Process
21 Mar, 2007 - 06:18 AM
Post #3

New D.I.C Head
*

Joined: 17 Jan, 2007
Posts: 15


My Contributions
Here's a quote from the MS documentation:

QUOTE
_putenv and _wputenv affect only the environment that is local to the current process ; you cannot use them to modify the command-level environment(emphasis by cdk). That is, these functions operate only on data structures accessible to the run-time library and not on the environment segment created for a process by the operating system. When the current process terminates, the environment reverts to the level of the calling process (in most cases, the operating-system level).


Also notice that I used getenv and putenv in the sample code that shows how it doesn't do what I want.

To clarify: I want the parent process on a Windows XP system to receive (somehow -- using any technique) a modified environment variable changed by a child process.

Any help anyone?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Change Environment Variable In Parent Process
21 Mar, 2007 - 01:06 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
Sorry. I knew that, I was just in a hurry.

I have been thinking about this and searching the net and I don't *think* there is an easy solution. The problem is that windows does not WANT you to edit the environment of the parent process[1]. It is possible to add varaibles to the system environment, but to get these new values the parent process has to look for them. The command prompt does not, it needs to be reloaded in order to see the new values.

To actaully edit the environment of the parent process would require a nonstandard *hack* which may cause an access violation.

The best solutions would be to look for another way to do whatever you are trying to accomplish with this. For example instead of using a batch file, use a more powerful scripting language which can handle inter-process communication or just registry entries etc.
User is offlineProfile CardPM
+Quote Post

cdk
RE: Change Environment Variable In Parent Process
22 Mar, 2007 - 07:36 AM
Post #5

New D.I.C Head
*

Joined: 17 Jan, 2007
Posts: 15


My Contributions
Thanks -- that's pretty much the conclusion I've come to accept too.

Can you recommend an open-source scripting language that meets our criteria?

cdk
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Change Environment Variable In Parent Process
22 Mar, 2007 - 09:26 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
What is your criteria? What is is you are trying to accomplish? I am a big fan of ActivePerl. I find the VBS/JS are good (they already come with windows). If you want a GUI the only way I know to go is TCL/TK. Windows has a new scriping environment "PowerShell" which I am told makes many of the WMI tasks more manageable.

Now a script (using WMI) can edit the environment but also can not change the environment of the parent process.

All programs return an "errorlevel" to the parent process which can be used to pass simple messages back and forth (anything that can fit into an int). But if you were asking the user to type in a word you would have to abandon the batch file and move to a scripts can do all that a batch file can do and much much more.

VBS/JS - Both come with windows and are about equal in power. There are LOTS of examples of how to work with them at the scripting center. And "The scripting guys" are a hoot. The learning curve is not too hard here.

ActivePerl - Perl is a fantastic language for processing data. It plays well with MySQL, it works well as a web CGI scripting language (I perfer PHP now but what can you do). The learning curve is steaper but this is a very powerful language and great experiance.

TCL/TK - a more confusing language which has the added benefit of scripting GUI applications. The learning curve is pretty tall, but you can generate a nice GUI for your users to see.

Now we venture ouside of my sphere of direct knowledge:

PowerShell - Don't know much but I here it is the Cadillac of Microsoft scripting languages. From what I see in the microsoft newsgroups vista-administrators seem to really like it.

Python - Lots of people like this one. It gained popularity as a CGI application but has since moved on. It is better at dealing with exceptions than most of the other script languages.

scheme - computer science people like this one. A child of Lisp it can be used as a functional programming language (I really should look into this).

Oh the list goes on and on. Really I think you could proably use just about any of these languages or a half-dozen others.

User is offlineProfile CardPM
+Quote Post

cdk
RE: Change Environment Variable In Parent Process
22 Mar, 2007 - 11:58 AM
Post #7

New D.I.C Head
*

Joined: 17 Jan, 2007
Posts: 15


My Contributions
My choice was VBS/JS.

The client said "no" because then they would have to have people who knew those technologies in order to modify the scripts.

Unless someone has another idea for how to accommodate this request, I'm closing the issue by using a different solution.

Thanks for all the help.

cdk
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month