Hash :
b72938c8
Author :
Date :
2015-04-20T12:22:44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
/* See COPYING.txt for the full license governing this code. */
/**
* \file windows_process.c
*
* Source file for the process API on windows.
*/
#include <SDL.h>
#include <SDL_test.h>
#include <string.h>
#include <stdlib.h>
#include "SDL_visualtest_process.h"
#if defined(__WIN32__)
void
LogLastError(char* str)
{
LPVOID buffer;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buffer,
0, NULL);
SDLTest_LogError("%s: %s", str, (char*)buffer);
LocalFree(buffer);
}
int
SDL_LaunchProcess(char* file, char* args, SDL_ProcessInfo* pinfo)
{
BOOL success;
char* working_directory;
char* command_line;
int path_length, args_length;
STARTUPINFO sui = {0};
sui.cb = sizeof(sui);
if(!file)
{
SDLTest_LogError("Path to executable to launched cannot be NULL.");
return 0;
}
if(!pinfo)
{
SDLTest_LogError("pinfo cannot be NULL.");
return 0;
}
/* get the working directory of the process being launched, so that
the process can load any resources it has in it's working directory */
path_length = SDL_strlen(file);
if(path_length == 0)
{
SDLTest_LogError("Length of the file parameter is zero.");
return 0;
}
working_directory = (char*)SDL_malloc(path_length + 1);
if(!working_directory)
{
SDLTest_LogError("Could not allocate working_directory - malloc() failed.");
return 0;
}
SDL_memcpy(working_directory, file, path_length + 1);
PathRemoveFileSpec(working_directory);
if(SDL_strlen(working_directory) == 0)
{
SDL_free(working_directory);
working_directory = NULL;
}
/* join the file path and the args string together */
if(!args)
args = "";
args_length = SDL_strlen(args);
command_line = (char*)SDL_malloc(path_length + args_length + 2);
if(!command_line)
{
SDLTest_LogError("Could not allocate command_line - malloc() failed.");
return 0;
}
SDL_memcpy(command_line, file, path_length);
command_line[path_length] = ' ';
SDL_memcpy(command_line + path_length + 1, args, args_length + 1);
/* create the process */
success = CreateProcess(NULL, command_line, NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, working_directory, &sui, &pinfo->pi);
if(working_directory)
{
SDL_free(working_directory);
working_directory = NULL;
}
SDL_free(command_line);
if(!success)
{
LogLastError("CreateProcess() failed");
return 0;
}
return 1;
}
int
SDL_GetProcessExitStatus(SDL_ProcessInfo* pinfo, SDL_ProcessExitStatus* ps)
{
DWORD exit_status;
BOOL success;
if(!pinfo)
{
SDLTest_LogError("pinfo cannot be NULL");
return 0;
}
if(!ps)
{
SDLTest_LogError("ps cannot be NULL");
return 0;
}
/* get the exit code */
success = GetExitCodeProcess(pinfo->pi.hProcess, &exit_status);
if(!success)
{
LogLastError("GetExitCodeProcess() failed");
return 0;
}
if(exit_status == STILL_ACTIVE)
ps->exit_status = -1;
else
ps->exit_status = exit_status;
ps->exit_success = 1;
return 1;
}
int
SDL_IsProcessRunning(SDL_ProcessInfo* pinfo)
{
DWORD exit_status;
BOOL success;
if(!pinfo)
{
SDLTest_LogError("pinfo cannot be NULL");
return -1;
}
success = GetExitCodeProcess(pinfo->pi.hProcess, &exit_status);
if(!success)
{
LogLastError("GetExitCodeProcess() failed");
return -1;
}
if(exit_status == STILL_ACTIVE)
return 1;
return 0;
}
static BOOL CALLBACK
CloseWindowCallback(HWND hwnd, LPARAM lparam)
{
DWORD pid;
SDL_ProcessInfo* pinfo;
pinfo = (SDL_ProcessInfo*)lparam;
GetWindowThreadProcessId(hwnd, &pid);
if(pid == pinfo->pi.dwProcessId)
{
DWORD result;
if(!SendMessageTimeout(hwnd, WM_CLOSE, 0, 0, SMTO_BLOCK,
1000, &result))
{
if(GetLastError() != ERROR_TIMEOUT)
{
LogLastError("SendMessageTimeout() failed");
return FALSE;
}
}
}
return TRUE;
}
int
SDL_QuitProcess(SDL_ProcessInfo* pinfo, SDL_ProcessExitStatus* ps)
{
DWORD wait_result;
if(!pinfo)
{
SDLTest_LogError("pinfo argument cannot be NULL");
return 0;
}
if(!ps)
{
SDLTest_LogError("ps argument cannot be NULL");
return 0;
}
/* enumerate through all the windows, trying to close each one */
if(!EnumWindows(CloseWindowCallback, (LPARAM)pinfo))
{
SDLTest_LogError("EnumWindows() failed");
return 0;
}
/* wait until the process terminates */
wait_result = WaitForSingleObject(pinfo->pi.hProcess, 1000);
if(wait_result == WAIT_FAILED)
{
LogLastError("WaitForSingleObject() failed");
return 0;
}
if(wait_result != WAIT_OBJECT_0)
{
SDLTest_LogError("Process did not quit.");
return 0;
}
/* get the exit code */
if(!SDL_GetProcessExitStatus(pinfo, ps))
{
SDLTest_LogError("SDL_GetProcessExitStatus() failed");
return 0;
}
return 1;
}
int
SDL_KillProcess(SDL_ProcessInfo* pinfo, SDL_ProcessExitStatus* ps)
{
BOOL success;
DWORD exit_status, wait_result;
if(!pinfo)
{
SDLTest_LogError("pinfo argument cannot be NULL");
return 0;
}
if(!ps)
{
SDLTest_LogError("ps argument cannot be NULL");
return 0;
}
/* initiate termination of the process */
success = TerminateProcess(pinfo->pi.hProcess, 0);
if(!success)
{
LogLastError("TerminateProcess() failed");
return 0;
}
/* wait until the process terminates */
wait_result = WaitForSingleObject(pinfo->pi.hProcess, INFINITE);
if(wait_result == WAIT_FAILED)
{
LogLastError("WaitForSingleObject() failed");
return 0;
}
/* get the exit code */
success = GetExitCodeProcess(pinfo->pi.hProcess, &exit_status);
if(!success)
{
LogLastError("GetExitCodeProcess() failed");
return 0;
}
ps->exit_status = exit_status;
ps->exit_success = 1;
return 1;
}
#endif