2

I have an assembly, Utils which is set to build as "Any CPU", and I have a managed EXE which is set to also build as "Any CPU". The exe uses my Utils assembly.

My Utils assembly MUST load a unmanaged, non-com 32bit DLL and call some functions.

I am getting an exception "An attempt was made to load a program with an incorrect format. " every time.

Is there any way to still have my EXE and assembly running in 64bit mode but still load the 32bit DLL to make some simple function calls?

I know that if I build my EXE in 32bit mode then it will work, but my Utils assembly is going to be used by a WCF web service and other programs that might run in 64bit mode and it still needs to call the 32bit dll.

Is there any way? I've tried using LoadLibrary, which just returns a 0 pointer when called from 64bit code, and DllImport which gives me an exception.

Thank you for your help.

Andrei B
  • 33
  • 6
  • Expose the 32 bit code as a COM object and access it out-of-process. That's really your only option. – David Heffernan Mar 31 '11 at 17:42
  • If you're using IIS to host WCF, you should try this answer: http://stackoverflow.com/questions/9419403/could-not-load-file-or-assembly-exception/9419522#9419522 – Arman Bimatov Aug 21 '13 at 18:58

3 Answers3

4

No, it is impossible. If you can't get the DLL upgraded to 64-bit then you'll have to do something desperate like hosting it in a 32-bit process and use one of the .NET inter-process communication mechanisms to use it. Like WCF.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

If you can't, or won't, change the process or the assembly to match the other, then no, there is no easy way to do what you want to do.

The only way to interact with a 32-bit assembly from a 64-bit process, or vice versa, is to load the assembly into a separate process and use inter-process calls to talk to it.

The code is fundamentally incompatible between the two cases, so there really is no way.

Either:

  1. Change your program, and all other processes that need to interact with that assembly, to also be 32-bit
  2. Change your assembly to be Any CPU as well
  3. Use the shim process and IRPC, like WCF or similar
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
0

No. Everything must be the same.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445