Discussion:
String Array to LPWSTR*
(too old to reply)
avleo20
2009-04-23 04:39:58 UTC
Permalink
Hello All,
I am trying to convert a list of Strings in LPWSTR* array. I am using the
following way to convert it. But here the problem occurs in the returning
array items. It contains all the elements same in the array. I found the
problme is due to memset() which updates the memory address everytime same as
earlier. Please help me out----

for(DWORD i = 0; i < itemIds->Count; i++)
{
ATL::CComBSTR itemWChar;
itemWChar = ::GetAtlBstr(itemIds[i]);
WCHAR _itemWChar[_MAX_PATH];
memset(_itemWChar, 0, sizeof(_itemWChar) / sizeof(_itemWChar[0]));
memcpy(_itemWChar, static_cast<BSTR>(itemWChar), itemWChar.ByteLength());
items[i] = _itemWChar;
}
Ben Voigt [C++ MVP]
2009-04-23 15:00:00 UTC
Permalink
Post by avleo20
Hello All,
I am trying to convert a list of Strings in LPWSTR* array. I am using
the following way to convert it. But here the problem occurs in the
returning array items. It contains all the elements same in the
array. I found the problme is due to memset() which updates the
memory address everytime same as earlier. Please help me out----
for(DWORD i = 0; i < itemIds->Count; i++)
{
ATL::CComBSTR itemWChar;
itemWChar = ::GetAtlBstr(itemIds[i]);
WCHAR _itemWChar[_MAX_PATH];
memset(_itemWChar, 0, sizeof(_itemWChar) / sizeof(_itemWChar[0]));
memcpy(_itemWChar, static_cast<BSTR>(itemWChar), itemWChar.ByteLength());
items[i] = _itemWChar;
}
_itemWChar just went out of scope, your pointer to it becomes invalid.

What will be done with the LPWSTR[] ? Does the recipient free or modify any
of the strings? If not, then just store the BSTRs themselves into the
array, a BSTR variable is a pointer to the wide character content of an OLE
string, so it's fully compatible with LPCWSTR.

Loading...