C++ COM 对象作为属性
C++ COM object as property
我需要我的库的一个对象,特别是作为一个属性,因为我在我的方法中需要它。
如果我将其声明为局部变量,它可以工作:
#import "...library.tlb"
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
RobstepRemoteLibraryCSharp::IRobstepPtr interf(__uuidof(RobstepRemoteLibraryCSharp::Robstep));
std::wcout << interf->Test() << std::endl;
interf->ConnectToPlattform("192.168.0.1");
}
这有效并完全符合它应该做的事情。但是如何获取"interf"变量作为属性呢?到目前为止,我尝试了不同的东西。
文件.h
#import "...library.tlb"
class robstep
{
public:
robstep(void);
~robstep(void);
private:
CComPtr interf; //Version 1
CComObject<RobstepRemoteLibraryCSharp::Robstep>* interf; //Version 2
}
文件.cpp
#import "...library.tlb"
robstep::robstep(void)
{
CoInitialize(NULL);
interf.CoCreateInstance(__uuidof(RobstepRemoteLibraryCSharp::Robstep)); //Version 1
CComObject<RobstepRemoteLibraryCSharp::Robstep>::CreateInstance(&interf); //Version 2
}
我使用了这个链接和这个
我必须投掷它或类似的东西吗?
您应该使用第一个选项 CComPtr<Interface>
。
但是,如果要导入 TLB,则通过#import
应该会生成一些可以使用的智能指针模板。
RobstepRemoteLibraryCSharp::IRobstepPtr interf;
interf.CreateInstance(__uuidof(Class));
除了公认的答案之外,这是我对这个问题的解决方案:
CComPtr<RobstepRemoteLibraryCSharp::IRobstep> interf;
interf.CoCreateInstance(__uuidof(RobstepRemoteLibraryCSharp::Robstep));
相关文章:
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- C++ 在堆栈中包含多态属性的类对象存储
- 为什么我的 DeviceInformation 对象没有 System.Devices.InterfaceClassGuid 属性?
- 私有对象的互斥体 - 全局与属性
- 如何从传递到对象的对象内部访问对象的类属性?
- 从 lambda 返回的对象将丢失属性值
- 是否可以在C++中移动临时对象的属性?
- 如何通过对象的类属性删除对象,并返回其对象值?
- 类对象属性是否可以调用返回属性应具有的值的方法C++?
- C++对象作为 Objective-C++ 属性
- 如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组
- 对象属性同级访问
- 如何访问 JSCValue 对象的属性
- 从基于属性的列表中返回随机对象
- 迭代 Napi::对象属性(键)在 Node.js C++ N-API 中
- 返回对象会丢失属性
- 根据 C++ 中的属性对对象类进行分区的优化方法
- 基于对象的两个属性的自定义比较器会引发异常
- 创建新对象并立即为其设置属性时出现编译器错误
- 复合属性对象,当所有子对象都更改时,它禁止多个信号发射