Created
June 24, 2015 07:38
-
-
Save rdp/6b5fc8993089ee12b44d to your computer and use it in GitHub Desktop.
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
#include <stdlib.h> // NULL | |
#include <stdbool.h> // false | |
#include <windows.h> // AttachConsole, CTRL_C_EVENT, etc. | |
#include <stdio.h> // printf | |
#include <strsafe.h> | |
void logLastError() | |
{ | |
LPTSTR errorText = NULL; | |
FormatMessage( | |
// use system message tables to retrieve error text | |
FORMAT_MESSAGE_FROM_SYSTEM | |
// allocate buffer on local heap for error text | |
|FORMAT_MESSAGE_ALLOCATE_BUFFER | |
// Important! will fail otherwise, since we're not | |
// (and CANNOT) pass insertion parameters | |
|FORMAT_MESSAGE_IGNORE_INSERTS, | |
NULL, // unused with FORMAT_MESSAGE_FROM_SYSTEM | |
GetLastError(), | |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
(LPTSTR)&errorText, // output | |
0, // minimum size for output buffer | |
NULL); // arguments - see note | |
if ( NULL != errorText ) | |
{ | |
printf("failure: %s", errorText); | |
LocalFree(errorText); | |
errorText = NULL; | |
} | |
} | |
void WaitProgram(int pid) | |
{ | |
HANDLE h=OpenProcess(SYNCHRONIZE , TRUE, pid); | |
if (!h) { | |
logLastError(); | |
} | |
printf("waiting for pid %d", pid); | |
WaitForSingleObject(h, INFINITE ); | |
} | |
int main( int argc, const char* argv[] ) { | |
// assume they know the pid... | |
if (argc != 2) { | |
printf("syntax: pid"); | |
return 1; | |
} | |
int pid = atoi(argv[1]); | |
WaitProgram(pid); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment