0

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

borrible
  • 17,120
  • 7
  • 53
  • 75
Naveen
  • 23
  • 1
  • 9
  • Post real code, passing safearray* and LONG to the function can never compile. Strings in a SAFEARRAY must be BSTR, not wchar_t. – Hans Passant Jan 10 '12 at 14:03

1 Answers1

2

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); 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for the reply, Here what is the alternate for 'L' in L'a', coz i already have a string in a varaiable so how to convert it – Naveen Jan 12 '12 at 06:41
  • What kind of variable are you using? I showed you how to convert `wchar_t` and `wchar_t*`. Are you using something else? – Remy Lebeau Jan 12 '12 at 17:09