OLE字符串

 

一、概述

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);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值