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
}
Do I have to cast it or something like this?
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.Besides the accepted answer, here's mine solution for this problem: