【发布时间】:2011-06-28 15:33:11
【问题描述】:
我是 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 定义应该为接口提供默认实现,但对我来说它看起来像一个没有方法的空类......
【问题讨论】:
-
你读过Exposing COM Components to the .NET Framework (MSDN)。当我试图理解 .Net/COM 互操作时,这很有帮助。
-
您在使用 ATL 吗? MFC?还是您全部手动完成?
-
@bdonlan:我正在使用 MFC,我想使用 ATL,但该向导对我不起作用(由于一个模糊的原因,当它启动时它会清除我的资源附加包含目录,然后给出我是一个 RC1015 错误)。所以我尝试手动完成(同时我尝试了解 COM 的工作原理)
-
好的。这是EXE还是DLL?
-
另外,对于 DLL,这是进程内服务器还是进程外服务器?