Chr*_*ian 5 .net com interop 32bit-64bit
我在COM Interop遇到了一些麻烦,情况如下:
32位COM Exe服务器(用C++编程)提供了一个类,其中包含一些处理第三方硬件的成员函数(此硬件还将COM Exe Server绑定到32位,因为制造商不支持64-位).
我想在64位.NET(C#)应用程序中使用32位COM Exe Server ...首先,我尝试在Visual Studio 2010中添加对Exe Server的引用,并创建了一个Interop-DLL.这个Interop-DLL为我提供了必要的功能,其中一个被声明为:
int Initialize(ref string callingApplicationPath);
Run Code Online (Sandbox Code Playgroud)
C++中的原始声明如下所示:
LONG Class::Initialize(BSTR* callingApplicationPath)
Run Code Online (Sandbox Code Playgroud)
......在IDL中这样:
[id(1)] LONG Initialize([in] BSTR* callingApplicationPath);
Run Code Online (Sandbox Code Playgroud)
但是,当我想通过Interop-DLL从C#调用此函数时,它会抛出BadImageFormatException.看起来Interop-DLL是一个32位DLL(也许有可能生成64位DLL?).
我的下一次尝试是使用以下代码实例化Exe Server:
Type type = Type.GetTypeFromProgID("OurCompany.Class");
Object o = Activator.CreateInstance(type);
Object[] args = { Marshal.StringToBSTR(str) };
Object result = type.InvokeMember("Initialize", BindingFlags.InvokeMethod, null, o, args);
Run Code Online (Sandbox Code Playgroud)
另一方面,这段代码会抛出一个TargetInvocationException(更具体地说:0x80020005(DISP_E_TYPEMISMATCH)).不幸的是我无法找到我必须从C#传递给函数的类型...我在Marshal类中尝试了所有的StringToXXX函数但似乎没有任何工作:/我想我在这里缺少一些简单的东西,但我看不出来.
任何帮助是极大的赞赏!
最好的祝福
基督教
IDL 声明
[id(1)] LONG Initialize([in] BSTR* str);
Run Code Online (Sandbox Code Playgroud)
没有意义。当您传递 aBSTR作为in参数时,只需“按值”传递它:
[id(1)] LONG Initialize([in] BSTR str);
Run Code Online (Sandbox Code Playgroud)
那么你不需要在 C# 代码中做任何特殊的事情 - 只需传递string到那里,编组就会自动完成。
当然,您还必须更改方法实现签名。