hello i'm having a problem, i try to solve it since yesterday but no luck. i have 32 bit delphi dll which i want to import in .NET Win Application. this application has to be built on ANY CPU mode. of course, there's BadImageFormatException coming, which means that in x64 application can't be loaded x86 dll.. i googled around and find solution, it said i have to do wrapper, but it wasn't clear for me. can anybody tell how to solve this problem, is there any possible way i can import 32bit Delphi dll in program builted Any CPU or x64 mode(maybe another solution).
As I understand things, you have no way of using a 32-bit DLL from a 64-bit application. That said, you may compile your application for X86 only.
The solution you found may be about how to use a DLL that exists for both 32- and 64-bit versions in an "Any CPU"-compiled project depending on whether the application is running in a 32- or 64-bit environment.
To do that, you could write two wrapper DLLs in C#, one for 64-bit and one for 32-bit and use the respective wrapper depending on whether you're running on a 64-bit or 32-bit OS.
However, this does not work when all you have is a 32-bit DLL. A 64-bit application can not use 32-bit DLLs, as well as a 32-bit application can not use 64-bit DLLs.
So you either need to compile your application for 32-bit, or you have to create a 64-bit version of your DLL.
A solution although a bit of a mess could be to write a separate 32-bit application that you can talk to from your 64-bit application such as a console application you send commands to/from.
Not pretty but may work if you only need the occasional call to it.
What you have to do is write a wrapper application that hosts the 32-bit DLL file, in a 32-bit process.
Your 64-bit application then has to talk to this 32-bit process, through network means, or by making the DLL functions available through a COM object, or similar.
You can not run a 32-bit DLL inside a 64-bit process, no matter how hard you try, so you need to run it in a 32-bit process.
If compiling your application for 32-bit only is not an option, you have no choice but to create a host application.
A general idea could be to wrap your (unmanaged) 32-bit DLL with a managed 32-bit wrapper dll and make it COM visible. This allows calls to your wrapper DLL via its COM interface.
You can than use a COM surrogate to make your COM dll appear as an out of process COM server. Take a look at this SO question for some further information on this topic: Access x86 COM from x64 .NET.
Just compile your .Net Application as Platform x86. It will run on x64 machines and it will use your 32bit DLL. Don't waste time on a wrapper.