I have a 64-bit VB.Net application which needs to use a 3rd party 32-bit unmanaged DLL.
Here are the things I've tried:
I created a 32-bit vb.net wrapper class library, called COM1, and added a VB.Net COM class that calls the 32-bit unmanaged dll's exposed functions. The project had "Register for COM interop" enabled. When I referenced the 32-bit DLL (COM1.dll) to my 64-bit application and executed the application, I received the following exception:
Could not load file or assembly 'COM1.dll'...An attempt was made to load a program with an incorrect format.
I created a 64-bit vb.net wrapper class library, called COM2, and added a VB.Net COM class that calls the 32-bit unmanaged dll. The project had "Register for COM interop" enabled. When I referenced the 64-bit DLL (COM2.dll) in my 64-bit application and executed the application, I was able to load the 64-bit dll but I received the following exception when I called one of the functions exposed in the unmanaged dll (via the 64-bit wrapper dll):
An attempt was made to load a program with an incorrect format.
- I tried the above steps using a WCF application as well where I replace the COM wrapper with the WCF service, but I get the same result.
I understand that I cannot call a 32-bit dll directly from my 64-bit application. What I am trying to do is call the 32-bit dll through the IPC mechanism — in this case COM or WCF. Obviously, I am making some mistake here.
Can someone give me a working code or tell me what am I doing incorrect in the above-mentioned steps?
Part of my code:
My COM class
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _ Public Class ComClass1 Public Declare Sub InitializePort Lib "I2CDrvrs" (ByVal I2cAddr As Byte, ByVal evalBoardUsed As Byte) #Region "COM GUIDs" ' These GUIDs provide the COM identity for this class ' and its COM interfaces. If you change them, existing ' clients will no longer be able to access the class. Public Const ClassId As String = "5da6d3a4-848c-42b1-bc7c-4079ec5457b1" Public Const InterfaceId As String = "8de9508b-fda6-496e-bb29-a90dc5282d2c" Public Const EventsId As String = "cfec40ff-fec0-4250-9d72-9d63f1e37d21" #End Region ' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() End Sub End Class
My 64-bit application
Public Function foo() As Boolean Try COM1.ComClass1.InitializePort(2, 2) Catch ex As Exception MsgBox(ex.ToString) End Try Return True End Function