简体   繁体   中英

How can we execute multiple cmd commands in one session using C++ Lib Function or Windows API?

Before running a cmd command in c++, I want to set some temporary environment variable, Which gets deleted with end of command line session. for eg before executing below cmd command, I want to set P4PASSWD perforce environment variable.

sprintf_s(p4Command, 500, "/C p4.exe print -o \"%s\" -q %s", destination, source);

LPCSTR Command = p4Command;

ShellExecute(0, "open", "cmd.exe", Command, 0, SW_HIDE);

This can be possible if we are allowed to execute multiple cmd commands in one session. But I don't know how it can be achieved.Please let me know if some more inputs are required.

Use CreateProcess to run p4.exe and pass a set of environment variables in the lpEnvironment parameter.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682009%28v=vs.85%29.aspx

CreateProcess gives you added benefits; you can wait for the process to terminate, you can retrieve the exit code etc.

Don't set environment variables for your own process as Gunther suggested earlier. The 7th parameter to CreateProcess is a new set of environment variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM