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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access