C++ COM ATL DLL
C++ COM ATL DLL
我是visual studio 2012 pro,使用v110_xp工具集。我想在COM类中"转换"我的c++动态库。库的结构是这样的:
struct A;
struct B;
class IClass {
public:
virtual ~IClass() = 0;
virtual A doA() = 0;
virtual B doB() = 0;
virtual void getA( A& a ) = 0;
virtual void getB( B& b) = 0;
};
inline IClass::~IClass() {}
typedef std::unique_ptr< IClass > IClassPtr;
API_LIB IClassPtr ClassCreate( /* param */ );
现在,所有的方法和函数都可以抛出一个从std::exception派生的类(析构函数除外)。
我想让它成为一个COM类,这样我就可以从C#中使用它。实现这一点最快捷的方法是什么?ATL有帮助吗?有人知道一些教程或书籍吗。我在COM中没有经验。
您至少应该从IUnknown派生类。如果您要在某些脚本中使用COM,那么您可以从IDispatch派生类。COM的一本好书是Jonathan Bates的《用ATL创建轻量级组件》。
但是,一些真正初级的实现可能是这样的:
class MyCOM : public IUnknown
{
public:
static MyCOM * CreateInstance()
{
MyCOM * p( new(std::nothrow) MyCOM() );
p->AddRef();
return p;
}
ULONG __stdcall AddRef()
{
return ++nRefCount_;
}
ULONG __stdcall Release()
{
assert( nRefCount_ > 0 );
if( --nRefCount_ == 0 )
{
delete this;
return 0;
}
return nRefCount_;
}
HRESULT __stdcall QueryInterface( const IID & riid, void** ppvObject )
{
if( riid == IID_IUnknown )
{
AddRef();
*ppvObject = this;
return S_OK;
}
// TO DO: add code for interfaces that you support...
return E_NOINTERFACE;
}
private:
MyCOM()
: nRefCount_( 0 ){}
MyCOM(const MyCOM & ); // don't implement
MyCOM & operator=(const MyCOM & ); // don't implement
~MyCOM(){}
ULONG nRefCount_;
};
相关文章:
- 挂起和取消挂起一个文件DLL
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 导入库可以跨dll版本工作吗
- WinAPI - GetLastError 在通过 COM ATL DLL 调用时始终返回 0
- MFC/DLL中缺少ATL :: CSTRINGT功能,该功能与Clang/vs-2019构建
- 如何在ATL项目中使用ActiveX DLL
- ATL结构从dll上断开COM对象
- 如何安全地删除ATL DLL中的std ::线程
- ATL ActiveX DLL 作为映射类型图像和数据加载了两次
- 删除/编辑 ATL COM DLL 属性/方法
- 在 Visual Studio 2013 中找不到C++ ATL 库(atl.lib 和 atl120.dll)
- 使用 ATL 编译 dll,并将方法参数作为接口,但将它们作为 coclass 获取
- 非 MFC ATL 项目 (dll) 生成与 MFC 相关的错误
- Calling ATL COM dll from MFC
- 如何在ATL DLL中获取HWND(用于SendMessage或PostMessage)
- PostMessage(),SendMessage 在 ATL dll 中不起作用(事件处理)
- ATL COM dll中调用了错误的方法
- ATL COM dll中的缓冲区溢出
- C++ COM ATL DLL
- 如何在Visual Studio c++ ATL项目创建的COM .dll中添加新函数