Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This code snippet is licensed under The MIT License (MIT)
- http://opensource.org/licenses/MIT
- Copyright (c) 2014 Egor Suvorov aka yeputons
- */
- #include <stdio.h>
- #include <windows.h>
- #include <assert.h>
- int main(int argc, char* argv[]) {
- if (argc != 2) {
- printf("Usage: textexe.exe <executable-to-test-with-extension>\n");
- printf("If argument is not a valid executable, prints a message\n");
- printf("and exits with corresponding error code. Know codes:\n");
- printf(" 2 - file not found\n");
- printf(" 5 - access denied\n");
- printf(" 8 - not enough memory\n");
- printf("193 - not a valid Win32 application (too big static arrays?)\n");
- printf("216 - executable is not compatible with the version of Windows\n");
- return 1;
- }
- STARTUPINFO sinf;
- PROCESS_INFORMATION pinf;
- memset(&sinf, 0, sizeof sinf);
- sinf.cb = sizeof(sinf);
- SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
- if (!CreateProcess(argv[1], argv[1], NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &sinf, &pinf)) {
- DWORD error = GetLastError();
- printf("Got system error code %u while trying to execute '%s'\n", (unsigned)error, argv[1]);
- LPSTR msg;
- DWORD msg_len = FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER
- | FORMAT_MESSAGE_FROM_SYSTEM
- | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- error,
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (LPSTR)&msg,
- 32,
- NULL
- );
- if (!msg_len) {
- printf("Unable to get description (FormatMessage failed with error code %u)\n", (unsigned)GetLastError());
- } else {
- printf("%s", msg);
- LocalFree(msg);
- }
- return error;
- }
- TerminateProcess(pinf.hProcess, -1);
- WaitForSingleObject(pinf.hProcess, INFINITE);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement