简体   繁体   English

如何使用CreateProcess或ShellExecute执行管道命令

[英]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: 我想执行一些没有打开新cmd管道命令(因此我不能只使用system() ),这是我尝试执行的命令:

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.) 一切正常(除了打开cmd窗口的部分。)

How can I execute this line from c/c++ program without new cmd window? 如何在没有新cmd窗口的情况下从c / c ++程序执行此行?

If you want to be in control of everything, you need to create both processes (openssl and mplayer) yourself. 如果要控制一切,则需要自己创建两个进程(openssl和mplayer)。 So that would be two CreateProcess calls. 因此,这将是两个CreateProcess调用。 Of course, then, you have to create the redirection yourself, as well, and this is done using CreatePipe , before creating the processes. 当然,然后,您也必须自己创建重定向,这是在创建流程之前使用CreatePipe完成的。

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). 注意:在传递给CreateProcess的命令行参数中,字符串中的第一个“单词”必须是进程名称(这不是您要执行的操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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