找回密码
 注册
搜索
查看: 3940|回复: 0

BSTR 字符串

[复制链接]
发表于 2011-5-2 20:06:22 | 显示全部楼层 |阅读模式
原文:http://www.vckbase.com/document/viewdoc/?id=19111 A+ u5 x# u- n5 J# }/ e/ n3 b

6 \) x, @- ^( I$ `2 ~BSTR是“Basic String”的简称,微软在COM/OLE中定义的标准字符串数据类型。
0 n; o' j4 g# \& V5 Q对于VC++,Windows头文件wtypes.h和WinNT.h中定义如下:
  G5 t, T" w3 C* ?5 Y. R
  1. typedef /* [wire_marshal] */ OLECHAR *BSTR;
    . y, [/ P2 A& e& x# z" |
  2. typedef WCHAR OLECHAR;
    ( L# I. S8 k1 m& B
  3. typedef wchar_t WCHAR;
复制代码
由于COM是跨系统及不同开发语言间实现互操作的技术,常规以Null结尾的简单字符串在COM 组件间传递不太方便。因此,便出现了BSTR字符串类型,BSTR是指针类型,标准BSTR是一个有长度前缀和null结束符的OLECHAR数组。BSTR的前4字节是一个表示字符串长度的前缀。BSTR长度域的值是字符串的字节数,但不包括字符串结束符。 BSTR实际上包含的是Unicode串,所以字符数是字节数的一半。这种方式的优点是允许程序员在BSTR串中间嵌入NULL字符,字符串的长度由BSTR的前四个字节表示,(长度中可能包含NULL),bstr字符串是不能象基本数据类型一样直接进行定义的。为处理BSTR字符串,微软在OleAuto.h中定义了BSTR API,分别是:
  1. BSTR SysAllocString(const OLECHAR * psz);
    + S& e' H1 J7 `3 l4 }
  2. INT  SysReAllocString(BSTR* pbstr,const OLECHAR* psz);+ S+ I4 o+ m+ ~9 c
  3. BSTR SysAllocStringLen(const OLECHAR * strIn, UINT ui);) T; m" c: p* h$ s9 g- O9 k9 V0 }
  4. INT  SysReAllocStringLen(BSTR* pbstr,const OLECHAR* psz,unsigned int len);) a+ }% P* E0 z: x: @3 `  u
  5. void SysFreeString(BSTR bstrString);4 g4 H, g# K# G5 N; R
  6. UINT SysStringLen(BSTR);
    $ ~- i$ d7 H9 Y) W, \3 j) X, |3 E
  7. UINT SysStringByteLen(BSTR bstr);% p6 z# e1 b( I8 v# p5 N
  8. BSTR SysAllocStringByteLen(LPCSTR psz,UINT len);
复制代码
下面对上述API用法进行说明。" l* V" i* I) `: I) @6 U' S' d

) l  |, C1 d3 |9 l2 J9 x' w1、SysAllocString函数用于创建一个BSTR字符串,如:BSTR bstr=SysAllocString(L"create bstr string");就创建了一个BSTR字符串,上面说过BSTR字符串最初4个字节表示了字符串中字符个数,接下来是字符串中包含的真实字符,下面我们验证一下是否是这样的。我们生成一个WIN32控制台工程,生成时选择支持ATL的头文件,也可以后添加相应的头文件,主要是atlbase.h和atlstr.h,输入如下语句:
) `/ b! V6 r5 o5 b' K1 S0 D
+ d, l" _; E5 D) f! Wprintf("bstr front 4 byte is %x",*((int*)(((int*)bstr-1))));
; i$ e* Y# K/ ~2 M# j" ^
$ C+ v2 d- H# E6 K( t& f; r语句中的bstr是以上刚刚生成的BSTR组符串,此语句显示的结果是“bstr front 4 byte is 24”换算成10进制正好是34,是“create bstr string”长度的2倍因为BSTR字符串中的字符是用Unicode表示的,而语句' C. `+ D+ T/ j5 P

