ATL - BSTRs and Properties
-
//////////////////////////////////////////////////////////// // All, // I would appreciate any feedback on this. // Is this the best way to do this? // Thanks, // Rich //////////////////////////////////////////////////////////// BSTR bstrProperty; STDMETHODIMP MyObject::get_Property(BSTR *pVal) { // According to KB article, I need // return a copy. *pVal = SysAllocString(bstrProperty); return S_OK; } STDMETHODIMP MyObject::put_Property(BSTR newVal) { // According to KB article, I need // my own copy to use and later free. bstrProperty = SysAllocString(newVal); return S_OK; }
-
//////////////////////////////////////////////////////////// // All, // I would appreciate any feedback on this. // Is this the best way to do this? // Thanks, // Rich //////////////////////////////////////////////////////////// BSTR bstrProperty; STDMETHODIMP MyObject::get_Property(BSTR *pVal) { // According to KB article, I need // return a copy. *pVal = SysAllocString(bstrProperty); return S_OK; } STDMETHODIMP MyObject::put_Property(BSTR newVal) { // According to KB article, I need // my own copy to use and later free. bstrProperty = SysAllocString(newVal); return S_OK; }
You also need to free the BSTRs before storing new values in them. So in get_Property:
SysFreeString ( *pVal );
And in put_Property:SysFreeString ( bstrProperty );
I believe it's ok to pass NULL to SysFreeString(), but it wouldn't hurt to put in some standard pointer error-checking as well. --Mike-- http://home.inreach.com/mdunn/ This posting is provided "as was" with no warranties, guarantees, lotteries, or any of those little bags of peanuts you get on planes. You assume all risk for crossing the street without holding mommy's hand. © 2001 Mike's Classy Software. Member FDIC. If rash develops, discontinue use. :love: your :bob: with :vegemite: and :beer: -
You also need to free the BSTRs before storing new values in them. So in get_Property:
SysFreeString ( *pVal );
And in put_Property:SysFreeString ( bstrProperty );
I believe it's ok to pass NULL to SysFreeString(), but it wouldn't hurt to put in some standard pointer error-checking as well. --Mike-- http://home.inreach.com/mdunn/ This posting is provided "as was" with no warranties, guarantees, lotteries, or any of those little bags of peanuts you get on planes. You assume all risk for crossing the street without holding mommy's hand. © 2001 Mike's Classy Software. Member FDIC. If rash develops, discontinue use. :love: your :bob: with :vegemite: and :beer: