简体   繁体   中英

Regarding SafeArrayPutElement

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" ie

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

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM