Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The .NET/Link tutorial shows how to call functions defined in DLLs. The example uses the GetTickCount Win32 API function

<< NetLink`
InstallNET[]
getTickCount = DefineDLLFunction["GetTickCount", "kernel32.dll", "int", {}]

getTickCount[]

(* ==> 91226108 *)

Unfortunately this does not work by default when usign a 64-bit version of Mathematica to call a function defined in a 32-bit DLL. Let's use the 32-bit version of kernel32.dll to test:

getTickCount = 
 DefineDLLFunction["GetTickCount", "c:\\windows\\SysWOW64\\kernel32.dll", "int", {}]

getTickCount[]

NET::netexcptn: A .NET exception occurred: System.BadImageFormatException: 
   An attempt was made to load a program with an incorrect format. 
   (Exception from HRESULT: 0x8007000B)
   at Wolfram.NETLink.DynamicDLLNamespace.DLLWrapper15.GetTickCount().

How can I call a 32-bit DLL from a 64-bit Mathematica?

share|improve this question
    
I thought this will be valuable for the community. It's based on a MathGroup question I solved. –  Szabolcs Mar 2 '12 at 10:13

2 Answers 2

Or simply use

ReinstallNET["Force32Bit" -> True]

which is a convenience function that calls

UninstallNET[]
InstallNET["Force32Bit" -> True]
share|improve this answer
    
I indeed learned ReinstallNET["Force32Bit"->True] in Integrating C and Mathematica by Joel F. Klein and Todd Gayley –  unstable May 18 '12 at 8:54
up vote 21 down vote accepted

The solution is forcing .NET/Link to load its 32-bit executable instead of the 64-bit one. Since Mathematica communicated with the .NET/Link process through MathLink, it does not matter if the Mathematica kernel is 64 bit and the .NET/Link executable is a 32 bit version. They are separate processes. However, the .NET/Link executable must match the DLL that is being loaded.

There is an undocumented option to force loading the 32-bit version of .NET/Link:

UninstallNET[]
InstallNET["Force32Bit" -> True]

Now 32-bit DLLs can be loaded through .NET/Link, but 64-bit ones cannot.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.