« Sum up values in JSON… | Home | MBS Sessions at FileM… »

Detect 32 vs. 64bit DLL in Xojo


Today we got some code to detect whether a DLL is 32-bit or 64-bit. That can be useful if your app requires the user to install a custom DLL, e.g. mysql DLL for use with our MBS Xojo SQL Plugin.

Technically you need to read the DOS header from the DLL/EXE file, check where the PE header starts and lookup the field for CPU architecture. If something is not supported, we ignore it, like a wrong position value and if the MZ or PE texts are not there. As you see, we can easily do that with binary stream class in Xojo and Microsoft defines a lot of constants for the various possible architectures.

If you find a DLL from a Windows RT copy, please let me know if it detects ARM as target. Thanks.

EventHandler Sub DropObject(obj As DragItem, action As Integer) // constants defined in WinNT.h C header file const IMAGE_FILE_MACHINE_I386 = &h014c // Intel 386. const IMAGE_FILE_MACHINE_R3000 = &h0162 // MIPS little-endian, = &h160 big-endian const IMAGE_FILE_MACHINE_R4000 = &h0166 // MIPS little-endian const IMAGE_FILE_MACHINE_R10000 = &h0168 // MIPS little-endian const IMAGE_FILE_MACHINE_WCEMIPSV2 = &h0169 // MIPS little-endian WCE v2 const IMAGE_FILE_MACHINE_ALPHA = &h0184 // Alpha_AXP const IMAGE_FILE_MACHINE_SH3 = &h01a2 // SH3 little-endian const IMAGE_FILE_MACHINE_SH3DSP = &h01a3 const IMAGE_FILE_MACHINE_SH3E = &h01a4 // SH3E little-endian const IMAGE_FILE_MACHINE_SH4 = &h01a6 // SH4 little-endian const IMAGE_FILE_MACHINE_SH5 = &h01a8 // SH5 const IMAGE_FILE_MACHINE_ARM = &h01c0 // ARM Little-Endian const IMAGE_FILE_MACHINE_THUMB = &h01c2 // ARM Thumb/Thumb-2 Little-Endian const IMAGE_FILE_MACHINE_ARMNT = &h01c4 // ARM Thumb-2 Little-Endian const IMAGE_FILE_MACHINE_AM33 = &h01d3 const IMAGE_FILE_MACHINE_POWERPC = &h01F0 // IBM PowerPC Little-Endian const IMAGE_FILE_MACHINE_POWERPCFP = &h01f1 const IMAGE_FILE_MACHINE_IA64 = &h0200 // Intel 64 const IMAGE_FILE_MACHINE_MIPS16 = &h0266 // MIPS const IMAGE_FILE_MACHINE_ALPHA64 = &h0284 // ALPHA64 const IMAGE_FILE_MACHINE_MIPSFPU = &h0366 // MIPS const IMAGE_FILE_MACHINE_MIPSFPU16 = &h0466 // MIPS const IMAGE_FILE_MACHINE_TRICORE = &h0520 // Infineon const IMAGE_FILE_MACHINE_CEF = &h0CEF const IMAGE_FILE_MACHINE_EBC = &h0EBC // EFI Byte Code const IMAGE_FILE_MACHINE_AMD64 = &h8664 // AMD64 (K8) const IMAGE_FILE_MACHINE_M32R = &h9041 // M32R little-endian const IMAGE_FILE_MACHINE_ARM64 = &hAA64 // ARM64 Little-Endian const IMAGE_FILE_MACHINE_CEE = &hC0EE // check all dropped files do if obj.FolderItemAvailable then dim f as FolderItem = obj.FolderItem if f <> nil then List.AddRow f.name dim b as BinaryStream = BinaryStream.Open(f) if b <> nil then if b.read(2) = "MZ" then // is DLL/EXE file? b.Position = 60 // position where to find position of PE header b.LittleEndian = true dim pos as Integer = b.ReadInt32 if pos >= 0 and pos < b.Length then b.Position = pos // read PE header dim PE as integer = b.ReadInt32 if pe = &h4550 then // "PE"+chr(0)+chr(0) // now check architecture dim Arch as integer = b.ReadUInt16 dim Type as string = "" Select case arch case IMAGE_FILE_MACHINE_I386 Type = "32-bit x86" case IMAGE_FILE_MACHINE_IA64 Type = "64-bit Itanium" case IMAGE_FILE_MACHINE_AMD64 Type = "64-bit x86" case IMAGE_FILE_MACHINE_ARM, IMAGE_FILE_MACHINE_THUMB, IMAGE_FILE_MACHINE_ARMNT Type = "32-bit ARM" case IMAGE_FILE_MACHINE_ARM64 Type = "64-bit ARM" else Type = hex(arch) end Select list.Cell(List.LastIndex,1) = type end if end if end if end if end if end if loop until not obj.NextItem End EventHandler
11 05 18 - 21:35
No comments

  
Remember personal info?

Emoticons / Textile


Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.