开发者

Unable to read Russian string with _stprintf

开发者 https://www.devze.com 2023-03-23 12:08 出处:网络
I have following code to assign the retrieved firewire board name to a TCHAR variable. TCHAR* firewireName = new TCHAR开发者_开发问答[wcslen(variantProperty.bstrVal)+1];

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消