views:

222

answers:

5

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).

+2  A: 

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.

Thorsten Dittmar
At this time, i have only on DLL (32bit one) and there's no chance to have another DLL - 64 bit version, meantime my .NET application has to be compiled as ANY CPU (which considers 64 bit too). soo, isn't there any solution for this case?
scatterbraiin
Well, as Lasse V. Karlsen suggested, you can write a separate 32-bit application (NOT LIBRARY!) which you start as a separate process and "talk to" using inter process communication.
Thorsten Dittmar
A: 

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.

Mikael
I tried, not exactly like that, I wrote Class library, where I imported that Delphi DLL, compiled x86 and then i referenced it to my 64 bit Main Application, but still no luck.
scatterbraiin
Because it is the same problem, only with more complexity. Your 64-bit application can not use a 32-bit class library either! Mikael was talking about a 32-bit application you communicate with through Named Pipes or other IPC methods.
Thorsten Dittmar
I read that it is possible through IPC but to tell you the truth it was not quite clear for me.. Is there any chance you have some instructions about that?? I need more details, because it's really new for me
scatterbraiin
+2  A: 

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.

Lasse V. Karlsen
Can you give me any source how can i do that? Or do you know where can be any Detail explanations?Thanks a lot
scatterbraiin
I'm pretty sure that a 64-bit application can also not use 32-bit COM components, or am I wrong here? I've had this problem before using a 32-bit COM control and got errors until I compiled my C# project as X86.
Thorsten Dittmar
Unfortunatly this is not an option for me :(
scatterbraiin
I think they can use out-of-process COM objects, but I don't really know actually. @scatterbraiin, "what" is not an option for you?
Lasse V. Karlsen
@Lasse V. Karlsen to compile my C# project as X86
scatterbraiin
@Lasse V. Karlsen I think you are right and what you said will solve my problem. thanks a lot
scatterbraiin
+2  A: 

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.

Frank Bollack
A: 

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.

sixlettervariables