yeputons

Untitled

May 25th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <assert.h>
  4.  
  5. int main(int argc, char* argv[]) {
  6.   if (argc != 2) {
  7.     printf("Usage: textexe <executable-to-test>\n");
  8.     return 1;
  9.   }
  10.  
  11.   STARTUPINFO sinf;
  12.   PROCESS_INFORMATION pinf;
  13.   memset(&sinf, 0, sizeof sinf);
  14.   sinf.cb = sizeof(sinf);
  15.  
  16.   if (!CreateProcess(argv[1], argv[1], NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &sinf, &pinf)) {
  17.     DWORD error = GetLastError();
  18.     printf("Got system error code %u while trying to execute '%s'\n", (int)error, argv[1]);
  19.  
  20.     LPSTR msg;
  21.     DWORD msg_len = FormatMessage(
  22.       FORMAT_MESSAGE_ALLOCATE_BUFFER
  23.       | FORMAT_MESSAGE_FROM_SYSTEM
  24.       | FORMAT_MESSAGE_IGNORE_INSERTS,
  25.       NULL,
  26.       error,
  27.       MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  28.       (LPSTR)&msg,
  29.       32,
  30.       NULL
  31.     );
  32.     if (!msg_len) {
  33.       printf("Unable to get description (FormatMessage failed with error code %u)\n", (unsigned)GetLastError());
  34.     } else {
  35.       printf("%s", msg);
  36.       LocalFree(msg);
  37.     }
  38.     return error;
  39.   }
  40.   TerminateProcess(pinf.hProcess, -1);
  41.   WaitForSingleObject(pinf.hProcess, INFINITE);
  42.   return 0;
  43. }
Add Comment
Please, Sign In to add comment