C++ COM object as property

妖精的绣舞 提交于 2019-12-11 08:30:28

问题


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");
}

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

Do I have to cast it or something like this?


回答1:


You should use the first option CComPtr<Interface>.

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));



回答2:


Besides the accepted answer, here's mine solution for this problem:

CComPtr<RobstepRemoteLibraryCSharp::IRobstep> interf;
interf.CoCreateInstance(__uuidof(RobstepRemoteLibraryCSharp::Robstep));


来源:https://stackoverflow.com/questions/29969302/c-com-object-as-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!