views:

91

answers:

2

Greeting earthmen,

Here is my question:

How can I create a program which sets variable to current session of cmd.exe e.g.

c:\> set myvar
Environment variable myvar not defined
c:\>myexe.exe
c:>set myvar
myvar=myvalue

The only similar topic that I've found is this -

http://stackoverflow.com/questions/774047/how-can-i-change-windows-shell-cmd-exe-environment-variables-from-c+c

But I didn't get a single word from this:

There is a way... Just inject your code into parent process and call SetEnvironmentVariableA inside cmd's process memory. After injecting just free the allocated memory.

While C/C++ is not my "native" language I've felt myself completely lost when I've searched google with "c++ inject code" and etc... Is there an article where I can get more info about this.

BTW now I'm using one a little bit stupid workaround.As the setting a variable to

HKEY_CURRENT_USER\Environment

and

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

is comparatively easy I'm just recording similar variable to the registry:

load.temp.vars=set myvar1=myval1&set myvar2=myval2& ....

and then just call %load.temp.vars% and it will be executed as a command:

c:/>%load.temp.vars%
c:/>set myvar1
myvar1=myval1

It works fine bu it's not good enough for me :)

Live long and prosper, \\//_

+1  A: 

Check out this article: Three Ways to Inject Your Code into Another Process.

Also you probably will need a handle to your parent process (to determine the target process whose environment to change). A way to get it is described here.

Just keep in mind that injection may not work, depending on user account privileges, and also that some especially paranoid antivirus solutions may frown upon it.

atzz
+1  A: 

There are very easy way to do this without any tricks.

You should write a small program myexe.exe which produce a simple output (console output) like following:

SET myvar=Some value

then you start your program with the following steps:

myexe.exe >%TEMP%\t.cmd
call %TEMP%\t.cmd
del %TEMP%\t.cmd

Now in the current cmd.exe which started myexe.exe exist the environment variable myvar and it has the value Some value. Is it not exactly what you want?

The way is extrem simple and it works on all versions of Windows (and not only Windows). So it is my recommendation for you.

Oleg
your example as posted will only work on Windows. The method, however, is universal (if you assume everyone has some shell interpreter like Bash).
rubenvb
I mean of cause the method (or the way)
Oleg
@Johannes: Every person can have his own opinion. If you prefer to produce output with a value only than you have to write more code in batch which is VERY slow, please do this. Moreover in a real situation you need set not one, but a set of environment variables. In this case the output of several lines "SET myvar=Some value" is the best solution which I know. I had the first experience in solving of such problem in MS DOS using undocumented but well known features. Later I tested different ways and find this way which I find as the best one. If you do prefer another way, please use it.
Oleg