我有一个接口的 ATL/COM 实现,我需要在主代码中实例化它并传递给另一个 COM 对象。这个实现是用 C++ 实现的。
从我的主要 C++ 代码中,我想直接访问对象(不通过 COM 端口),并直接访问类成员和方法。
我认为正确的方法是在 ATL/COM 类中添加
DECLARE_NO_REGISTRY()
;然后调用 myCOMClass:CreateObject()
而不是 CoCreateInstance
;但我不知道如何使用这个(也没有找到任何例子)。
我尝试了几种组合,但没有成功。
在我的主要代码中:
//I added this line to call the lib directly
#include "comClass\StdAfx.h"
#include "comClass\comClass_i.c"
#include "comClass\comClass.h"
//I removed this line to bypass COM
//#import "comClass\comClass.dll"
//What can I put here to replace this block, bypass COM
//and being able to access class members??
CoInitialize(NULL);
comClassInterface *myObject = NULL;
HRESULT hr = ::CoCreateInstance(__uuidof(comClass),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(comClassInterface),
(void**)&myObject);
直接创建 COM 对象或多或少是“标准”的:
CComObject<comClass> *myObject;
CComObject<comClass>::CreateInstance(&myObject);
基本上就是这样..如果类不是抽象的,你也可以只执行“new comClass”,这通常是使用 ATL 时的情况。