mirror of
https://github.com/wesnoth/wesnoth
synced 2025-04-30 11:51:26 +00:00

As the --timeout option for Wesnoth can stall the process, we replace it by an encapsulation comparable to the Unix timeout function. WindowsTimeout isn't limited to Wesnoth, it can in fact set a timeout for any executable called through it. An example on the use of these WinAPI functions can be found on http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512.aspx
59 lines
1.6 KiB
C++
59 lines
1.6 KiB
C++
/*
|
|
Copyright (C) 2014 by Sebastian Koelle <sk.aquileia@gmail.com>
|
|
Part of the Battle for Wesnoth Project http://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
|
|
#include <windows.h>
|
|
#else
|
|
#include <Processthreadsapi.h>
|
|
#endif
|
|
// #define WIN32_LEAN_AND_MEAN
|
|
#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;
|
|
}
|