将 CreateProcess 输入流重定向到文件
Redirecting CreateProcess input stream to a file
我正在使用 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 毫秒),但似乎此方法不起作用。
我正在将进程的输入重定向到我想要作为输入的文件的句柄(以替代 < 运算符),但是进程没有收到任何东西。不过,它确实会在单独的控制台中启动 xfoil.exe。
您需要设置文件安全属性以允许继承句柄和启动信息标志以使子进程使用传递的句柄:
::STARTUPINFO startup_information{};
::SECURITY_ATTRIBUTES security_attributes
{
sizeof(::SECURITY_ATTRIBUTES)
, nullptr
, TRUE // allow handle to be inherited
};
::HANDLE const inpfl{::CreateFileW(input_file, GENERIC_READ, FILE_SHARE_READ, ::std::addressof(security_attributes), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)};
if(INVALID_HANDLE_VALUE == inpfl)
{
auto const last_error{::GetLastError()};
// handle error...
}
startup_information.cb = sizeof(startup_information);
startup_information.dwFlags = STARTF_USESTDHANDLES;
startup_information.hStdInput = inpfl;
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;
下一次通话后 StartupInfo.hStdInput 会怎样?
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
糟糕...hStdInput 现在已归零
最初这样做...
PROCESS_INFORMATION ProcessInfo = {};
STARTUPINFO StartupInfo = {};
...将阻止您将任何内容归零...see
此外,根据 CreateProcess StartupInfo documentation,您必须为 hStdInput,设置 dwFlags 到 STARTF_USESTDHANDLES,否则默认输入是键盘缓冲区
我正在使用 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 毫秒),但似乎此方法不起作用。 我正在将进程的输入重定向到我想要作为输入的文件的句柄(以替代 < 运算符),但是进程没有收到任何东西。不过,它确实会在单独的控制台中启动 xfoil.exe。
您需要设置文件安全属性以允许继承句柄和启动信息标志以使子进程使用传递的句柄:
::STARTUPINFO startup_information{};
::SECURITY_ATTRIBUTES security_attributes
{
sizeof(::SECURITY_ATTRIBUTES)
, nullptr
, TRUE // allow handle to be inherited
};
::HANDLE const inpfl{::CreateFileW(input_file, GENERIC_READ, FILE_SHARE_READ, ::std::addressof(security_attributes), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)};
if(INVALID_HANDLE_VALUE == inpfl)
{
auto const last_error{::GetLastError()};
// handle error...
}
startup_information.cb = sizeof(startup_information);
startup_information.dwFlags = STARTF_USESTDHANDLES;
startup_information.hStdInput = inpfl;
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;
下一次通话后 StartupInfo.hStdInput 会怎样?
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
糟糕...hStdInput 现在已归零
最初这样做...
PROCESS_INFORMATION ProcessInfo = {};
STARTUPINFO StartupInfo = {};
...将阻止您将任何内容归零...see
此外,根据 CreateProcess StartupInfo documentation,您必须为 hStdInput,设置 dwFlags 到 STARTF_USESTDHANDLES,否则默认输入是键盘缓冲区