wesnoth/projectfiles/VC14/WindowsTimeout.cpp
Martin Hrubý (hrubymar10) 674fda85b7 Migrate links to https if available - Fwd c18537edc0678f40a209797d72dfaba3e5e88545
(cherry-picked from commit bc4d22dc72e79c3d6a3364ac896e473afd298246)
2018-10-07 03:23:36 +00:00

59 lines
1.6 KiB
C++

/*
Copyright (C) 2014 by Sebastian Koelle <sk.aquileia@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Execute another executable:
* - wait for the process to quit, then pass through the error code
* - terminate it after a timeout of "t" microseconds, then return 2
*/
#if _WIN32_WINNT < 0x0602 //Windows version before Win8
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <Processthreadsapi.h>
#endif
#include <stdlib.h>
#include <iostream>
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cout << "WindowsTimeout: Error USAGE1: " << argv[0] << " \"command [--options]\" t";
return -1;
}
PROCESS_INFORMATION pi;
STARTUPINFO info = { sizeof(info) };
HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
info.hStdError = hStdErr;
if (!CreateProcess(NULL, argv[1], NULL, NULL, TRUE, 0, NULL, NULL, &info, &pi))
return -1;
DWORD dwExitCode;
if (::WaitForSingleObject(pi.hProcess, atoi(argv[2])) == WAIT_OBJECT_0) {
GetExitCodeProcess(pi.hProcess, &dwExitCode);
}
else {
TerminateProcess(pi.hProcess, 2);
dwExitCode = 2;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hStdErr);
return dwExitCode;
}