Third-party library requires 32-bit application?

0

Dear Internet community.

I am looking into an issue for a client who is experiencing problems with migrating to 64-bit based Windows 7 client machines. One of their programs is dependent on a third-party library that kicks up a fuss if the current application is not 32-bit.

MyClientApplication calls into MyThirdPartyLibrary0.dll, which in turn calls into MyThirdPartyLibrary1.dll. In the 32-bit enviroment I'd typically do this:

var myObject = New MyThirdPartyLibrary0.MyClass(); myObject.MyMethod();

And everything is great.

On the 64-bit environment I get the following exception:

System.BadImageFormatException: Could not load file or assembly 'MyThirdPartyLibrary1.dll' or one of its dependencies.  is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)
File name: 'MyThirdPartyLibrary1.dll'
   at MyThirdPartyLibrary1.MyInnerClass.MyMethod()
   at MyThirdPartyLibrary0.MyClass.MyMethod()
   at MyApplication.Program.Main(String[] args)

On my developer client I run the same code in an nUnit test and it produces the same BadImageFormatException; if I run it using Microsoft.VisualStudio.QualityTools.UnitTestFramework then it runs OK. I assume this is because VS 2010 is a 32-bit application?

I notice that when using Microsoft.VisualStudio.QualityTools.UnitTestFramework I cannot run the tests if I switch over to using Patform = 64 bit - it appears these tests cannot run under those conditions.

I've not been able to "force" MyApplication to sucessfully execute MyMethod. I suspect the root cause here is that MyThirdPartyLibrary1.dll cannot run within a 64-bit application context, but I can't figure out a way to "force" it to run in a 32-bit context: Swapping between Platform = Any CPU, x86 and 64-bit all yield the same BadImageFormatException, as does playing around with the compatibility settings. Also, the "Troubleshoot compatibility" option suggests running the application in the Windows XP (SP 2) mode: Same result.

It seems to me that I need to find a way to compile MyApplication as a bona-fide 32-bit application or something, but I can't seem to figure out how to do this.

Any help would be appreciated, KS

.net
64-bit
32-bit
asked on Stack Overflow Mar 1, 2012 by kenosis stern

1 Answer

2

You cannot load a 32-bit assembly into a running 64-bit process, or a 64-bit assembly into a running 32-bit process, not even into a separate application domain. The reason you see it working with NUnit is that NUnit runner is a 32-bit application. I ran into a similar issue when my assembly failed to run inside NUnit because of a reference to a 64-bit C++ assembly. I ended up compiling my C++ code twice, and loading the needed library dynamically, based on a sizeof(int) check in an unsafe context (a complete hack).

If you must interact with a 32-bit third-party DLL from your 64-bit application, the only option that I know is to write a wrapper that runs in a separate 32-bit host process, and communicates to your application using the interprocess communication facilities. This approach brings some important performance implications with it, so you need to think twice before adopting it.


User contributions licensed under CC BY-SA 3.0