Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2978581
  • 博文数量: 950
  • 博客积分: 52280
  • 博客等级: 大将
  • 技术积分: 13070
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-07 18:49
文章分类

全部博文(950)

文章存档

2011年(1)

2008年(949)

我的朋友

分类: C/C++

2008-08-07 18:54:33

下载本文示例代码
一、概述

32位宽字符串,前面32位为长度,尾部以0结束

二、相关定义
BSTR (又称Basic 类型字符串)
LPOLESTR

相关宏定义:


typedef unsigned short wchar_t; (unsigned short为两字节)

typedef wchar_t WCHAR;

typedef WCHAR OLECHAR;	(Win32)

typedef OLECHAR* BSTR;

typedef /* [string] */ OLECHAR __RPC_FAR *LPOLESTR;

三、使用

1.分配


// Create BSTR containing "Text"

bs = SysAllocString(L"Text")



// Create BSTR containing "Te"

bs = SysAllocStringLen(L"Text", 2)

// Create BSTR containing "Text" followed by \0 and a junk character

bs = SysAllocStringLen(L"Text", 6)



// Reallocate BSTR bs as "NewText"  重新赋值

f = SysReAllocString(&bs, "NewText"); 

2.长度


// Get character length of string.

cch = SysStringLen(bs);

3.释放


// Deallocate a string.

SysFreeString(bs);

四、转换问题

1 使用_bstr_t


BSTR tmpBStr;

m_pObject1->get_ObjectString(&tmpBStr);

_bstr_t tmpbstr(tmpBStr, FALSE); //将得到的BSTR作为构造函数参数

SetDlgItemText(IDC_CURPROPVAL, tmpbstr);

注意: 直接赋值转换,会引发内存泄漏


BSTR tmpBStr;

m_pObject1->get_ObjectString(&tmpBStr);

_bstr_t tmpbstr;

tmpbstr= tmpBStr; //Caution: 内存泄漏参数

SetDlgItemText(IDC_CURPROPVAL, tmpbstr);

2 使用T2COLE、T2OLE等

#include <afxpriv.h>

在转换前需要添加宏

USES_CONVERSION


#define USES_CONVERSION int _convert = 0; \

	_convert;\

	 UINT _acp = GetACP();\

	 _acp;\

	 LPCWSTR _lpw = NULL;\

	 _lpw;\

	 LPCSTR _lpa = NULL;\

	 _lpa

这个宏提供了一些临时变量供转化用

看一下其在单字节字符集时的宏定义


#define T2COLE(lpa) A2CW(lpa)

#define T2OLE(lpa) A2W(lpa)

#define OLE2CT(lpo) W2CA(lpo)

#define OLE2T(lpo) W2A(lpo)

  

#define A2CW(lpa) ((LPCWSTR)A2W(lpa))

#define W2CA(lpw) ((LPCSTR)W2A(lpw))



#define A2W(lpa) (\

	((_lpa = lpa) == NULL) ? NULL : (\

		_convert = (lstrlenA(_lpa) 1),\

		ATLA2WHELPER((LPWSTR) alloca(_convert*2), _lpa, _convert)))



#define W2A(lpw) (\

	((_lpw = lpw) == NULL) ? NULL : (\

		_convert = (lstrlenW(_lpw) 1)*2,\

		ATLW2AHELPER((LPSTR) alloca(_convert), _lpw, _convert)))



LPSTR AFXAPI AfxW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars)

{

	if (lpw == NULL)

		return NULL;

	ASSERT(lpa != NULL);

	// verify that no illegal character present

	// since lpa was allocated based on the size of lpw

	// don''t worry about the number of chars

	lpa[0] = ''\0'';

	VERIFY(WideCharToMultiByte(CP_ACP, 0, lpw, -1, lpa, nChars, NULL, NULL));

	return lpa;

}



LPWSTR AFXAPI AfxA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars)

{

	if (lpa == NULL)

		return NULL;

	ASSERT(lpw != NULL);

	// verify that no illegal character present

	// since lpw was allocated based on the size of lpa

	// don''t worry about the number of chars

	lpw[0] = ''\0'';

	VERIFY(MultiByteToWideChar(CP_ACP, 0, lpa, -1, lpw, nChars));

	return lpw;

}

3 .CString 对转化的支持


//m_cstrCaption是一个CString 对象。



BSTR bstrCaption =m_cstrCaption.AllocSysString();

FireChange(&bstrCaption,&m_lAlignment);

::SysFreeString(bstrCaption);

(全文完) 下载本文示例代码
阅读(92) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~