Regarding SafeArrayPutElement

2.3k views Asked by At

FYI I am begginer in COM\ATL and unicode

I am using SafeArrayPutElement(safearray*,LONG,void*) in my code and the problem is...

here, the function works fine when i give the third parameter as L"ItWorks" i.e

SafeArrayPutElement(safearray*,LONG, L"ItWorks");

but if i use

wchar_t str;
str = 'a';
SafeArrayPutElement(safearray*,LONG,&str);

this function fails saying E_OUTOFMEMORY

here my need is, i have a string in char* variable, some how i need to use this as the THIRD parameter for the above function. Can anyone please help me in this regard.

TIA

Naveen

1

There are 1 answers

2
Remy Lebeau On

The only string type that is safe to use in COM in a BSTR, not a raw wchar_t*. This is because a BSTR contains extra internal data that COM uses for marshalling purposes. Use SysAllocString() or SysAllocStringLen() to allocate a new BSTR from a wchar_t*, and then use SysFreeString() to free it when you are finished using it, eg:

BSTR bstr = SysAllocString(L"ItWorks");
SafeArrayPutElement(..., bstr);
SysFreeString(bstr);

.

wchar_t str = L'a'; 
BSTR bstr = SysAllocStringLen(&str, 1);
SafeArrayPutElement(..., bstr);
SysFreeString(bstr);