How to work with string properties in SDK products

Question

How to work with string properties in SDK products?

Answer

Working with string properties is similar to working with simple properties, but there are important differences. A C++ developer working with string properties must free the strings that are passed to set methods, and also those that are returned by get-methods. This is done automatically in Visual Basic and in C++ with Native COM support.

In this example, MyObject also supports a string property called Name. This property is described in the type library as follows:
interface IMyObject : IUnknown {
...
[propget]
HRESULT Name([out, retval]BSTR* result);

[propput]
HRESULT Name([in]BSTR value);
...
};

Below is a C++ code sample property that uses this property.

IMyObject* pMyObject;
...
// "get" method
BSTR res;
pMyObject->get_Name(&res);
...
// Now it is possible to free the string
::SysFreeString(res);

// "put" method
BSTR str = ::SysAllocString(L"New Name");
pMyObject->put_Name(str);
// Now free the string that we allocated
::SysFreeString(str);

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.

Recently viewed