vendredi 25 août 2017

Redirecting CreateProcess input stream to a file

I'm using CreateProcess to substitute a system() call in my code. I was using:

system(xfoil.exe < create_results.txt");

Which I am substituing by this:

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;
}

The reason being I need to control how long the process is allowed to run ( 2000ms in this case), however it seems that this method does not work. I'm redirecting the input of the process to the handle of the file I want as input (to substitute the < operator), but the process is not receiving anything. It does launch the xfoil.exe in a separate console, though.

Aucun commentaire:

Enregistrer un commentaire