2

From inside my Java application, I wish to find out if the user-defined dll is 32 or 64 bit before attempting to load it.

I know (programmatically) whether the JVM on which my application is running is 32 or 64 bit. I also know (programmatically) if the host is Windows or Linux.

How can I programmatically determine whether a dll is 32 or 64 bit before calling "System.loadLibrary()" method?

Santosh Tiwari
  • 1,167
  • 2
  • 14
  • 26
  • 3
    Try to load it and inspect the exception it throws when loading fails? – millimoose Nov 21 '12 at 17:04
  • Thanks. Yes, I do get an exception if something is wrong (dependencies not satisfied, bitness mismatch,...). Does the exception (trace) contain sufficient relevant information for me to infer to reliably infer the bitness of the dll? – Santosh Tiwari Nov 21 '12 at 17:06
  • possible duplicate of [How to detect that a given PE file (exe or dll) is 64 bit or 32 bit](http://stackoverflow.com/questions/1153090/how-to-detect-that-a-given-pe-file-exe-or-dll-is-64-bit-or-32-bit) – Tony Hopkinson Nov 21 '12 at 17:07
  • On Linux you can use `file {library}`, perhaps Windows has something similar. – Peter Lawrey Nov 21 '12 at 17:07
  • 1
    @SantoshTiwari I'm honestly not sure. It's just that any less hackish method would require using some library that can parse the .DLL / ELF / whatever binary format, and using the correct one for a given platform, and that just seems like too much work just to get a precise result. – millimoose Nov 21 '12 at 17:08
  • @TonyHopkinson Can you include how you would do that in Java? – Peter Lawrey Nov 21 '12 at 17:08
  • 1
    @PeterLawrey The accepted answer also doesn't work for DLLs, apparently. – millimoose Nov 21 '12 at 17:09
  • Not a java guy, so I wouldn't. Besides I'm a keep it brutally simple sort of guy, I'd put 64 in the name and get on with something more interesting or important. – Tony Hopkinson Nov 21 '12 at 22:18
  • Why do you need to know? If the DLL loads, use it, otherwise report the error and exit. – user207421 Nov 22 '12 at 01:56

1 Answers1

0

On windows this requires a pipe like this one

dumpbin /headers lib.dll | findstr /i "machine magic"

this is using the dumpbin utility that usually comes installed with Visual studio, findstr is a system utility, i don't know if there is a refistributable version of dumpbin.

Just execute this command from java and apply some basic comparison check to the result.

user1797612
  • 812
  • 6
  • 22
  • If I have to launch a separate process then I know how to do it from C/C++ using Win32 API. I was wondering if Java has some inbuilt functionality (API) to detect the bitness of a library. – Santosh Tiwari Nov 21 '12 at 17:13
  • 1
    @SantoshTiwari the file format used by Windows in this case it's called PE http://en.wikipedia.org/wiki/Portable_Executable , maybe if you will google "java pe header" you will find something useful for you or some guy who already wroted an utility like this one in java. – user1797612 Nov 21 '12 at 17:17