Article ID: 104138
Article Last Modified on 12/3/2003
LPOLEOBJECT lpObject;
.
. // Get pointer to IOleObject.
.
LPVIEWOBJECT lpView = lpObject;
lpObject->AddRef()
// GetViewObject is a theoretical function that takes a
// pointer to anything derived from IUnknown, and then
// returns a pointer to IViewObject in the same variable
// passed as the parameter. The AddRef() above is needed so
// that the original pointer to IOleObject is not freed.
GetViewObject(lpView);
void function()
{
// Get a pointer to IOleObject from a global variable.
LPOLEOBJECT lpOleObject = glpObject;
// This AddRef() is needed so that the interface
// pointed to by the global variable, glpObject,
// does not get released by a different part of
// the applications code.
lpOleObject->AddRef();
.
. // use lpOleObject;
.
lpOleObject->Release();
}
STDMETHDOIMP IUnknown::QueryInteface( REFIID iidInterface,
LPVOID FAR *ppvObj)
{
*ppvObj = NULL;
SCODE sc = E_NOINTERFACE;
if (iidInterface == IUnknown)
{
*ppvObj = this;
// This AddRef() is needed because a new pointer
// was just created.
AddRef();
sc = S_OK;
}
return ResultFromScode(sc);
}
// m_lpOleObject is a private member variable of a C++ class.
// GetOleObject is a member function to return access to this
// pointer.
void GetOleObject (LPVOID FAR *ppObject)
{
*ppObject = m_lpOleObject;
// This AddRef() is needed due to this rule.
m_lpOleObject->AddRef();
}
void function (LPOLEOBJECT lpOleObject)
{
// Can use lpOleObject in this function
// without doing AddRef() and Release().
}
LPVIEWOBJECT lpView;
HERROR hErr = lpOleObject->QueryInterface(IID_IViewObject,
(LPVOID FAR *)lpView);
if (hErr = NOERROR)
{
// The QueryInterface succeeded. lpView does not have
// to be AddRef()'d because it has already been done
// by the QueryInterface method.
}
void function()
{
LPOLEOBJECT lpTempObject;
.
.
.
lpTempObject = lpObject;
.
. // lpTempObject can be used
. // without reference counting as long as
. // it is known that the lifetime of lpObject
. // outside of this function call.
.
}
Additional query words: 2.00 3.50 4.00
Keywords: kbprogramming KB104138