如何从.NET中定义.NET中定义的COM对象的后期绑定调用(通过DispID)?

时间:2012-02-14 20:31:47

标签: c# .net com interop

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

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

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互操作,它用强密钥签名。

现在我想使用来自另一个.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);
}

第一次调用成功,应用程序打印“通过姓名:我是国王!”。

但是在第二次调用(使用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;
    };
};

我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

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

因此,当您使用InvokeMember()时,您将获得它的System.Reflection版本,而不是IDispatch版本。另一种看待它的方法是使用“getTheTruth”。注意现在第一次尝试失败了。 IDispatch不区分大小写,System.Reflection不区分大小写。 “[DispID = 1030]”技巧不起作用,因为System.Reflection对disp-id不了解。

功能,而不是错误,CLR在可以直接创建.NET对象时创建RCW没有意义。这也不容易实现。