BSTR 与wchar_t 的区别

本文探讨了BSTR与wchar_t的区别,解释了BSTR如何存储字符串长度,并通过实例展示了SysAllocString函数的工作原理。此外,还介绍了wchar_t类型的字符串如何被编译器处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

# ChildEBP RetAddr Args to Child 00 (Inline) -------- -------- -------- -------- dll_repair!ATL::ChTraitsCRT<wchar_t>::StringCompare+0x5 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\atlmfc\include\cstringt.h @ 564] 01 (Inline) -------- -------- -------- -------- dll_repair!ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::Compare+0x9 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\atlmfc\include\cstringt.h @ 1446] 02 (Inline) -------- -------- -------- -------- dll_repair!ATL::operator<+0xe [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\atlmfc\include\cstringt.h @ 2555] 03 1913f71c 61c5a6a6 0a082580 0b1b0718 6aa97f07 dll_repair!std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >::operator()+0x11 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xstddef @ 141] 04 1913f764 61c5ca28 1913f794 21724f08 0b1b0718 dll_repair!std::_Tree<std::_Tmap_traits<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > >,0> >::_Insert_hint<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > &,std::_Tree_node<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > >,void *> *>+0xf6 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree @ 1686] 05 (Inline) -------- -------- -------- -------- dll_repair!std::_Tree<std::_Tmap_traits<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > >,0> >::emplace_hint+0x28 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xtree @ 1163] 06 (Inline) -------- -------- -------- -------- dll_repair!std::map<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > > >::_Try_emplace+0x63 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\map @ 232] 07 (Inline) -------- -------- -------- -------- dll_repair!std::map<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > > >::try_emplace+0x63 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\map @ 248] 08 1913f78c 61c5d871 1913f8b8 1913f8a8 6aa970af dll_repair!std::map<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > >,std::less<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > >,std::allocator<std::pair<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > const ,std::vector<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > >,std::allocator<ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > > > > > >::operator[]+0x68 [c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\map @ 363]
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值