Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29276 Discussions

Setting & getting environment variables

michael_green
Beginner
612 Views
I want to get program A to communicate with program B (created with CreateProcess) by use of environment variables, but can't get it to work. I have been experimenting within just program A and get the following:

iret = SetEnvironmentVariable('junk','asdf')

This works fine. However, if the very next statement is:

iret = GetEnvironmentVariable('junk',temp,4)

This does not work. iret has a value of 5, and temp is blank. If the next statement after that is:

call getenv('junk',temp)

This works fine here, but it doesn't work if I place it in program B.

I guess there are a number of issues here that I don't understand, including how to use the functions, the scope of the environment variables (are they just local to one process?), and even whether this is the right approach. Also, what is the difference between Getenv and GetEnvironmentVariable? Please could someone point me to an example of a process communicating with its child.

With many thanks

Mike


0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
612 Views
The main thing you forgot is that all APIs require (and return) C-style, char(0) terminated strings:

(C=>F) sFortran = sC(1 : index(sC,char(0))-1)
(F=>C) sC = trim(sFortran)//char(0)

Getenv is probably just a thin wrapper around GetEnvironmentVariable, which just performs string conversion.

"Parent communicating with a child process" is a very broad description. This MSDN article is a "classic" example -- it uses pipes to redirect standard input and output; the child process may have unchanged code (i.e. retain READ(* and WRITE(* ), and the parent should be responsible to CreatePipe/WriteFile/ReadFile.

Jugoslav
0 Kudos
Steven_L_Intel1
Employee
612 Views
Environment variables are local to a process. When you create a child process, you can specify that the child should inherit a copy of its parent's environment variables, but it's only a copy. Changes in one after that are not seen in the other. Not an effective means of communication.

Steve
0 Kudos
Reply