如何从父进程发送命令以在现有子 cmd.exe 进程上运行?

发布于 2025-01-11 08:01:41 字数 1657 浏览 0 评论 0原文

我有一段代码,除其他外,在给定特定输入的情况下,它会创建一个具有重定向 I/O 句柄的子 cmd.exe 进程。

HANDLE child_IN_Rd = NULL;
HANDLE child_IN_Wr = NULL;
HANDLE child_OUT_Rd = NULL;
HANDLE child_OUT_Wr = NULL;
SECURITY_ATTRIBUTES sa;
STARTUPINFOW si;
PROCESS_INFORMATION pi;

saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
saAttr.bInheritHandle = TRUE; 
saAttr.lpSecurityDescriptor = NULL;

CreatePipe(&child_IN_Rd, &child_IN_Wr, sa, 0);
CreatePipe(&child_OUT_Rd, &child_OUT_Wr, sa, 0);

ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO); 
si.hStdError = child_OUT_Wr;
si.hStdOutput = child_OUT_Wr;
si.hStdInput = child_IN_Rd;

CreateProcessA("C:\\Windows\\system32\\cmd.exe",
               NULL,
               NULL.
               NULL,
               false,
               NULL,
               NULL,
               NULL,
               si,
               pi)

CloseHandle(child_IN_Rd);
CloseHandle(child_OUT_Wr);

其目的是让代码根据远程客户端的要求运行 cmd 命令。父进程从客户端接收一个字符串,并将其写入child_IN_Wr。然后,cmd.exe 进程应将字符串作为命令运行,并在父控制台上读取输出。

像这样的事情:

WriteFile(child_IN_Wr, command, command_size, NULL, NULL);
...
ReadFile(child_OUT_Rd, buffer, buffer_size, NULL, NULL);

//Do something with the output, eg. print, or sending it back to client.
printf("%s", buffer);

但是,我无法让它工作。现有的 cmd.exe 进程似乎没有注册/处理写入 child_IN_Wr 的输入,因此不会读取任何输出。

据我了解,每次父级从远程客户端收到输入时,只需使用 CommandLine 参数下的给定命令运行 C​​reateProcess 即可轻松解决此问题。但是,每次运行命令时都会生成并终止一个新的 cmd.exe 进程,并且我希望在给定客户端的特定输入之前创建的单个 cmd.exe 进程上运行所有命令,该进程仅在客户端之后终止发送另一个特定的输入。

我该怎么办?我一直在尝试使用管道进行 IPC,如上所示,但到目前为止还没有运气。提前致谢。

I have a piece of code that, amongst other things, creates a child cmd.exe process with redirected I/O handles, given a specific input.

HANDLE child_IN_Rd = NULL;
HANDLE child_IN_Wr = NULL;
HANDLE child_OUT_Rd = NULL;
HANDLE child_OUT_Wr = NULL;
SECURITY_ATTRIBUTES sa;
STARTUPINFOW si;
PROCESS_INFORMATION pi;

saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
saAttr.bInheritHandle = TRUE; 
saAttr.lpSecurityDescriptor = NULL;

CreatePipe(&child_IN_Rd, &child_IN_Wr, sa, 0);
CreatePipe(&child_OUT_Rd, &child_OUT_Wr, sa, 0);

ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO); 
si.hStdError = child_OUT_Wr;
si.hStdOutput = child_OUT_Wr;
si.hStdInput = child_IN_Rd;

CreateProcessA("C:\\Windows\\system32\\cmd.exe",
               NULL,
               NULL.
               NULL,
               false,
               NULL,
               NULL,
               NULL,
               si,
               pi)

CloseHandle(child_IN_Rd);
CloseHandle(child_OUT_Wr);

The intent is for the code to run cmd commands at the behest of a remote client. The parent receives a string from the client, and writes it to child_IN_Wr. The cmd.exe process should then run the string as a command, and the output is read on the parent console.

Something like this:

WriteFile(child_IN_Wr, command, command_size, NULL, NULL);
...
ReadFile(child_OUT_Rd, buffer, buffer_size, NULL, NULL);

//Do something with the output, eg. print, or sending it back to client.
printf("%s", buffer);

However, I am unable to get it to work. The existing cmd.exe process does not seem to register/process an input written to child_IN_Wr, and no output is read as a result.

I understand that this issue can be easily fixed by simply running CreateProcess with the given command under the CommandLine parameter, every time the parent receives an input from the remote client. However, this spawns and terminates a new cmd.exe process every time a command is run, and I wish to run all the commands on a single cmd.exe process created beforehand given a specific input from a client, that only terminates after the client sends another specific input.

How should I go about this? I have been trying out the use of pipes for IPC, as seen above, but have had no luck thus far. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文