【发布时间】:2019-11-11 11:12:13
【问题描述】:
我对 CreateProcess 有疑问。我创建了以下源代码:
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);
问题是 CreateProcess 只执行了一半的命令。命令plink.exe -telnet -P 9999 192.168.230.75被执行,但是CreateProcess忽略了这部分< C:\\Users\\Mahre\\Scripts\\Telenet_Script.txt。我很确定这是小于号。
对于邪恶的system(),它工作得非常好,它执行了完整的命令:
system("plink.exe -telnet -P 9999 192.168.230.75 < C:\\Users\\Mahre\\Scripts\\Telenet_Script.txt");
有人可以向我解释如何使用 CreateProcess 做到这一点吗?
【问题讨论】:
-
查看执行 cmd.exe。
-
CreateProcess() 对输入重定向一无所知。这是一个shell功能,执行 cmd.exe /c 代替。
-
我可以想象
<和C:\\Users\\Mahre\\Scripts\\Telenet_Script.txt也作为参数传递给telnet,telnet以某种方式处理它们或只是忽略它们。这与在bash或cmd.exe中调用此行相反,其中<之后的部分由bash或cmd.exe本身处理(作为@HansPassant 已经提到的输入重定向)。
标签: c++ system createprocess