Ticket #3049: try_open_process.cpp

File try_open_process.cpp, 713 bytes (added by rransom, 9 years ago)

test program to probe OpenProcess error codes on Windows

Line 
1// try_open_process.cpp : Defines the entry point for the console application.
2
3#include <stdio.h>
4#include <tchar.h>
5
6#include <windows.h>
7
8int _tmain(int argc, _TCHAR* argv[])
9{
10  int pid = atoi(argv[1]);
11
12  printf("using pid %d...\n", pid);
13
14  HANDLE hproc = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE,
15                             FALSE,
16                             pid);
17
18  if (hproc != NULL) {
19    printf("successfully opened process\n");
20    CloseHandle(hproc);
21    return 1; // report failure - we didn't get what we wanted
22  }
23
24  DWORD err_code = GetLastError();
25  printf("failed to open process -- error code: %ld\n",
26         (unsigned long)(err_code));
27  return 0; // report success -- we got an error code!
28}
29