使用 C++ C# 中的 COM 接口,适用于客户端和服务器

发布于 2024-07-08 21:54:38 字数 2525 浏览 5 评论 0原文

我需要让一段 C# 代码通过 COM 与各种实现进行交互。

为了使该集成的用户更轻松,我将交互的接口包含在 IDL 中(作为相关现有 DLL 的一部分,但没有组件类或实现),然后通过运行 Tlbimp 创建类型定义将其添加到我的 C# 代码中。

我实现了 C#,根据 Windows 注册表信息创建 COM 对象并将该对象转换为我需要的接口。

然后,我在一个单独的项目中创建了该接口的 C# 实现并注册了它。 主程序正确创建测试 COM 对象,但无法将其转换为接口(使用 C# 'as' 时获取空对象,获取显式转换的 InvalidCastException)。

有人可以建议为什么接口未被识别为由测试对象实现吗?

这是 IDL 中的接口定义(在 VS 2005 中用 C++ 编译):

    [
    object,
    uuid(B60C546F-EE91-48a2-A352-CFC36E613CB7),
    dual,
    nonextensible,
    helpstring("IScriptGenerator Interface"),
    pointer_default(unique)
]
interface IScriptGenerator : IDispatch{

    [helpstring("Init the Script generator")] 
        HRESULT Init();
    [helpstring("General purpose error reporting")] 
        HRESULT GetLastError([out] BSTR *Error);
};

这是 Tlbimp 为 C# 创建的存根:

[TypeLibType(4288)]
[Guid("B60C546F-EE91-48A2-A352-CFC36E613CB7")]
  public interface IScriptGenerator
  {
    [DispId(1610743813)]
    void GetLastError(out string Error);
    [DispId(1610743808)]
    void Init();
  }

这是主 C# 代码的一部分,通过其 ProgID 创建 COM 对象并将其转换为 IScriptGenerator 接口:

public ScriptGenerator(string GUID)
{
  Type comType = Type.GetTypeFromProgID(GUID);
  object comObj = null;
  if (comType != null)
  {
    try
    {
      comObj = Activator.CreateInstance(comType);
    }
    catch (Exception ex)
    {
      Debug.Fail("Cannot create the script generator COM object due to the following exception: " + ex, ex.Message + "\n" + ex.StackTrace);
      throw ex;
    }
  }
  else
    throw new ArgumentException("The GUID does not match a registetred COM object", "GUID");

  m_internalGenerator = comObj as IScriptGenerator;
  if (m_internalGenerator == null)
  {
    Debug.Fail("The script generator doesn't support the required interface - IScriptGenerator");
    throw new InvalidCastException("The script generator with the GUID " + GUID + " doesn't support the required interface - IScriptGenerator");
  }

}

这是实现 C# 代码,用于测试它是否工作(也不是):

  [Guid("EB46E31F-0961-4179-8A56-3895DDF2884E"),
  ProgId("ScriptGeneratorExample.ScriptGenerator"),
  ClassInterface(ClassInterfaceType.None),
  ComSourceInterfaces(typeof(SOAAPIOLELib.IScriptGeneratorCallback))]
  public class ScriptGenerator : IScriptGenerator
  {
   public void GetLastError(out string Error)
    {
      throw new NotImplementedException();
    }
    public void Init()
    {
      // nothing to do
    }
  }

I need to make a piece of C# code interact through COM with all kinds of implementations.

To make it easeier for users of that integration, I included the interacted interfaces in IDL (as part of a relevant existing DLL, but without coclass or implementation), then got that into my C# code by running Tlbimp to create the types definition.

I implemented my C#, creating COM objects based on Windows registry info and casting the object into the interface I need.

I then created a C# implementation of the interface in a seperate project and registered it.
The main program creates the testing COM object correctly but fails to cast it into the interface (gets a null object when using C# 'as', gets an InvalidCastException of explicit cast).

Can someone suggest why the interface is not identified as implemented by the testing object?

This is the interface defition in IDL (compiled in C++ in VS 2005):

    [
    object,
    uuid(B60C546F-EE91-48a2-A352-CFC36E613CB7),
    dual,
    nonextensible,
    helpstring("IScriptGenerator Interface"),
    pointer_default(unique)
]
interface IScriptGenerator : IDispatch{

    [helpstring("Init the Script generator")] 
        HRESULT Init();
    [helpstring("General purpose error reporting")] 
        HRESULT GetLastError([out] BSTR *Error);
};

This is the stub created for C# by Tlbimp:

[TypeLibType(4288)]
[Guid("B60C546F-EE91-48A2-A352-CFC36E613CB7")]
  public interface IScriptGenerator
  {
    [DispId(1610743813)]
    void GetLastError(out string Error);
    [DispId(1610743808)]
    void Init();
  }

This is part of the main C# code, creating a COM object by its ProgID and casting it to the IScriptGenerator interface:

public ScriptGenerator(string GUID)
{
  Type comType = Type.GetTypeFromProgID(GUID);
  object comObj = null;
  if (comType != null)
  {
    try
    {
      comObj = Activator.CreateInstance(comType);
    }
    catch (Exception ex)
    {
      Debug.Fail("Cannot create the script generator COM object due to the following exception: " + ex, ex.Message + "\n" + ex.StackTrace);
      throw ex;
    }
  }
  else
    throw new ArgumentException("The GUID does not match a registetred COM object", "GUID");

  m_internalGenerator = comObj as IScriptGenerator;
  if (m_internalGenerator == null)
  {
    Debug.Fail("The script generator doesn't support the required interface - IScriptGenerator");
    throw new InvalidCastException("The script generator with the GUID " + GUID + " doesn't support the required interface - IScriptGenerator");
  }

}

And this is the implementing C# code, to test it's working (and it's not):

  [Guid("EB46E31F-0961-4179-8A56-3895DDF2884E"),
  ProgId("ScriptGeneratorExample.ScriptGenerator"),
  ClassInterface(ClassInterfaceType.None),
  ComSourceInterfaces(typeof(SOAAPIOLELib.IScriptGeneratorCallback))]
  public class ScriptGenerator : IScriptGenerator
  {
   public void GetLastError(out string Error)
    {
      throw new NotImplementedException();
    }
    public void Init()
    {
      // nothing to do
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姜生凉生 2024-07-15 21:54:38

再次感谢您的建议。

我最终能够自己解决这个问题。 我尝试了上述建议,但没有取得任何进展。 然后我更改了“测试”代码中互操作的命名空间 - 它与主代码中的命名空间不同,因为使用 Tlbimp 时使用了不同的参数。 这解决了问题。

我的猜测是:.Net 创建了 COM 对象,但是当它检测到这实际上是一个 .Net 对象时,它会绕过 COM 层并直接进行通信。 在这种情况下,不会使用 queryInterface(带有接口 GUID),并且由于 C# 命名空间不同,接口也会有所不同。

这意味着为了支持与 .Net 代码的集成,我需要在 IDL 之外发布我的原始互操作程序集。

谢谢,
因巴尔

Again - thanks for the suggestions.

I was able to finally resolve the issue on my own. I tried the above suggestions and didn't made any progress. Then I changed the namespace of the interop in the 'testing' code - it varied from the one in the main code because of different argument use when using Tlbimp. This solved the problem.

Here's my guess to why: .Net creates the COM object, but when it detects this is actually a .Net object, it bypass the COM layer and communicates directly. In which case, queryInterface (with the interface GUID) is not used and the interface do differ because of different C# namespaces.

This means that in order to supprot integration with .Net code, I will need to publish my original interop assembly aside the IDL.

Thanks,
Inbar

宁愿没拥抱 2024-07-15 21:54:38

我想你在界面上需要这个

[InterfaceType(ComInterfaceType.InterfaceIsDual)]

I think you need this on the interface

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文