views:

285

answers:

4

I am having a C# (.NET 3.5, VS2005 Professional) application that uses unmanaged 32bit library written in C/C++. API that I use is like this:

void * Initialize(int x);

voic GetData(void *);

And this works when I run it on Windows XP 32bit, but on Windows XP64bit it throws exception:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at Aktuelizator.CommonLibrary.InitializeRingBuffer(Int32 dim) at Aktuelizator.AktuelizatorWService.AktuelizatorWS..ctor()

These unmanaged DLL's work under 64bit XP when called from 32bit unmanages application writetn in C/C++.

Does anyone have any idea?

+1  A: 

It sounds like your DLL is only compiled to 32 bit, but you try to call it from both a 32 bit and 64 bit process. The former will work, of course. The later, however, won't. 32 bit DLLs can only be used in 32 bit processes. Try compiling the DLL to a 64 bit target and let the C# app use that one.

eran
Yes it is, but it works when it is called from 32bit compiled application run on XP64bit. Can it be done without recompiling dll?
Mita
A 32 bit executable running on a 64 bit OS thinks it's running in the 32 bit world - Google "WOW64" for more. Try launching the app from a 32 bit environment, such as C:\Windows\SysWOW64\cmd.exe, so the process will be a 32 bit one, like the DLL.
eran
BTW, my suggestion applies in case you have to make do with the app and DLL you have, hence need a workaround. If you can rebuild the app, Stephen Martin's solution will solve the problem rather than bypass it.
eran
A: 

Check all parameters that are defined as Int32, that should in fact be IntPtr.

leppie
I double checked it, it didn't help
Mita
+2  A: 

Your build configuration Platform is set to 'Any CPU', that means on a 64 bit OS it runs as 64 bit and you can't load the dll. Set it instead to x86, this will force it to run as 32 bit regardless of OS and your dll will load fine.

Stephen Martin
This solved the problem!Thanks!
Mita
A: 

Stephen Martin's suggestion fixed the problem. I compiled dll's for x86.

Mita