0

I am trying to set the environment variables from batch file Session.bat which is generated from my C++ code storing some session related data to be set in enviroment variable some thing like this

Content of Session.bat

set SessionID_User=SAsdC123ASCascs123 echo %SessionID_User%

I tried to execute it from my C++ code using CreateProcess and system but none seems to work.

Although the batch file is executed from within the exe file without any exceptions when I try to see the environment variable that was intended to be set, I don't see it in the variables list I am printing on console using set I cannot see SessionID_User=SAsdC123ASCascs123

Below is the way I did the process

C:\Users\admin\Desktop>Session.exe C:\Users\admin\Desktop>set SessionID_User=SAsdC123ASCascs123 C:\Users\admin\Desktop>echo SAsdC123ASCascs123 SAsdC123ASCascs123 C:\Users\admin\Desktop> 

Why is the environment variable not getting set when I execute batch file from my exe but sets all good when executing batch file on it's own ?

Another Question is, Hows CreateProcess is different from system() in this usecase and in general

1 Answer 1

2

When you execute the batch file from your executable, you're creating a new process.

The batch file sets environment variables locally in that new process.

There is no way to export them back up to the parent process, but there are ways of communicating results back, including:

  • Via the standard output stream.
  • Via a stored file.
  • Via the Windows registry (e.g. command reg and friends).
Sign up to request clarification or add additional context in comments.

2 Comments

How do you do this, can you give me a lead ? @Cheers
For example, in the batch file you can use the redirection operators to place echo output in a file, that the parent process can read later. Like echo.SessionId=Blah45 >results.txt.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.