简体   繁体   中英

How to use CreateProcess or ShellExecute to execute piped commands

I want to execute a few commands piped without new cmd opened (so I can't just use system() ) This is the command I try to execute:

C:\\openssl.exe enc -aes-128-ofb -d -in C:\\encrypted.bin -iv a2b050be9463 -K 6ba62eb7bb2ccace -nopad | C:\\\\mplayer.exe -"

This is what I tried :

WCHAR prog[] = L"C:\\openssl.exe";
WCHAR args[] = L"enc -aes-128-ofb -d -in C:\\encrypted.bin -iv a2b050be9463 -K 6ba62eb7bb2ccace -nopad | C:\\mplayer.exe -";

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

CreateProcess(prog, args, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);

And it didn't work (There is no error, its just not opening)

I also tried this:

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
HINSTANCE hinstRun1 = ShellExecute(NULL, L"open", L"cmd.exe", str2.c_str(), L"", SW_HIDE);
CoUninitialize();

//str2 == C:\\openssl.exe enc -aes-128-ofb -d -in C:\\encrypted.bin -iv a2b050be9463 -K 6ba62eb7bb2ccace -nopad | C:\\\\mplayer.exe -")

This is also not working (Again there is no error, its just not opening)

When I tried it like this :

system(("cmd.exe /c " str2).c_str());

Everything works good (Except the part that its opened also a cmd window.)

How can I execute this line from c/c++ program without new cmd window?

If you want to be in control of everything, you need to create both processes (openssl and mplayer) yourself. So that would be two CreateProcess calls. Of course, then, you have to create the redirection yourself, as well, and this is done using CreatePipe , before creating the processes.

Here is an example (haven't compiled it, may require some tuning):

HANDLE proc1_out;
HANDLE proc2_in;
SECURITY_ATTRIBUTES security_attributes;

// create the pipe between the processes
security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
security_attributes.bInheritHandle = TRUE; // pipe handles should be inheritable by sub-processes
security_attributes.lpSecurityDescriptor = NULL;
CreatePipe(&proc2_in, &proc1_out, &security_attributes, 0);

// create the first process
WCHAR proc1_app[] = L"C:\\openssl.exe";
WCHAR proc1_cmd_line[] = L"openssl enc -aes-128-ofb -d -in C:\\encrypted.bin -iv a2b050be9463 -K 6ba62eb7bb2ccace -nopad";
PROCESS_INFORMATION proc1_info;
STARTUPINFO proc1_startup_info;
ZeroMemory(&proc1_info, sizeof(PROCESS_INFORMATION));
ZeroMemory(&proc1_startup_info, sizeof(STARTUPINFO));
proc1_startup_info.cb = sizeof(STARTUPINFO);
proc1_startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
proc1_startup_info.hStdOutput = child_output_write; // redirected output
proc1_startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
proc1_startup_info.dwFlags |= STARTF_USESTDHANDLES;
CreateProcess(proc1_app, proc1_cmd_line, NULL, NULL, TRUE, 0, NULL, NULL, &proc1_startup_info, &proc1_info);

// create the second process
WCHAR proc2_app[] = L"C:\\mplayer.exe";
WCHAR proc2_cmd_line[] = L"mplayer -";
PROCESS_INFORMATION proc2_info;
STARTUPINFO proc2_startup_info;
ZeroMemory(&proc2_info, sizeof(PROCESS_INFORMATION));
ZeroMemory(&proc2_startup_info, sizeof(STARTUPINFO));
proc2_startup_info.cb = sizeof(STARTUPINFO);
proc2_startup_info.hStdInput = proc2_in; // redirected input
proc2_startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
proc2_startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE);
proc2_startup_info.dwFlags |= STARTF_USESTDHANDLES;
CreateProcess(proc2_app, proc2_cmd_line, NULL, NULL, TRUE, 0, NULL, NULL, &proc2_startup_info, &proc2_info);

Notes: in the command line argument passed to CreateProcess, the first "word" in the string must be the process name (this was not what you were doing).

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