Created
January 17, 2016 07:59
-
-
Save anaisbetts/324bd8a782ad3bd184ef to your computer and use it in GitHub Desktop.
Collect a process into a job group, then let you kill it via writing to a pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Jobber.cpp : Defines the entry point for the application. | |
// | |
#include "stdafx.h" | |
#include "Jobber.h" | |
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, | |
_In_opt_ HINSTANCE hPrevInstance, | |
_In_ LPWSTR lpCmdLine, | |
_In_ int nCmdShow) | |
{ | |
AttachConsole(ATTACH_PARENT_PROCESS); | |
// Create a named pipe that we can be signaled on for early termination | |
wchar_t buf[512]; | |
wsprintf(buf, L"\\\\.\\pipe\\jobber-%d", GetCurrentProcessId()); | |
HANDLE hPipe = CreateNamedPipe(buf, | |
PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, | |
PIPE_READMODE_BYTE | PIPE_WAIT, | |
1, 256, 256, 100000, NULL); | |
OVERLAPPED io = { 0 }; | |
io.hEvent = CreateEvent(NULL, true, true, NULL); | |
if (!ConnectNamedPipe(hPipe, &io) && GetLastError() != ERROR_IO_PENDING) { | |
DWORD dwLastError = GetLastError(); | |
return -1; | |
} | |
STARTUPINFO si = { 0 }; | |
PROCESS_INFORMATION pi = { 0 }; | |
si.cb = sizeof(STARTUPINFO); | |
si.wShowWindow = nCmdShow; | |
if (!CreateProcess(NULL, lpCmdLine, NULL, NULL, true, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) { | |
DWORD dwLastError = GetLastError(); | |
return -1; | |
} | |
HANDLE hJob = CreateJobObject(NULL, NULL); | |
AssignProcessToJobObject(hJob, pi.hProcess); | |
ResumeThread(pi.hThread); | |
HANDLE handles[2]; | |
handles[0] = io.hEvent; | |
handles[1] = pi.hProcess; | |
int result = WaitForMultipleObjects(2, handles, false, INFINITE); | |
TerminateJobObject(hJob, -1); | |
if (result == WAIT_OBJECT_0) { | |
return -1; | |
} else { | |
DWORD dwExit; | |
GetExitCodeProcess(pi.hProcess, &dwExit); | |
return dwExit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment