我是COM的新手,我在互联网上找到了如何在C++中创建一个COM类(以被C#使用)。我发现在.idl文件中,我必须像这样填写:
[
object,
uuid(a93164ee-e6d4-44be-aa27-f00ce6972719),
helpstring("interface ITopologyQuery")
]
interface ITopologyQuery : IUnknown
{
HRESULT LoadFile(BSTR completeFileName);
HRESULT SetParameters(int above, int percentage);
}
[
uuid(a958f2af-8b55-43c4-9fc3-c39d83fc1376)
]
library TopologyQueryLib
{
[
uuid(cc814992-31ec-4a1f-a41e-111ade27bdfe),
helpstring("TopologyQuery class")
]
coclass CTopologyQuery
{
[default] interface ITopologyQuery;
};
}
现在我的问题是,你在哪里定义CTopologyQuery类?如果我把它定义为另一个文件中的普通C++类,编译器会正确地将我的类与接口链接起来吗?这个C++类的代码如下(它是在.cpp文件中实现的):
class CTopologyQuery : public ITopologyQuery
{
public:
__stdcall CTopologyQuery();
//IUnknown interface
HRESULT __stdcall QueryInterface(REFIID riid, void **ppObj);
ULONG __stdcall AddRef();
ULONG __stdcall Release();
//ITopologyQuery interface
HRESULT __stdcall LoadTopologyFile(BSTR completeFileName);
HRESULT __stdcall SetParameters(int above, int percentage);
private:
};
现在,如果我将库部分放入.idl文件中或不放入,它都可以编译。我有点迷失于此,不知道应该怎么做?我知道coclass定义应该为接口提供默认实现,但对我来说看起来像一个没有方法的空类...