C++ COM object as property

2019-08-14 19:42发布

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?

标签: c++ com
2条回答
再贱就再见
2楼-- · 2019-08-14 20:05

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));
查看更多
干净又极端
3楼-- · 2019-08-14 20:07

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

CComPtr<RobstepRemoteLibraryCSharp::IRobstep> interf;
interf.CoCreateInstance(__uuidof(RobstepRemoteLibraryCSharp::Robstep));
查看更多
登录 后发表回答