Unable to read Russian string with _stprintf
I have following code to assign the retrieved firewire board name to a
TCHAR
variable.
TCHAR* firewireName = new TCHAR开发者_开发问答[wcslen(variantProperty.bstrVal)+1];
_stprintf(firewireName, _T("%S"), variantProperty.bstrVal);
VariantClear(&variantProperty);
but when firewire name is in russian LSI 1394 OHCI совместимый хост-контроллер
I the firewireName
variable contains only LSI 1394 OHCI
I'm a n00b in C++, is there anything wrong in the code?
Yes. Lots. In short.
Firstly, BSTRs are not what is expected by the printf
family of functions. BSTRs are prefixed by their length, and printf
will expect NULL-terminator. This also marks your use of wcslen
as wrong.
Secondly, you failed to use any sort of proper memory management.
You need to use SysStringLen to get the length, and then store the result in a C++ buffer that is guaranteed to free itself. ATL provides a CComBSTR class which should do the trick.
精彩评论