C#COM Interop:使用托管对象作为参数公开函数

时间:2013-06-25 13:53:01

标签: c# c++ pointers com variant

我浏览了tutorial of C# COM INterop。执行链接成功运行时给出的演示示例!理解那里出现的每一个陈述,它们的含义。 并遇到了the equivalent data types for c# objects.

除了Adam Nathan的.NET和COM互操作性以及Nishant Shivakumar的CCLI in Action之外,我找不到任何好书(有关C#COM互操作和从本机C ++调用COM组件的示例)。我在注册程序集和其他东西方面没有问题,但在COM语法中遇到问题。 在解释我的代码之前,我想了解VARIANT是什么。为什么会这样?

这是我的情况,我有一个C#类如下:

using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace ManagedDLL
{
    // Interface declaration.
    [ComVisible(true), Guid("3265C537-E149-4559-B4E1-DBE334927DFA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface ICalculator
    {
        int Add(int Number1, int Number2);
        int Subtract(Subtraction sb);
    };
    // Interface implementation.
    [ComVisible(true), Guid("0F50D3BE-CEEA-4C57-9882-4A609C7BB36C")]
    public class ManagedClass : ICalculator
    {
        private int a, b;
        public int Add(int Number1, int Number2)
        {
            return Number1 + Number2;
        }
        public int Subtract(Subtraction sub)
        {
            int a = 10, b = 3;
            return sub.SubtractTwoNumbers(a, b);
        }

    }
    [ComVisible(true)]
    [Guid("C718EDDE-541C-4D15-B7EA-0533AB11A839")]
    [ClassInterface(ClassInterfaceType.None)]
    public class Subtraction
    {
        public int SubtractTwoNumbers(int a, int b)
        {
            return a - b;
        }

    }
}

导入tlb文件后得到.tlh文件,如下所示:

#pragma once
#pragma pack(push, 8)

#include <comdef.h>

namespace ManagedDLL {

//
// Forward references and typedefs
//

struct __declspec(uuid("4e5098b7-4e51-45e5-a705-a7e3c51e2a80"))
/* LIBID */ __ManagedDLL;
struct __declspec(uuid("3265c537-e149-4559-b4e1-dbe334927dfa"))
/* interface */ ICalculator;
struct /* coclass */ ManagedClass;
struct /* coclass */ Subtraction;
struct __declspec(uuid("c8e9181c-f064-3ec1-869e-042c6fdd3e46"))
/* dual interface */ _ManagedClass;

//
// Smart pointer typedef declarations
//

_COM_SMARTPTR_TYPEDEF(ICalculator, __uuidof(ICalculator));
_COM_SMARTPTR_TYPEDEF(_ManagedClass, __uuidof(_ManagedClass));

//
// Type library items
//

struct __declspec(uuid("3265c537-e149-4559-b4e1-dbe334927dfa"))
ICalculator : IUnknown
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall Add (
        /*[in]*/ long Number1,
        /*[in]*/ long Number2,
        /*[out,retval]*/ long * pRetVal ) = 0;
      virtual HRESULT __stdcall Subtract (
        /*[in]*/ struct _Object * sb,
        /*[out,retval]*/ long * pRetVal ) = 0;
};

struct __declspec(uuid("0f50d3be-ceea-4c57-9882-4a609c7bb36c"))
ManagedClass;
    // [ default ] interface _ManagedClass
    // interface _Object
    // interface ICalculator

struct __declspec(uuid("c718edde-541c-4d15-b7ea-0533ab11a839"))
Subtraction;
    // [ default ] interface _Object

struct __declspec(uuid("c8e9181c-f064-3ec1-869e-042c6fdd3e46"))
_ManagedClass : IDispatch
{};

//
// Named GUID constants initializations
//

extern "C" const GUID __declspec(selectany) LIBID_ManagedDLL =
    {0x4e5098b7,0x4e51,0x45e5,{0xa7,0x05,0xa7,0xe3,0xc5,0x1e,0x2a,0x80}};
extern "C" const GUID __declspec(selectany) IID_ICalculator =
    {0x3265c537,0xe149,0x4559,{0xb4,0xe1,0xdb,0xe3,0x34,0x92,0x7d,0xfa}};
extern "C" const GUID __declspec(selectany) CLSID_ManagedClass =
    {0x0f50d3be,0xceea,0x4c57,{0x98,0x82,0x4a,0x60,0x9c,0x7b,0xb3,0x6c}};
extern "C" const GUID __declspec(selectany) CLSID_Subtraction =
    {0xc718edde,0x541c,0x4d15,{0xb7,0xea,0x05,0x33,0xab,0x11,0xa8,0x39}};
extern "C" const GUID __declspec(selectany) IID__ManagedClass =
    {0xc8e9181c,0xf064,0x3ec1,{0x86,0x9e,0x04,0x2c,0x6f,0xdd,0x3e,0x46}};

} // namespace ManagedDLL

#pragma pack(pop)

最后这是我的原生C ++应用程序:

HRESULT hr = CoInitialize(NULL);
ManagedDLL::ICalculatorPtr ptrInterface(__uuidof(ManagedDLL::ManagedClass));
IUnknown* pUnk=NULL;
hr=CoCreateInstance(__uuidof(ManagedDLL::Subtraction), NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void **)pUnk);
long lResult;
ptrInterface->Subtract(pUnk, &lResult); //what should be written instead of pUnk, because it expects a _Object* as evident in the tlh file
return  lResult;

问题丰富:

  1. 如何访问界面中的Subtract方法?我们应该如何实例化Subtraction类,其对象应该作为此Subtract方法中的参数传递?

  2. 如何收集返回COM类作为返回类型的函数的值?

  3. 什么是IUnknownIDispatch

  4. 最重要的是,为什么_Object*作为参数创建在COM .tlh文件而不是Subtraction*作为参数?

1 个答案:

答案 0 :(得分:2)

您提出的问题是关于COM的经典基本问题,可能需要一本小书才能回答。我建议你得到关于COM的教程或书籍,它会让你加快速度。在了解COM基础知识之前,您不会理解互操作。从那里开始,然后解决更难的事情。

在所有相关问题上加快速度需要一些时间,所以不要匆忙!

这是一个开始的地方: http://msdn.microsoft.com/en-us/library/727z646z(v=vs.80).aspx