Skip to content

Instantly share code, notes, and snippets.

@kevinkirkup
Created April 1, 2011 16:18
Show Gist options
  • Save kevinkirkup/898418 to your computer and use it in GitHub Desktop.
Save kevinkirkup/898418 to your computer and use it in GitHub Desktop.
COM Method implementation to return an array of strings
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