Kris
Kris

Reputation: 56

How do I test an executable without actually running it?

I would like to know how to test an executable file, without running the executable.

I am trying to write a function which returns a boolean, indicating whether the program will run or not.

The issue I have is that running a program which will not work, results in the windows error message "16-bit application not compatible with this 64-bit version of windows", which is not at all desirable.

I need advice on how to modify my code to skip the message programatically instead of manually (pressing OK to close the pop-up window is too slow).

My code at the moment:

        try
        {
            Process.Start(exePath);
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return false;
        }

I expect this error to appear 99.9% of the time because the aim of my code is to repeatedly test randomly generated executables until one of them works.

Upvotes: 0

Views: 489

Answers (1)

jaket
jaket

Reputation: 9341

Assuming 16-bit programs will never run you could read the exe header and determine if it's a PE image. If not don't even attempt to run it. Below is a hexdump of an exe file. The byte at 0x3c is the offset to the PE header. If you then look there you should see e0. Now at offset e0 you'll find the letters 'PE'.

00000000  4d 5a 90 00 03 00 00 00  04 00 00 00 ff ff 00 00  |MZ..............|
00000010  b8 00 00 00 00 00 00 00  40 00 00 00 00 00 00 00  |........@.......|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 e0 00 00 00  |................|
00000040  0e 1f ba 0e 00 b4 09 cd  21 b8 01 4c cd 21 54 68  |........!..L.!Th|
00000050  69 73 20 70 72 6f 67 72  61 6d 20 63 61 6e 6e 6f  |is program canno|
00000060  74 20 62 65 20 72 75 6e  20 69 6e 20 44 4f 53 20  |t be run in DOS |
00000070  6d 6f 64 65 2e 0d 0d 0a  24 00 00 00 00 00 00 00  |mode....$.......|
00000080  2e 1c ae 49 6a 7d c0 1a  6a 7d c0 1a 6a 7d c0 1a  |...Ij}..j}..j}..|
00000090  6a 7d c1 1a e2 7d c0 1a  1c e0 bb 1a 65 7d c0 1a  |j}...}......e}..|
000000a0  1c e0 bd 1a 6b 7d c0 1a  1c e0 ad 1a 77 7d c0 1a  |....k}......w}..|
000000b0  a9 72 9e 1a 6b 7d c0 1a  1c e0 b8 1a 6b 7d c0 1a  |.r..k}......k}..|
000000c0  52 69 63 68 6a 7d c0 1a  00 00 00 00 00 00 00 00  |Richj}..........|
000000d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000e0  50 45 00 00 64 86 04 00  30 04 ba 49 00 00 00 00  |PE..d...0..I....|

Upvotes: 1

Related Questions