我使用CreateProcess來替換我的代碼中的system()調用。我用的是:將CreateProcess輸入流重定向到一個文件
system(xfoil.exe < create_results.txt");
對此我這個substituing:
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毫秒)的,但它似乎這種方法不起作用。 我將流程的輸入重定向到我希望作爲輸入的文件的句柄(替換<運算符),但該進程未收到任何內容。但它確實在單獨的控制檯中啓動了xfoil.exe。
訂購事宜!你在哪裏設置'hStdInput',你在哪裏調用'ZeroMemory'? –
你需要呼叫ZeroMemory * BEFORE *你分配給hStdInput –
vtc作爲「簡單的印刷錯誤」 –