C ++ COM对象作为属性 [英] C++ COM object as property

查看:264
本文介绍了C ++ COM对象作为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个我的库的对象,特别是作为一个属性,因为我需要它在我的方法。

I need a object of my library and especially as a property because I need it in my methods.

如果我声明它是一个局部变量,它的工作原理:

It works if I declare it as a local variable:

#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变量作为属性?我尝试了不同的东西。

This works and does exactly what it should do. But how do get the "interf" variable as a property? I tried different things so far.

file.h

#import "...\library.tlb"

class robstep
{
public:
    robstep(void);
    ~robstep(void);

private:
    CComPtr interf; //Version 1
    CComObject<RobstepRemoteLibraryCSharp::Robstep>* interf; //Version 2
}

file.cpp

#import "...\library.tlb"

robstep::robstep(void)
{
    CoInitialize(NULL);

    interf.CoCreateInstance(__uuidof(RobstepRemoteLibraryCSharp::Robstep)); //Version 1
    CComObject<RobstepRemoteLibraryCSharp::Robstep>::CreateInstance(&interf); //Version 2

}

我使用此链接和此

I used this link and this one

我必须强制转换它吗?

推荐答案

您应该使用第一个选项 CComPtr< Interface>

You should use the first option CComPtr<Interface>.

如果你正在导入TLB,通过 #import 你应该为你生成一些智能指针模板,你可以使用。

If you're importing the TLB though, via #import you should have some smart pointer templates generated for you that you can use.

RobstepRemoteLibraryCSharp::IRobstepPtr interf;
interf.CreateInstance(__uuidof(Class));

这篇关于C ++ COM对象作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