简体   繁体   English

CreateProcess 只执行一半命令

[英]CreateProcess only executes the half command

I have a problem with CreateProcess.我对 CreateProcess 有疑问。 I have create the following Source Code:我创建了以下源代码:

STARTUPINFOA si;
PROCESS_INFORMATION pi;

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

CreateProcessA(nullptr,
    "plink.exe -telnet -P 9999  192.168.230.75 < C:\\Users\\Mahre\\Scripts\\Telenet_Script.txt",
    nullptr,//security
    nullptr,// security
    FALSE,//inherits handles
    0,
    nullptr,
    nullptr,
    &si,
    &pi);

WaitForSingleObject(pi.hProcess, INFINITE);

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

The problem is that CreateProcess executes the command only in half.问题是 CreateProcess 只执行了一半的命令。 The command plink.exe -telnet -P 9999 192.168.230.75 is executed, but CreateProcess ignore this part < C:\\Users\\Mahre\\Scripts\\Telenet_Script.txt .命令plink.exe -telnet -P 9999 192.168.230.75执行,但 CreateProcess 忽略这部分< C:\\Users\\Mahre\\Scripts\\Telenet_Script.txt I'm pretty sure it's the less than sign.我很确定这是小于号。

With the evil system() it works very well and it was execute the complete command:使用 evil system()它工作得很好,它执行了完整的命令:

system("plink.exe -telnet -P 9999  192.168.230.75 < C:\\Users\\Mahre\\Scripts\\Telenet_Script.txt");

Can someone explain to me how I can do this with the CreateProcess?有人可以向我解释如何使用 CreateProcess 做到这一点吗?

The input redirection operator is provided by the shell, in this case cmd.exe .输入重定向运算符由 shell 提供,在本例中cmd.exe

To do this with CreateProcess() , you need to do this yourself.要使用CreateProcess()执行此操作,您需要自己执行此操作。

First open the file you want to use as input for reading using CreateFile() and set the handle to be inheritable.首先使用CreateFile()打开要用作读取输入的文件,并将句柄设置为可继承。 When you call CreateProcess() , you need to set the inheritable handles flag to TRUE, and fill in the STARTUPINFO struct to contain the handle you want to use for standard input.调用CreateProcess()时,需要将可继承句柄标志设置为 TRUE,并填写STARTUPINFO结构以包含要用于标准输入的句柄。

When doing it this way, you should not provide the "< filename" part of the command because you have set that up by hand.以这种方式执行此操作时,您不应提供命令的“< 文件名”部分,因为您已经手动设置了它。

For more information, read the CreateProcess documentation on MSDN.有关详细信息,请阅读 MSDN 上的CreateProcess文档。 The are examples in the documentation covering variants on this case.这些是文档中涵盖此案例变体的示例。

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

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