【发布时间】:2017-08-25 09:16:14
【问题描述】:
我正在使用 CreateProcess 替换我的代码中的 system() 调用。我正在使用:
system(xfoil.exe < create_results.txt");
我用这个代替:
PROCESS_INFORMATION ProcessInfo; //This is what we get as an [out] parameter
STARTUPINFO StartupInfo; //This is an [in] parameter
LPCWSTR input_file = _tcsdup(TEXT(".\\create_results.txt"));
HANDLE inpfl = CreateFile(input_file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
StartupInfo.hStdInput = inpfl;
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo; //Only compulsory field
LPCWSTR exe_path =_tcsdup(TEXT(".\\xfoil.exe"));
if (CreateProcess(exe_path, NULL,
NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL,
NULL, &StartupInfo, &ProcessInfo))
{
WaitForSingleObject(&ProcessInfo.hProcess, 2000);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(inpfl);
}
else
{
CloseHandle(inpfl);
std::cout << "Could not create xfoil process" << std::endl;
}
原因是我需要控制允许进程运行多长时间(在这种情况下为 2000 毫秒),但是这种方法似乎不起作用。 我将进程的输入重定向到我想要作为输入的文件的句柄(以替换
【问题讨论】:
-
订单很重要!你在哪里设置
hStdInput,你在哪里调用ZeroMemory? -
你需要调用 ZeroMemory BEFORE 你分配给 hStdInput
-
vtc 为“简单的印刷错误”
-
该死,这是一个菜鸟的错误。我已更改顺序,但仍无法按预期工作。
-
文件被正确打开了吗?
标签: c++ windows c++11 createprocess