Windows Component Implementation
We now have stub code for the TagParse
component.
In
this section, we show how to
fill in the method stubs and compile this code into a Windows DLL.
Add the output files listed earlier in Table 9-9
to the ParseServer project.
Tip
It is a good idea to verify that the stub component successfully compiles before proceeding.
Smart Pointer
We use the IPtr
smart pointer interface
template in the implementation of the
CTagParse
class and ITagParse
interface.[64] A
smart pointer automatically releases the interface to which it points
when it goes out of scope. This alleviates some of the messy code
that can accompany COM programming when an error occurs in the middle
of a method, and existing interfaces need to be released before
returning.
For example, in the code below, if bSuccess
is set
to false
, we need to make sure and call
Release
on pMyString
before
returning. Otherwise, we will have a memory leak.
HRESULT MyMethod( ) { IString* pMyString; pCoCreateString(L"MyString", &pMyString); bool bSuccess = true; //...Processing ... if(bSuccess == false) { pMyString->Release( ); return E_FAIL; } return S_OK; }
Contrast this approach with using a smart pointer.
HRESULT MyMethod( ) { IPtr<IString, &IID_IString> spMyString; pCoCreateString(L"MyString", &spMyString); bool bSuccess = true; //...Processing ... if(bSuccess == false) return E_FAIL; return S_OK; }
Here, we can simply return E_FAIL
. The smart
pointer automatically calls Release
on its interface when it ...
Get Programming Visual Basic for the Palm OS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.