Solved best way to set OS variable from C

Custom / CUSTOM
December 4, 2014 at 11:46:49
Specs: windows 7, 2.401 GHz / 2047 MB
I'm having the following issue: I have this C program, which is basically allowing a user to select a character string amongst a whole series. So, in the C program, I have this one character variable that holds a given value. No problem.

Thing is : I have this executable file (EXE) which I next am going to call from different Windows batch programs (CMD). Obviously, the selected value inside the executable file, is what I am interested in, and I would like to subsequently, use that value inside my CMD file. So, I would like to store and use it there as a normal variable, like any other CMD program variable.

I know one way of doing that, but that would not be a very clean way. So, my question is : what is the cleanest way to do that ?

If not sure what I'm asking, I'll be happy to explain more or better. Thanks


edit: I was trying something like this, but that doesn't work:


char set_command[100];

strcpy(set_command,"set p_var=");
strcat(set_command,"testtest");
system(set_command);

message edited by tvc


See More: best way to set OS variable from C

Report •

✔ Best Answer
December 4, 2014 at 17:20:04
I'm just guessing here, but it may be that you're trying to pass the environment variable between discreet instances of CMD. f/e: I open a command window and type "set k=kelvin", no other windows will acknowledge that variable, with that value. If this is the case in your situation, use a "squib" file to pass and hold data.


#1
December 4, 2014 at 12:35:01
Use setenv()

http://pubs.opengroup.org/onlinepub...

Alternatively, if it is only one value that you want to pass back, you could retun an errorcode which the batch file can test.


Report •

#2
December 4, 2014 at 14:26:54
That looks perfect, thanks !

Can't get it going though:

I'm having:
#include<stdlib.h>

and
setenv("p_lov_value", "test", 0);

and it fails compil with:
C:\Users\username\AppData\Local\Temp\ccQ5ZF1c.o:test.c:(.text+0x56b): undefined reference to `setenv'
collect2.exe: error: ld returned 1 exit status


Report •

#3
December 4, 2014 at 14:41:14
My bad. It looks like, in Windows, it's called SetEnvironmentVariable():

http://msdn.microsoft.com/en-us/lib...
http://advancedcppwithexamples.blog...


Report •

Related Solutions

#4
December 4, 2014 at 15:00:25
That compiles, but it doesn't do the whole trick:

If from the same C-executable I call :

system("echo +++%p_lov_value%+++");

that works, and displays the value,
but when the C exectuable has finished, this doesn't display anything anymore:

echo +++%p_lov_value%+++


Report •

#5
December 4, 2014 at 17:20:04
✔ Best Answer
I'm just guessing here, but it may be that you're trying to pass the environment variable between discreet instances of CMD. f/e: I open a command window and type "set k=kelvin", no other windows will acknowledge that variable, with that value. If this is the case in your situation, use a "squib" file to pass and hold data.

Report •

#6
December 5, 2014 at 03:02:40
Yes, that is sort of the idea. The thing is that CMD files, when they call each other - and all withing 1 CMD window - keep all variables they set themselves, no matter which parent or child CMD sets any variable. But, here I'm writing and thus calling an EXE, and the only target of that specific EXE actually is to set one variable. Don't know what a "squib" file is, but possibly I've done just that, by means of letting the EXE file create a temporary CMD file, that sets 1 variable only. That CMD then can be called by other CMD's, because they know the name of that CMD file (since they in fact also call the EXE).

Report •

#7
December 5, 2014 at 20:22:30
Yes, we've both reached the same objective, slightly different point-of-view. ("squib file" is must my own term for a scratch-file, or temporary disk-based storage dump of no particular format). In your case, the "squib-file" is an executable batch, but of course it could be any format, as long as it holds the pertinant data we need. Your batch (CMD) format is the most efficient that I know of for this application.

Report •

#8
December 6, 2014 at 09:40:30
Indeed. But in fact, creating such a file, is what I refer to as the "dirty" solution, in my opening post. I do that a bit too much, and for that reason I was thinking I'm needing to do it differently. Technically, it'll always work to create such a file, but technical problems often start with file-write-failures, whatever the reason.That's the weak point of that method. If the file cannot be written, that is a total failure of that script. And, that occur by means of external conditions.

Report •

Ask Question