SQL-DMO uses the OLE BSTR object to return strings to the client application. By definition, an OLE BSTR object is composed of Unicode characters.
Further, when an OLE BSTR object is returned, the reference count on the string-implementing resource is implicitly incremented. String references are released using the COM SysFreeString function. For example:
LPSQLDMODATABASE pDatabase; BSTR bstrDBName = NULL; HRESULT hr; // Getting a string back from SQL-DMO is also getting a // reference on an object. Be sure to release it. hr = pDatabase->GetName(&bstrDBName); if (SUCCEEDED(hr)) SysFreeString(bstrDBName);
When setting a SQL-DMO property, or providing a string as a method argument, be sure to use Unicode character strings. A number of macros exist to aid in coding constant values. For example:
LPSQLDMOCOLUMN pColumn; WCHAR* szColumnName = L"EmployeeID"; // Use L macro to force // Unicode character // string. Could use // OLESTR() macro as // well. HRESULT hr; hr = CoCreateInstance(CLSID_SQLDMOColumn, NULL, CLSCTX_INPROC_SERVER, IID_ISQLDMOColumn, (void**) &pColumn); if (SUCCEEDED(hr)) pColumn->SetName(szColumnName);
When developing an application for operating systems that do not provide native Unicode support, such as Microsoft® Windows® 95, you need to convert strings as required to ensure that the correct character set is used. The Windows API functions MultiByteToWideChar and WideCharToMultiByte provide conversion between ANSI or other multibyte character sets and Unicode. If using MFC, objects of the CString class can be used to convert strings easily from ANSI to Unicode and vice versa.