% v- ]# L) |: I; k( Q/ l5 gprintf("bstr front 0 byte is %c",*((int*)((int*)bstr)));; z4 V: e6 m8 W# Y
8 p  |4 |. J( m2 \
输出的是“c”是语句中的第一个字符,语句# x1 A1 |8 q/ T6 s" t
% t" ?9 a, b2 ^: `# Q
printf("bstr front 35 byte is %c\n",*((char*)(((char*)bstr+34))));' Y+ N8 o- m% j9 l4 O; [' Q9 j

( N, |$ e) }* T# _输出的是“g”语句中的最后一个字符,以上说明了SysAllocString函数用法及BSTR字符串在内存中的存储格式。7 {4 }+ \, j8 X; Z: U
( e8 H" g  |& F: {* ^
2、SysReAllocString用于在一个已有的BSTR字符串的基础上重新建立一个BSTR字符串,输入如下的语句,可以检测该函数使用后的效果:
8 z. z% {" A( A! c3 j: c1 z6 {
  1. BSTR bstr=SysAllocString(L"create bstr string");   
    / y5 |+ k2 I8 p$ p
  2. SysReAllocString (&bstr,L"new string");printf("bstr front 4 byte is %x\n",*((int*)(((int*)bstr-1))));- A+ l6 X7 t0 w% {' t! ?9 v
  3. printf("bstr front 0 byte is %c\n",*((int*)((int*)bstr)));
    1 S& ~' ?8 _5 v4 a
  4. printf("bstr front 35 byte is %c\n",*((char*)(((char*)bstr+34))));
复制代码
该语句最后输出是“14,n,g”说明SysReAllocString (&bstr,L"new string");语句是在已经生成的bstr字符串上建立一个新的字符串,因新串“new string”的长度是20,换算成16进制正好是14,而输出的g说明因新串短于旧串所以原字符串超出的部分为变化,但已经超出14所指出的长度范围。但如果新的字符串如果超出原来串的长度,那么原来的字符串将被全部覆盖,长度以新的字符串为准,下面的语句说明了这一点。  : ^& e0 x2 p& {, q' A2 e6 q1 F3 e
  1. SysReAllocString (&bstr,L"new string new string");    # [% z5 \  G+ N" _, T& H4 ?6 d
  2. printf("bstr front 4 byte is %x\n",*((int*)(((int*)bstr-1))));    9 v5 l! t6 B7 i/ X4 O! `; ?, _
  3. printf("bstr front 0 byte is %c\n",*((int*)((int*)bstr)));
    6 L4 H4 p+ B' ]/ l
  4. printf("bstr front 35 byte is %c\n",*((char*)(((char*)bstr+34))));
复制代码
3、SysAllocStringLen按照指定的长度分配一个BSTR字符串,例如:
  u0 S. {5 c6 N3 C- d1 M5 ~
  1. BSTR bstr=SysAllocStringLen(L"create bstr string",6);   
    % C* g) y, A, j/ ^
  2. printf("bstr front 4 byte is %x\n",*((int*)(((int*)bstr-1))));    : E- W/ k% ^' C* Y
  3. printf("bstr front 0 byte is %c\n",*((int*)((int*)bstr)));printf("bstr front 35 byte is %c\n",*((char*)(((char*)bstr+34))));
复制代码
虽然“create bstr string”的长度是18个字符但此语句只分派6个字符的BSTR串。' F, o& `' E. M/ p2 I+ D4 [
4、SysReAllocStringLen函数是在已有的字符串的基础上按指定长度重新分配一个新的BSTR字符串。如: ' b& z% X7 Q! I. T( v
  1. BSTR bstr=SysAllocString(L"create bstr string");   
    - h7 P# x  k9 J- E- {6 n1 k
  2. SysReAllocStringLen(&bstr,L"new string",3);' \8 Q8 d; u0 W) x. v$ I
  3. printf("bstr front 4 byte is %x\n",*((int*)(((int*)bstr-1))));
    6 u5 F5 f& `/ J( |% w" f( d
  4. printf("bstr front 0 byte is %c\n",*((int*)((int*)bstr)));3 q+ _/ a$ A* v
  5. printf("bstr front 35 byte is %c\n",*((char*)(((char*)bstr+34))));
复制代码
5、SysFreeString是释放一个BSTR字符串,BSTR字符串用后必须用此函数进行释放,例如: ; r8 ?* S% w6 s" h* U
  1. SysFreeString(bstr);
复制代码
6、SysStringLen求一个BSTR字符串的长度,如:. L( T1 \; X- C% z3 }# ?
  1. printf("bstr size is %d\n",SysStringLen(bstr));其长度是指字符串中字符个数,而非字节数。
复制代码
7、SysStringByteLen求一个BSTR字符串的字节长度,如:6 h( I+ b; B4 q7 P1 x1 U
  1. printf("bstr size is %d\n",SysStringByteLen(bstr));
复制代码
8、SysAllocStringByteLen是按字节长度分配一个BSTR字符串。
7 _, w+ q0 C+ k以上大致就是BSTR API 的用法,为了方便操作BSTR字符串,在ATL中建立了CComBSTR类专门用于操作BSTR字符,下面大致说说该类的用法。$ r/ c. d) n8 g2 S3 A0 R' u
CComBSTR类维护了公有的BSTR类型成员变量m_str,定义了8个构造函数用于创建CComBSTR对象,在析构函数中释放了m_str,在类中定义了常用的操作符并提供了对象持久性支持。
: R5 U' Z6 }3 k/ O! M1、构造一个CComBSTR对象; @/ g6 M) h2 F1 C- ~- Q
- P# {" v- h* R0 j8 Q; D$ |/ E( v
  1. CComBSTR bstr1;
    ) x% H, j$ i2 B4 z6 g! J8 T0 U! |
  2. MessageBox(NULL,bstr1.m_str ,L"bstr test",MB_OK); 创建一个空的CComBSTR 对象
    6 {6 C, V" h  c6 s6 w" p+ C& B+ t

  3. ' S. y8 c+ J7 y" [- ^3 h
  4. CComBSTR bstr2(L"this is a BSTR string");3 Q! T4 G; p3 s
  5. MessageBox(NULL,bstr2.m_str ,L"bstr test",MB_OK); 创建一个包含"this is a BSTR string"字符串的CComBSTR 对象2 \  z0 c8 \& B- \( N& k3 c4 T( O( i

  6. & S4 ]# \4 b0 v4 d0 {9 W8 d6 v
  7. CComBSTR bstr3(5);3 e; a# k% Y. M, h1 E% {
  8. MessageBox(NULL,bstr3.m_str ,L"bstr test",MB_OK);创建一个初始长度为5,含NULL的CComBSTR对象。5 u2 K. [7 c0 }+ O& ]1 ^" i  A

  9. % H; e8 x+ f! T! g/ d. c7 P
  10. CComBSTR bstr4(3,L"this is a BSTR string");: e3 A1 i/ F- t. d7 n/ G- v2 \3 B/ {
  11. MessageBox(NULL,bstr4.m_str ,L"bstr test",MB_OK);创建一个长度为3的CComBSTR对象,后面的字符串舍去。
复制代码
我们也可以从一个已经存在的CComBSTR对象创建另一个CCOmBSTR对象,如:
! p/ ?4 n- M" `: d
  1. CComBSTR bstr1(L"this is a BSTR string");
    9 b+ p! b6 R- ~, e) K6 }. b$ i3 k
  2. CComBSTR bstr2(bstr1);
    3 ^  h0 C8 o8 N5 M4 _3 j; I
  3. MessageBox(NULL,bstr2.m_str ,L"bstr test",MB_OK);
复制代码
最后的一个构造函数比较奇怪,他接受一个GUID参数,生成一个包含GUID字符串的CComBSTR对象,如: / b1 b; T2 w& ~# C& T* @, E' d
  1. static const GUID GUID_Sample={0X8a44e110,0xf134,0x11d1,{0x96,0xb1,0xba,0xdb,0xba,0xdb,0xad,0xcc}};
    0 Q% Y+ ?9 a- Z- V% j2 e
  2. CComBSTR bstr1(GUID_Sample);# U8 U2 b( Y' B' ]/ V3 j/ S  e
  3. MessageBox(NULL,bstr1.m_str ,L"bstr test",MB_OK);
复制代码
2、CComBSTR定义的常用操作如下7 u) h( I- M' z+ A
(1)“=”操作符(赋值操作),如:
6 A8 f5 }8 c1 n" ]+ G
  1. CComBSTR bstr1(L"this is a BSTR string");( K* u! @- ?" X
  2. CComBSTR bstr2=bstr1;4 _, l5 P6 L: e( I" P8 k
  3. MessageBox(NULL,bstr2.m_str ,L"BSTR test",MB_OK);bstr2和bstr1包含相同内容。
复制代码
(2)BSTR操作符把CComBSTR对象显式的或隐式的转换成BSTR字符串,如:
2 v% I8 Z6 V, g/ m5 F" l) \
  1. CComBSTR bstr1(L"this is a BSTR string");3 {  M! Z1 f/ T$ w! a5 X
  2. BSTR bstr2=bstr1;! j: N! t: D6 y3 ]
  3. MessageBox(NULL,bstr2 ,L"BSTR test",MB_OK);隐式转换
    4 s1 {" f! \" J* e7 {
  4. . [% ~- e' a( o0 D8 `" G
  5. CComBSTR bstr1(L"this is a BSTR string");
    ) J$ [9 T5 g/ p4 M2 Y
  6. MessageBox(NULL,(BSTR)bstr1 ,L"BSTR test",MB_OK);显式转换
复制代码
(3)!= 操作符,用来直接比较两个CComBSTR对象,如:7 H% `7 M+ m. j) K" ]
  1. CComBSTR bstr1(L"this is a BSTR string1");
    8 ^  i; Y# \6 I: _( K, C; p6 C
  2. CComBSTR bstr2(L"this is a BSTR string2");0 d7 Z, r) y1 `) s- H
  3. if (bstr1!=bstr2){
    ' q8 q. S' F0 O  S: V% l
  4.         MessageBox(NULL,L"true",L"BSTR test",MB_OK);9 |$ v% ]( m" u# i6 i: H1 Q
  5. }2 n/ m! e! Q) q$ X+ D
  6. else
    - F, {5 l: m8 q9 U
  7. {
    ( w0 n% a9 k0 y2 R5 r# i# I, t
  8.         MessageBox(NULL,L"false",L"BSTR $ E8 o/ y$ N: o7 ]; p! R2 b, `& M' t
  9. test",MB_OK);
    * w, M& j3 Y) E+ I
  10. }
复制代码
3、CComBSTR定义的常用方法如下
' c* r. C9 q3 o8 ~(1)Copy 方法,该方法拷贝CComBSTR对象中的BSTR字符串并返回BSTR字符串,如:
: H0 l1 }: {& x4 w! a
  1. CComBSTR bstr1(L"this is a BSTR string1");
    & z; [4 i) ]$ b6 _3 M3 Q
  2. BSTR bstr2=bstr1.Copy ();
    + Q: b; }1 j7 N+ e+ Q
  3. MessageBox(NULL,bstr2,L"BSTR test",MB_OK);
复制代码
(2)CopyTo 方法将CComBSTR对象中的BSTR字符串拷贝到新的位置,必须显式的调用SysStringFree来释放返回的字符串,如: ) R* `, i: o- L+ d1 x/ k
  1. CComBSTR bstr1(L"this is a BSTR string1");
    ) ]: }/ Y9 B( G
  2. BSTR bstr2;
    . E* v1 }) X1 u4 X6 X5 Z7 G6 e
  3. bstr1.CopyTo(&bstr2);4 T( r3 N* F# o, o: C/ a, m
  4. MessageBox(NULL,bstr2,L"BSTR test",MB_OK);
复制代码
(3)Attach方法把一个BSTR字符串附加到CComBSTR对象上,如:
- C/ o, X: }2 a' _+ `
  1. BSTR bstr1=::SysAllocString (L"this is a BSTR string");/ o6 \# \/ T/ V. X6 j
  2. CComBSTR bstr2;
    ! N' q. k7 l- R3 B
  3. bstr2.Attach(bstr1);1 K' L+ ?6 L) q9 z6 Q1 {
  4. MessageBox(NULL,bstr2.m_str ,L"BSTR test",MB_OK);
复制代码
(4)Detach方法分离BSTR字串与CComBSTR对象,用法:' Q) f0 Z9 k5 e9 y3 Q. x
  1. bstr2.Detach ();
复制代码
(5)Empty 方法,释放对象中的BSTR字符串。 CComBSTR对象还有一些其它的方法,如附加字符串、大小写转换、对象持久性等并不常用。总之BSTR字符串是COM技术的基石,CComBSTR是处理BSTR字符串的利器,深入理解BSTR字符串是研究COM技术必须经过的关口。
* s/ \$ l0 F6 `+ t- l# W8 n/ ^
. j2 p% _. W6 `! V' \; c$ j
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2026-8-15 06:39 , Processed in 0.018583 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表