gpt4 book ai didi

windows - system() 和 CreateProcess()/CreateProcessW()

转载 作者:可可西里 更新时间:2023-11-01 13:52:24 60 4
gpt4 key购买 nike

我想在 C 程序中执行 TEST.exe。当我使用

system( "TEST.exe <input-file> output-file" );

我能得到我所期望的。

但是当我使用以下代码时 CreateProcessW() 无法正常工作(参见 How do I run an external program? ):

if (CreateProcessW(const_cast<LPCWSTR>(FullPathToExe.c_str()), 
pwszParam, 0, 0, false,
CREATE_DEFAULT_ERROR_MODE, 0, 0,
&siStartupInfo, &piProcessInfo) != false)
{
/* Watch the process. */
dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (SecondsToWait * 1000));
iReturnVal = GetLastError();
}
else
{
/* CreateProcess failed */
iReturnVal = GetLastError();
}

在哪里

FullPathToExe="TEST.exe", pwszParam="TEST.exe <input-file> output-file".

而 WaitForSingleObject() 返回 258,GetLastError() 返回 1813(“在图像文件中找不到指定的资源类型。”)。

此外,上面的 CreateProcessW() 代码在我运行自己的 HelloProcess.exe 时工作正常(打印 hello,然后休眠几秒钟,由以下数字确定,然后退出。)

FullPathToExe="HelloProcess.exe", pwszParam="HelloProcess.exe 10".

有什么想法吗?感谢您的任何提示!

最佳答案

system实际上产生一个 cmd运行命令的实例:

The system function passes command to the command interpreter, which executes the string as an operating-system command. system refers to the COMSPEC and PATH environment variables that locate the command-interpreter file (the file named CMD.EXE in Windows NT). If command is NULL, the function simply checks to see whether the command interpreter exists.
Documentation of system

这就是为什么像 < 这样的重定向操作符和 >工作。 CreateProcess 不是这种情况它实际上只是生成一个进程,而不是一个执行另一个进程的 shell。由于重定向运算符是 shell 而不是 OS 的一个特性,因此您必须手动对进程进行输入和输出。

关于windows - system() 和 CreateProcess()/CreateProcessW(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9970730/

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