How can I test a Windows DLL file to determine if it is 32 bit or 64 bit? -
this question has answer here:
i'd write test script or program asserts dll files in given directory of particular build type.
i use sanity check @ end of build process on sdk make sure 64-bit version hasn't somehow got 32-bit dll files in , vice versa.
is there easy way @ dll file , determine type?
the solution should work on both xp32 , xp64.
gory details
a dll uses pe executable format, , it's not tricky read information out of file.
see msdn article on pe file format overview. need read ms-dos header, read image_nt_headers structure. contains image_file_header structure contains info need in machine member contains 1 of following values
- image_file_machine_i386 (0x014c)
- image_file_machine_ia64 (0x0200)
- image_file_machine_amd64 (0x8664)
this information should @ fixed offset in file, i'd still recommend traversing file , checking signature of ms-dos header , image_nt_headers sure cope future changes.
use imagehelp read headers...
you can use imagehelp api - load dll loadimage , you'll loaded_image structure contain pointer image_nt_headers structure. deallocate loaded_image imageunload.
...or adapt rough perl script
here's rough perl script gets job done. checks file has dos header, reads pe offset image_dos_header 60 bytes file.
it seeks start of pe part, reads signature , checks it, , extracts value we're interested in.
#!/usr/bin/perl # # usage: petype <exefile> # $exe = $argv[0]; open(exe, $exe) or die "can't open $exe: $!"; binmode(exe); if (read(exe, $doshdr, 64)) { ($magic,$skip,$offset)=unpack('a2a58l', $doshdr); die("not executable") if ($magic ne 'mz'); seek(exe,$offset,seek_set); if (read(exe, $pehdr, 6)){ ($sig,$skip,$machine)=unpack('a2a2v', $pehdr); die("no pe executable") if ($sig ne 'pe'); if ($machine == 0x014c){ print "i386\n"; } elsif ($machine == 0x0200){ print "ia64\n"; } elsif ($machine == 0x8664){ print "amd64\n"; } else{ printf("unknown machine type 0x%lx\n", $machine); } } } close(exe);
Comments
Post a Comment