BSTR 与wchar_t 的区别

 

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)处理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值