Skip to content

Instantly share code, notes, and snippets.

@rdp
Created June 24, 2015 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdp/6b5fc8993089ee12b44d to your computer and use it in GitHub Desktop.
Save rdp/6b5fc8993089ee12b44d to your computer and use it in GitHub Desktop.
#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