typedef /* [wire_marshal] */ OLECHAR __RPC_FAR *BSTR; in wtypes.h
typedef WCHAR OLECHAR; in wtypes.h
typedef wchar_t WCHAR; in winnt.h
typedef unsigned short wchar_t; in ctype.h
wchar_t wstrsource[16]=L"hello world";
BSTR bstrhello=::SysAllocString(wstrsource);
cout<<::SysStringLen(bstrhello)<<"/n";
cout<<::SysStringLen(wstrsource)<<"/n";
RESULT
11
2720172
wchar_t wstrsource[16]=L"hello/0world";
BSTR bstrhello=::SysAllocString(wstrsource);
cout<<::SysStringLen(bstrhello)<<"/n";
cout<<::SysStringLen(wstrsource)<<"/n";
RESULT
5
2720172
Q1:为什么会出现这样的结果?SysAllocString怎么判断字符串结束?'/0'会在"hello world"后自动添加?
A1: 根据'/0'判断字符串结束
Q2:BSTR与wchar_t *的区别?
A2: wchar_t* 只是一个简单的指针 跟int* ,char* 一样
BSTR 所指向的内存地址相当于一个wchar_t*,但BSTR往后退4Bytes(在内存中)的
地址中所存的一个DWORD值表示它的长度。
例如:
BSTR bstrT = ::SysAllocString(L"....");//(内存地址)0x00001000;
LPDWORD lpdwLen = (LPBYTE)bstrT - 4; //(相当于0x0000FFC)
ASSERT(*lpdwLen == ::SysStringLen(bstrT));
注意:SysStringLen的参数类型是BSTR而非wchar_t*
C++里,*lpdwLen/2==::SysStringLen(bstrT);
Q3:wchar_t wstrsource[16]=L"hello/0world";中L的作用?
A3: L""告诉编译器把双引号中的字符串按双字节(wchat_t)处理