【发布时间】:2017-04-01 22:28:32
【问题描述】:
我有一个 64 位 VB.Net 应用程序,它需要使用第 3 方 32 位非托管 DLL。
以下是我尝试过的事情:
-
我创建了一个名为 COM1 的 32 位 vb.net 包装类库,并添加了一个调用 32 位非托管 dll 的公开函数的 VB.Net COM 类。 该项目启用了“注册 COM 互操作”。 当我将 32 位 DLL (COM1.dll) 引用到我的 64 位应用程序并执行该应用程序时,我收到以下异常:
无法加载文件或程序集“COM1.dll”...试图加载格式不正确的程序。
-
我创建了一个名为 COM2 的 64 位 vb.net 包装类库,并添加了一个调用 32 位非托管 dll 的 VB.Net COM 类。 该项目启用了“注册 COM 互操作”。 当我在 64 位应用程序中引用 64 位 DLL (COM2.dll) 并执行该应用程序时,我能够加载 64 位 dll,但是当我调用其中一个公开的函数时收到以下异常非托管 dll(通过 64 位包装 dll):
试图加载格式不正确的程序。
- 我也使用 WCF 应用程序尝试了上述步骤,其中我将 COM 包装器替换为 WCF 服务,但我得到了相同的结果。
我了解我无法直接从我的 64 位应用程序调用 32 位 dll。我想做的是通过 IPC 机制调用 32 位 dll——在本例中为 COM 或 WCF。显然,我在这里犯了一些错误。
谁能给我一个工作代码或告诉我在上述步骤中我做错了什么?
我的部分代码:
-
我的 COM 类
<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 -
我的 64 位应用程序
Public Function foo() As Boolean Try COM1.ComClass1.InitializePort(2, 2) Catch ex As Exception MsgBox(ex.ToString) End Try Return True End Function
【问题讨论】: