Created
April 1, 2011 16:18
-
-
Save kevinkirkup/898418 to your computer and use it in GitHub Desktop.
COM Method implementation to return an array of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
STDMETHODIMP CMyClass::GetThings(VARIANT* returnThings) | |
{ | |
HRESULT hr = S_OK; | |
SAFEARRAY FAR* safeArray; | |
SAFEARRAYBOUND safeArrayBound; | |
if (m_thing != NULL) | |
{ | |
std::set<const char *>::iterator it; | |
std::set<const char *> things = m_thing->GetThings(); | |
// Initialize the bounds of the SafeArray | |
safeArrayBound.lLbound = 0; | |
safeArrayBound.cElements = things.size(); | |
safeArray = SafeArrayCreate( | |
VT_BSTR, // Array of BSTR | |
1, // 1 dimension | |
&safeArrayBound); // Initialize bounds | |
LONG i = 0; | |
// Add the names to the array | |
for (i = 0, it = things.begin(); it != things.end(); it++, i++) | |
{ | |
size_t length = strlen(*it) + 1; | |
OLECHAR *oleThing = new OLECHAR[length]; | |
mbstowcs(oleThing, *it, length); | |
hr = SafeArrayPutElement(safeArray, &i, SysAllocString(oleThing)); | |
delete oleThing; | |
// Quit any of the operations fail | |
if (FAILED(hr)) | |
break; | |
} | |
if (SUCCEEDED(hr)) | |
{ | |
returnThings->vt = VT_ARRAY | VT_BSTR; | |
returnThings->parray = safeArray; | |
} | |
} | |
return hr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment