Re: Need example to remove Object Reference using CComPtr class
"Landon" <Landon@discussions.microsoft.com> wrote in message
news:7EF92538-7279-4FA8-8A22-ABA485B3BBAD@microsoft.com...
Yesterday, I've got a suggestion to use the CComPtr class to remove the
object reference but it still became a quite big question for me because I
don't understand how to use it.
I have gone to MSDN, CodeProject, CodeGuru and somewhere else but I still
having difficulties to understand and implement it.
Does anyone ever use the CComPtr class and have it worked successfully?
When you receive/acquire a COM pointer store it in a CComPtr variable.
// .h file inside your class declaration:
CComPtr <_Application> m_spApp; // Word::Application object
// .cpp file
STDMETHODIMP CMyWordAddin::OnConnection(IDispatch * Application,
ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
{ // save Application pointer
m_spApp = CComQIPtr <_Application> (Application);
If you receive an Application* you can assign it directly to m_spApp. But
more often you receive an IDispatch*, which you convert to an Application*
using CComQIPtr (which calls QueryInterface for you).
Then use m_spApp as a pointer to call Application methods. That's really
all you do - the Release of the COM pointer is automatic when it goes out of
scope. I.e., its destructor calls Release().
--
Scott McPhillips [VC++ MVP]