我学习了C#COM INterop的教程。执行了链接中给出的演示示例成功运行!理解那里的每一句话,它们的含义。并发现了c#对象的等效数据类型。
除了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;
问题很多:
如何访问接口中的
Subtract
方法?在这个Subtract
方法中,我们应该如何实例化其对象应该作为参数传递的Subtraction
类?如何收集返回COM类作为返回类型的函数的值?
什么是
IUnknown
和IDispatch
?最重要的是,为什么
_Object*
被创建为COM.tlh
文件中的参数,而不是Subtraction*
作为参数?
您所问的问题是关于COM的经典、基本的问题,可能需要一本小书才能回答。我建议你找一本关于COM的教程或书,让你跟上进度。只有了解COM基础知识,才能理解互操作。从那里开始,然后完成更难的事情。
了解所有涉及的问题需要一些认真的时间,所以不要着急!
从这里开始:http://msdn.microsoft.com/en-us/library/727z646z(v=vs.80).aspx