gpt4 book ai didi

c# - 如何从 .NET 执行 .NET 中定义的 COM 对象的后期绑定(bind)调用(通过 DispID)?

转载 作者:行者123 更新时间:2023-11-30 22:31:22 26 4
gpt4 key购买 nike

我想使用 DispID 从另一个 .NET 应用程序调用在 .NET 中定义的 COM 对象上的方法。

我在 .NET 项目中定义了以下类和接口(interface):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace TestComObject
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[Guid("99476c04-574a-4271-abc8-f1d2615c3d93")]
public interface IKnowEverything
{
[DispId(1030)]
string GetTheTruth();
}

[ComVisible(true)]
[Guid("9B869BAF-622A-458B-BF80-2CD6D8A3C541")]
[ClassInterface(ClassInterfaceType.None)]
public class AllKnowing : IKnowEverything
{
public string GetTheTruth()
{
return "I am King!";
}
}
}

我构建它并将其设置为注册 COM 互操作,它使用强 key 进行签名。

现在我想使用来自另一个 .NET 应用程序的 DispID 调用此方法。我写了如下测试方法:

static void Main(string[] args)
{
Guid clsid = new Guid("9B869BAF-622A-458B-BF80-2CD6D8A3C541");
Type type = Type.GetTypeFromCLSID(clsid, true);
object instance = Activator.CreateInstance(type);

string methodName = "GetTheTruth";
string methodDispIdName = "[DispID=1030]";

string s1 = (string)type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, instance, null);
Console.WriteLine("Via name: {0}", s1);

string s2 = (string)type.InvokeMember(methodDispIdName, BindingFlags.InvokeMethod, null, instance, null);
Console.WriteLine("Via DispID: {0}", s2);
}

第一次调用成功,应用程序打印“Via name: I am King!”。

但是在第二次调用时(使用 DispID)我得到一个异常:

未处理的异常:System.MissingMethodException:未找到方法“TestComObject.AllKnowing.[DispID=1030]”。

在 OLE-COM 对象查看器中查看类型库时,我觉得它没问题:

// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: TestComObject.tlb

[
uuid(139DAA99-BF76-4057-BCC8-DCB35C153920),
version(1.0),
custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "TestComObject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=58b6bd22fbd98427")

]
library TestComObject
{
// TLib : // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
importlib("mscorlib.tlb");
// TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
importlib("stdole2.tlb");

// Forward declare all types defined in this typelib
dispinterface IKnowEverything;

[
uuid(99476C04-574A-4271-ABC8-F1D2615C3D93),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "TestComObject.IKnowEverything")

]
dispinterface IKnowEverything {
properties:
methods:
[id(0x00000406)]
BSTR GetTheTruth();
};

[
uuid(9B869BAF-622A-458B-BF80-2CD6D8A3C541),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "TestComObject.AllKnowing")
]
coclass AllKnowing {
interface _Object;
[default] dispinterface IKnowEverything;
};
};

我在这里做错了什么?

最佳答案

这不起作用,因为您在 .NET 中实现了 COM 服务器。 instance 成员是实际的 .NET 对象,而不是 RCW。从调试器可以看出,它不是 System.__ComObject,而是实际 .NET 类型的对象。

因此,当您使用 InvokeMember() 时,您会得到它的 System.Reflection 版本,而不是 IDispatch 版本。另一种查看方式是使用“getTheTruth”。请注意第一次尝试现在也是如何失败的。 IDispatch 不区分大小写,而 System.Reflection 则不是。 “[DispID=1030]”技巧不起作用,因为 System.Reflection 对 disp-id 一无所知。

Feature,不是bug,CLR在可以直接创建.NET对象的情况下创建RCW是没有意义的。这也不容易实现。

关于c# - 如何从 .NET 执行 .NET 中定义的 COM 对象的后期绑定(bind)调用(通过 DispID)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9283761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com