Answered by:
ATL COM: Convert from IMyInterface * to ATL::CComObject<T>

Question
-
I'm developing an ATL COM DLL, and I'm using a c++ library, for use it I have to instance a class of the type SmartPtr<TNLP>. I can do it with this code because CProblem implements TNLP:
CComObject<CProblem>* problem; HRESULT hRes = CComObject<CProblem>::CreateInstance(&problem); SmartPtr<TNLP> myadolc_nlp = problem;
But the problem is that code creates a new instance, and I wanted to use a previously intenced member ptrProblem of type "IProblem" (I tried also with IUnkonwn). But I don't find the way to convert that ptrProblem instance to SMartPtr.
I tried this:
CComObject<CProblem>* problem(ptrProblem); SmartPtr<TNLP> myadolc_nlp = problem;
But I get this error compiling:
" cannot convert from 'IProblem *' to 'ATL::CComObject<T> *' "
How should I do it?
Thanks in advance
- Edited by Alainee Wednesday, June 29, 2016 1:35 PM
Wednesday, June 29, 2016 1:35 PM
Answers
-
On 6/29/2016 9:35 AM, Alainee wrote:
I'm developing an ATL COM DLL, and I'm using a c++ library, for use it I have to instance a class of the type SmartPtr<TNLP>. I can do it with this code because CProblem implements TNLP:
CComObject<CProblem>* problem; HRESULT hRes = CComObject<CProblem>::CreateInstance(&problem); SmartPtr<TNLP> myadolc_nlp = problem;
But the problem is that code creates a new instance, and I wanted to use a previously intenced member ptrProblem of type "IProblem" (I tried also with IUnkonwn). But I don't find the way to convert that ptrProblem instance to SMartPtr.
Recall that an arbitrary IProblem* pointer doesn't necessarily point to an instance of CProblem, or any other class that "implements TNLP" (whatever that might mean). It could be an independent implementation provided by the client.
If somehow you are sure that the pointed-to object is in fact an instance of CProblem, then just cast: CProblem* p = static_cast<CProblem*>(ptrProblem); However, if you can be sure of that, then it's not clear why you would store an IProblem* pointer and not a CProblem* in the first place.
Wednesday, June 29, 2016 2:03 PM
All replies
-
What is a SmartPtr? What is type TNLP?
Since these are not ATL classes or types we need more information.
Wednesday, June 29, 2016 1:47 PM -
On 6/29/2016 9:35 AM, Alainee wrote:
I'm developing an ATL COM DLL, and I'm using a c++ library, for use it I have to instance a class of the type SmartPtr<TNLP>. I can do it with this code because CProblem implements TNLP:
CComObject<CProblem>* problem; HRESULT hRes = CComObject<CProblem>::CreateInstance(&problem); SmartPtr<TNLP> myadolc_nlp = problem;
But the problem is that code creates a new instance, and I wanted to use a previously intenced member ptrProblem of type "IProblem" (I tried also with IUnkonwn). But I don't find the way to convert that ptrProblem instance to SMartPtr.
Recall that an arbitrary IProblem* pointer doesn't necessarily point to an instance of CProblem, or any other class that "implements TNLP" (whatever that might mean). It could be an independent implementation provided by the client.
If somehow you are sure that the pointed-to object is in fact an instance of CProblem, then just cast: CProblem* p = static_cast<CProblem*>(ptrProblem); However, if you can be sure of that, then it's not clear why you would store an IProblem* pointer and not a CProblem* in the first place.
Wednesday, June 29, 2016 2:03 PM -
What is a SmartPtr? What is type TNLP?
Since these are not ATL classes or types we need more information.
TNLP is a class of IpOpt optimization library https://projects.coin-or.org/Ipopt
CProblem/IProblem is my ATL class.
ThanksFriday, July 1, 2016 7:38 AM -
On 6/29/2016 9:35 AM, Alainee wrote:
I'm developing an ATL COM DLL, and I'm using a c++ library, for use it I have to instance a class of the type SmartPtr<TNLP>. I can do it with this code because CProblem implements TNLP:
CComObject<CProblem>* problem; HRESULT hRes = CComObject<CProblem>::CreateInstance(&problem); SmartPtr<TNLP> myadolc_nlp = problem;
But the problem is that code creates a new instance, and I wanted to use a previously intenced member ptrProblem of type "IProblem" (I tried also with IUnkonwn). But I don't find the way to convert that ptrProblem instance to SMartPtr.
Recall that an arbitrary IProblem* pointer doesn't necessarily point to an instance of CProblem, or any other class that "implements TNLP" (whatever that might mean). It could be an independent implementation provided by the client.
If somehow you are sure that the pointed-to object is in fact an instance of CProblem, then just cast: CProblem* p = static_cast<CProblem*>(ptrProblem); However, if you can be sure of that, then it's not clear why you would store an IProblem* pointer and not a CProblem* in the first place.
Thanks a lot! It runs!
I defined it as IProblem * because it is a public member of another atl class. Perhaps there is another better way to do it but it is what I learned here
https://social.msdn.microsoft.com/Forums/vstudio/en-US/dfb6f3cd-30cf-4234-91ed-e5946dfcff06/set-public-an-agregated-atl-com-class?forum=vclanguage#a2e78374-5ea0-4c4d-b9fd-1927bd470db8
Friday, July 1, 2016 7:41 AM -
On 7/1/2016 3:41 AM, Alainee wrote:
I defined it as IProblem * because it is a public member of another atl class. Perhaps there is another better way to do it but it is what I learned here
https://social.msdn.microsoft.com/Forums/vstudio/en-US/dfb6f3cd-30cf-4234-91ed-e5946dfcff06/set-public-an-agregated-atl-com-class?forum=vclanguage#a2e78374-5ea0-4c4d-b9fd-1927bd470db8In that thread, you have an interface that accepts arbitrary interface pointers from the client. Once again, this means that you cannot, in general, assume that IProblem* actually points to a CProblem* (your implementation) - it could very well be independently implemented by the client.
If your server cannot do its work entirely in terms of IProblem interface but requires facilities provided by CProblem, then it appears unwise to provide a put_Problem(IProblem*) method in the first place.
Friday, July 1, 2016 5:11 PM -
And how should I define it to provide a put_Problem method? I tried with IUnknown but then the type is not visible from the client program in C#.
Thanks!
Monday, July 11, 2016 7:24 AM -
On 7/11/2016 3:24 AM, Alainee wrote:
And how should I define it to provide a put_Problem method?
You should not. If the server can only work with objects manufactured by the server itself, you don't want a put_* method - have the server keep track of the objects it created, don't place that burden onto the client. Design your interfaces and workflows accordingly.
Monday, July 11, 2016 2:57 PM