Advertisement
Guest User

Untitled

a guest
Jun 8th, 2014
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. /*
  2. This code snippet is licensed under The MIT License (MIT)
  3. http://opensource.org/licenses/MIT
  4. Copyright (c) 2014 Egor Suvorov aka yeputons
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <windows.h>
  9. #include <assert.h>
  10.  
  11. int main(int argc, char* argv[]) {
  12.   if (argc != 2) {
  13.     printf("Usage: textexe.exe <executable-to-test-with-extension>\n");
  14.     printf("If argument is not a valid executable, prints a message\n");
  15.     printf("and exits with corresponding error code. Know codes:\n");
  16.     printf("  2 - file not found\n");
  17.     printf("  5 - access denied\n");
  18.     printf("  8 - not enough memory\n");
  19.     printf("193 - not a valid Win32 application (too big static arrays?)\n");
  20.     printf("216 - executable is not compatible with the version of Windows\n");
  21.     return 1;
  22.   }
  23.  
  24.   STARTUPINFO sinf;
  25.   PROCESS_INFORMATION pinf;
  26.   memset(&sinf, 0, sizeof sinf);
  27.   sinf.cb = sizeof(sinf);
  28.  
  29.   SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
  30.   if (!CreateProcess(argv[1], argv[1], NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &sinf, &pinf)) {
  31.     DWORD error = GetLastError();
  32.     printf("Got system error code %u while trying to execute '%s'\n", (unsigned)error, argv[1]);
  33.  
  34.     LPSTR msg;
  35.     DWORD msg_len = FormatMessage(
  36.       FORMAT_MESSAGE_ALLOCATE_BUFFER
  37.       | FORMAT_MESSAGE_FROM_SYSTEM
  38.       | FORMAT_MESSAGE_IGNORE_INSERTS,
  39.       NULL,
  40.       error,
  41.       MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  42.       (LPSTR)&msg,
  43.       32,
  44.       NULL
  45.     );
  46.     if (!msg_len) {
  47.       printf("Unable to get description (FormatMessage failed with error code %u)\n", (unsigned)GetLastError());
  48.     } else {
  49.       printf("%s", msg);
  50.       LocalFree(msg);
  51.     }
  52.     return error;
  53.   }
  54.   TerminateProcess(pinf.hProcess, -1);
  55.   WaitForSingleObject(pinf.hProcess, INFINITE);
  56.   return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement