我创建了一个名为 Test.dll 的 COM 组件,其中我创建了一个 IDiscover 接口(interface)。
现在我想到了两种分配内存的方法
IDiscoverPtr id(__uuid(TestClass)); and Test::IDiscover *id=NULL;
这两者有什么区别....欢迎提供任何信息..
最佳答案
第一个是智能指针,第二个是普通指针。
您无需担心 Releasing
智能指针,而您必须显式 Release()
一个普通的 COM 接口(interface)指针。
有关智能指针的更多详细信息,Look Here
除此之外,第一个将尝试从注册表中为您名为 TestClass
的类找到一个 GUID,并将尝试通过 CoCreateInstance
API 创建此类的实例称呼。它将尝试通过所述 CoCreateInstance
调用获取 IDiscover
接口(interface)指针,如果失败,将抛出错误。成功执行 IDiscoverPtr id(__uuid(TestClass));
行后,您应该在 id
中有一个有效的 IDiscover
接口(interface)指针。
第二个只是接口(interface)指针的声明,没有别的。您必须自己通过(大多数时候)CoCreateInstance
或其变体之一来实例化它。
关于c++ - 通过以下两种方式分配指针对象有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1163250/