gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:00:30 24 4
gpt4 key购买 nike

我想在没有打开新cmd的情况下执行一些管道命令(所以我不能只使用system())这是我尝试执行的命令:

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

这是我尝试过的:

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);

它不起作用(没有错误,只是没有打开)

我也尝试过这个:

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 -")

这也不起作用(同样没有错误,只是没有打开)

当我这样尝试时:

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

一切正常(除了它打开的部分也是一个 cmd 窗口。)

如何在没有新 cmd 窗口的情况下从 C/C++ 程序执行此行?

最佳答案

如果你想掌控一切,你需要自己创建两个进程(openssl 和 mplayer)。因此,这将是两个 CreateProcess 调用。当然,您还必须自己创建重定向,这是在创建进程之前使用 CreatePipe 完成的。

这是一个示例(尚未编译,可能需要一些调整):

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);

注意:在传递给 CreateProcess 的命令行参数中,字符串中的第一个“单词”必须是进程名称(这不是您所做的)。

关于c++ - 如何使用 CreateProcess 或 ShellExecute 执行管道命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37586476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com