yeputons

Untitled

May 25th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 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.   SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
  17.   if (!CreateProcess(argv[1], argv[1], NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &sinf, &pinf)) {
  18.     DWORD error = GetLastError();
  19.     printf("Got system error code %u while trying to execute '%s'\n", (unsigned)error, argv[1]);
  20.  
  21.     LPSTR msg;
  22.     DWORD msg_len = FormatMessage(
  23.       FORMAT_MESSAGE_ALLOCATE_BUFFER
  24.       | FORMAT_MESSAGE_FROM_SYSTEM
  25.       | FORMAT_MESSAGE_IGNORE_INSERTS,
  26.       NULL,
  27.       error,
  28.       MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  29.       (LPSTR)&msg,
  30.       32,
  31.       NULL
  32.     );
  33.     if (!msg_len) {
  34.       printf("Unable to get description (FormatMessage failed with error code %u)\n", (unsigned)GetLastError());
  35.     } else {
  36.       printf("%s", msg);
  37.       LocalFree(msg);
  38.     }
  39.     return error;
  40.   }
  41.   TerminateProcess(pinf.hProcess, -1);
  42.   WaitForSingleObject(pinf.hProcess, INFINITE);
  43.   return 0;
  44. }
Add Comment
Please, Sign In to add comment