对不同数据类型拼接成一个内存块的封装

原创 2015年04月06日 14:23:05
/*================================================================*
// NSDataParser.h: interface for the CNSDataParser class.
【头文件】:	NSDataParser.h
【功  能】:	该模块属于独立模块,主要完成管理各种类型数据,
			转换成一个连续的内存数据块。返回的连续数据块为LPVOID类型,
			通过GetData()函数获取到该数据块的地址,通过GetDataSize()
			返回数据块的大小
【注  意】:	
*================================================================*/
#if !defined(AFX_DATAPARSER_H__C18BF820_ED69_42B0_A8F9_9570387F753C__INCLUDED_)
#define AFX_DATAPARSER_H__C18BF820_ED69_42B0_A8F9_9570387F753C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxtempl.h>
//=========================================================================
//=========================================================================
class  CDataParser
{
public:
	CDataParser();
	virtual ~CDataParser();
public:
	// 将数据写入内存
	/*================================================================*
	【函數】:	Add(...)
	【功能】:	往内存中添加各种数据,数据内存所占的长度会根据所加入的
				数据类型动态增加
	【参数】:	
	【返回】:	
	【例子】:	
	【注意】:	当参数为CNSVarObj类型时,往内存中加的是变量的工程值,
				即pVar->GetVarData(),并不是把变量对象中所有数据全加入内存
	*================================================================*/
	virtual BOOL Add(COleVariant* pvData);
	virtual BOOL Add(WORD wData);
	virtual BOOL Add(DWORD wData);
	virtual BOOL Add(LPVOID pData,int iLen);
	virtual BOOL Add(BOOL wData);
	virtual BOOL Add(BYTE wData);
	virtual BOOL Add(char wData);
	virtual BOOL Add(short wData);
	virtual BOOL Add(long wData);
	virtual BOOL Add(float wData);
	virtual BOOL Add(double wData);
	virtual BOOL Add(const COleDateTime& wData);
	virtual BOOL Add(const CString& strData);
public:
	// 从内存中读取值,注:先调用InitData初始化内存数据然后再调用GetData
	virtual void InitData( void *pData,unsigned long nDataSize);
	virtual BOOL GetData(COleVariant& pvData);
	virtual BOOL GetData(WORD &wData);
	virtual BOOL GetData(DWORD &wData);
	virtual BOOL GetData(LPVOID pData);
	virtual BOOL GetData(BOOL &wData);
	virtual BOOL GetData(BYTE &wData);
	virtual BOOL GetData(char &wData);
	virtual BOOL GetData(short &wData);
	virtual BOOL GetData(long &wData);
	virtual BOOL GetData(float &wData);
	virtual BOOL GetData(double wData);
	virtual BOOL GetData(COleDateTime& wData);
	virtual BOOL GetData(CString& strData);
	// 获取当前内存位置
	unsigned long GetCurrentPos();
public:
	/*================================================================*
	【函數】:	RemoveAllDataItem()
	【功能】:	删除所有数据将所占的内存清空
	【参数】:	
	【返回】:	返回BOOL类型,成功为TRUE,否则为FALSE
	【例子】:	
	【注意】:	
	*================================================================*/
	virtual BOOL	RemoveAllDataItem();

	/*================================================================*
	【函數】:	GetData()
	【功能】:	获取连续内存地址
	【参数】:	
	【返回】:	返回指向内存的首地址
	【例子】:	
	【注意】:	
	*================================================================*/
	virtual LPVOID  GetData();

	/*================================================================*
	【函數】:	GetDataSize()
	【功能】:	获取连续内存的大小
	【参数】:	
	【返回】:	返回指向内存的大小
	【例子】:	
	【注意】:	
	*================================================================*/
	virtual DWORD	GetDataSize();
protected:
	double VarToDouble(COleVariant* pvData);
protected:
	// 数据大小
	UINT m_uDataSize;
	// 数据
	LPVOID m_pData;
	// 内存需要删除标志
	bool m_bMustClean;
	unsigned long m_pCurDataPos;
private:
	CRITICAL_SECTION CRITICAL_LOCK;
};
#endif // !defined(AFX_DATAPARSER_H__C18BF820_ED69_42B0_A8F9_9570387F753C__INCLUDED_)
#include "stdafx.h"
#include "WSDataParser.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CDataParser::CDataParser()
{
	m_bMustClean = true;
	m_pData = NULL;
	m_uDataSize = 0;
	m_pCurDataPos = 0;
	InitializeCriticalSection( &CRITICAL_LOCK);
}

CDataParser::~CDataParser()
{
	RemoveAllDataItem();
	DeleteCriticalSection(&CRITICAL_LOCK);
}
double CDataParser::VarToDouble(COleVariant* pvData)
{
	double dfData;
	COleVariant vData;
	
	vData = *pvData;
	
	switch(pvData->vt)
	{
	case VT_I1:
		dfData = (double)vData.cVal ;
		break;
	case VT_UI1:
		dfData = (double)vData.bVal;
		break;
	case VT_I2:
		dfData = (double)vData.iVal;
		break;
	case VT_UI2:
		dfData = (double)vData.uiVal;
		break;
	case VT_INT:
		dfData = (double)vData.intVal;
		break;
	case VT_UINT:
		dfData = (double)vData.uintVal;
		break;
	case VT_I4:
		dfData = (double)vData.lVal;
		break;
	case VT_UI4:
		dfData = (double)vData.ulVal;
		break;
	case VT_R4:
		dfData = (double)vData.fltVal;
		break;
	case VT_R8:
		dfData = (double)vData.dblVal;
		break;
	case VT_BOOL:
		if(vData.boolVal == VARIANT_FALSE)
			dfData = 0;
		else
			dfData = 1;
		break;
	case VT_CY:
	case VT_DATE:
	case VT_BSTR:
	case VT_DISPATCH:
	case VT_ERROR:
	case VT_VARIANT:
	case VT_UNKNOWN:
	case VT_DECIMAL:
	case VT_EMPTY:
	case VT_NULL:
	default:
		dfData = 0.0;
		break;
	}
	
	return dfData;
}
BOOL CDataParser::Add(LPVOID pData,int iLen)
{
	BOOL bSuccess = FALSE;
	EnterCriticalSection(&CRITICAL_LOCK);
	if(pData != NULL && iLen > 0)
	{
		if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);

		// 先构造类型
		DWORD dwDataVT = VT_VOID;
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
		m_uDataSize += 4;

		// 构造长度
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&iLen,4);
		m_uDataSize += 4;

		// 构造值
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iLen, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,(char*)pData,iLen);
		
		m_uDataSize += iLen;
		bSuccess = TRUE;
	}
	LeaveCriticalSection(&CRITICAL_LOCK);
	return bSuccess;
}
BOOL CDataParser::Add(const CString& strData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);

	char *chAnsiCode = CUtility::ConvertStringToChar(strData);
	if(chAnsiCode)
	{
		//=================================================================================
		
		if(!m_pData) 
			m_pData = GlobalAlloc(GMEM_FIXED,1);
		
		// 先构造变量类型
		DWORD dwDataVT = VT_BSTR;
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
		m_uDataSize += 4;
		
		int istrVarNameLen = strlen(chAnsiCode);
		
		// 构造长度
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&istrVarNameLen,4);
		m_uDataSize += 4;
		
		
		//构造值
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + istrVarNameLen, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,chAnsiCode,istrVarNameLen);
		
		m_uDataSize += istrVarNameLen;
		
		//=================================================================================
		
		delete chAnsiCode;
		chAnsiCode = NULL;

		bSuccess = TRUE;
	}
	LeaveCriticalSection(&CRITICAL_LOCK);
	return bSuccess;
}
BOOL CDataParser::Add(WORD wData)
{
	BOOL bSuccess = FALSE;

	EnterCriticalSection(&CRITICAL_LOCK);
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_UI2;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	bSuccess = TRUE;
	//=================================================================================
	LeaveCriticalSection(&CRITICAL_LOCK);
	return bSuccess;
}
BOOL CDataParser::Add(DWORD wData)
{
	BOOL bSuccess = FALSE;

	EnterCriticalSection(&CRITICAL_LOCK);

	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_UI4;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);
	
	return bSuccess;
}
BOOL CDataParser::Add(BOOL wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_BOOL;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);
	
	return bSuccess;
}
BOOL CDataParser::Add(BYTE wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_UI1;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(char wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_I1;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(short wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_I2;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(long wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_I4;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(float wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_R4;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(double wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_R8;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(wData);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(const COleDateTime& wData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);
	
	if(!m_pData) 
		m_pData = GlobalAlloc(GMEM_FIXED,1);
	//=================================================================================
	// 先构造类型
	DWORD dwDataVT = VT_DATE;
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&dwDataVT,4);
	m_uDataSize += 4;
	
	int iDataLen = sizeof(double);
	
	//构造值
	m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iDataLen, GMEM_MOVEABLE);
	CopyMemory((char*)m_pData + m_uDataSize,&wData.m_dt,iDataLen);
	m_uDataSize += iDataLen;
	
	//=================================================================================
	bSuccess = TRUE;
	LeaveCriticalSection(&CRITICAL_LOCK);

	return bSuccess;
}
BOOL CDataParser::Add(COleVariant* pvData)
{
	BOOL bSuccess = FALSE;
	
	EnterCriticalSection(&CRITICAL_LOCK);

	if(pvData)
	{
		if(!m_pData) 
			m_pData = GlobalAlloc(GMEM_FIXED,1);

		COleVariant nVarData = *pvData;
		// 构造数据类型
		DWORD dwvDataVT = pvData->vt;
		m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
		CopyMemory((char*)m_pData + m_uDataSize,&dwvDataVT,4);
		m_uDataSize += 4;

		double dbData = VarToDouble(pvData);
		switch(dwvDataVT)
		{
		case VT_UI1:	//Byte                    bVal;
			{
				BYTE VarDataTmp = (BYTE)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 1, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,1);
				m_uDataSize += 1;
			}break;
		case VT_I1:		//char                    cVal;
			{
				char VarDataTmp = (char)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 1, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,1);
				m_uDataSize += 1;
			}break;
		case VT_I2:		//Short                   iVal;
			{
				short VarDataTmp = (short)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 2, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,2);
				m_uDataSize += 2;
			}break;
		case VT_UI2:	//unsigned short          uiVal;
			{
				WORD VarDataTmp = (WORD)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 2, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,2);
				m_uDataSize += 2;
			}break;
		case VT_INT:	//int                     intVal;
			{
				int VarDataTmp = (int)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_UINT:	//unsigned int            uintVal;
			{

				DWORD VarDataTmp = (DWORD)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_I4:		//long                    lVal;
			{
				long VarDataTmp = (long)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_UI4:	//unsigned long           ulVal;
			{
				unsigned long VarDataTmp = (unsigned long)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_R4:		//float                   fltVal;
			{
				float VarDataTmp = (float)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,4);
				m_uDataSize += 4;
			}break;
		case VT_R8:		//double                  dblVal;
			{
				double VarDataTmp = (double)dbData;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 8, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,8);
				m_uDataSize += 8;
			}break;
		case VT_BOOL:	//VARIANT_BOOL            boolVal;
			{
				bool VarDataTmp = (dbData == 0.0) ? false : true;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 1, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,1);
				m_uDataSize += 1;
			}break;
		case VT_DATE:	//DATE                    date;
			{
				DATE VarDataTmp = nVarData.date;
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 8, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&VarDataTmp,8);
				m_uDataSize += 8;
			}break;
		case VT_BSTR:	//BSTR                    bstrVal;
			{
				CString strData = nVarData.bstrVal;
				int iLen = strData.GetLength();
				
				// 先写长度
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + 4, GMEM_MOVEABLE);
				CopyMemory((char*)m_pData + m_uDataSize,&iLen,4);
				m_uDataSize += 4;
				
				// 写字符串
				m_pData = ::GlobalReAlloc(m_pData, m_uDataSize + iLen, GMEM_MOVEABLE);

				char *chAnsiCode = CUtility::ConvertStringToChar(strData);
				if(chAnsiCode)
				{
					CopyMemory((char*)m_pData + m_uDataSize,chAnsiCode,iLen);
				
					delete chAnsiCode;
					chAnsiCode = NULL;
				}
				m_uDataSize += iLen;
			}break;
		case VT_EMPTY:
			{

			}break;
		case VT_NULL:
			{

			}break;
		default:
			break;
		}
		bSuccess = TRUE;
	}
	
	LeaveCriticalSection(&CRITICAL_LOCK);
	
	return bSuccess;
}

BOOL CDataParser::RemoveAllDataItem()
{
	if(m_bMustClean)
	{
		if(m_pData)
		{
			GlobalFree(m_pData);
			m_pData = NULL;
		}
	}
	m_uDataSize = 0;
	return TRUE;
}

LPVOID CDataParser::GetData()
{
	return m_pData;
}

DWORD CDataParser::GetDataSize()
{
	return m_uDataSize;
}
//============================================================================================
void CDataParser::InitData( void *pData,unsigned long nDataSize)
{
	m_pCurDataPos = 0;
	m_pData = pData;
	m_uDataSize = nDataSize;
	// 数据由应用层传进来,在析构中不能释放
	m_bMustClean = false;
}
unsigned long CDataParser::GetCurrentPos()
{
	return m_pCurDataPos;
}
BOOL CDataParser::GetData(COleVariant& pvData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			pvData.vt = dwDataVT;
			switch(dwDataVT)
			{
			case VT_UI1:	//Byte                    bVal;
				{
					// 构造值
					memcpy(&pvData.bVal,(char*)m_pData + m_pCurDataPos, 1);
					m_pCurDataPos += 1;
					bSuccess = TRUE;
				}break;
			case VT_I1:		//char                    cVal;
				{
					memcpy(&pvData.cVal,(char*)m_pData + m_pCurDataPos, 1);
					m_pCurDataPos += 1;
					bSuccess = TRUE;
				}break;
			case VT_I2:		//Short                   iVal;
				{
					memcpy(&pvData.iVal,(char*)m_pData + m_pCurDataPos, 2);
					m_pCurDataPos += 2;
					bSuccess = TRUE;
				}break;
			case VT_UI2:	//unsigned short          uiVal;
				{
					memcpy(&pvData.uiVal,(char*)m_pData + m_pCurDataPos, 2);
					m_pCurDataPos += 2;
					bSuccess = TRUE;
				}break;
			case VT_INT:	//int                     intVal;
				{
					memcpy(&pvData.intVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				}break;
			case VT_UINT:	//unsigned int            uintVal;
				{
					memcpy(&pvData.uintVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				
				}break;
			case VT_I4:		//long                    lVal;
				{
					memcpy(&pvData.lVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				}break;
			case VT_UI4:	//unsigned long           ulVal;
				{
					memcpy(&pvData.ulVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				}break;
			case VT_R4:		//float                   fltVal;
				{
					memcpy(&pvData.fltVal,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;
					bSuccess = TRUE;
				}break;
			case VT_R8:		//double                  dblVal;
				{
					memcpy(&pvData.dblVal,(char*)m_pData + m_pCurDataPos, 8);
					m_pCurDataPos += 8;
					bSuccess = TRUE;
				}break;
			case VT_BOOL:	//VARIANT_BOOL            boolVal;
				{
					BOOL bData = FALSE;
					memcpy(&bData,(char*)m_pData + m_pCurDataPos, 1);
					pvData.boolVal = (bData == TRUE ) ? VARIANT_TRUE : VARIANT_FALSE;
					m_pCurDataPos += 1;
					bSuccess = TRUE;
				}break;
			case VT_DATE:	//DATE                    date;
				{
					memcpy(&pvData.date,(char*)m_pData + m_pCurDataPos, 8);
					m_pCurDataPos += 8;
					bSuccess = TRUE;
				}break;
			case VT_BSTR:	//BSTR                    bstrVal;
				{
					// 先读长度
					DWORD dwDataLen = 0;
					memcpy(&dwDataLen,(char*)m_pData + m_pCurDataPos, 4);
					m_pCurDataPos += 4;

					char *chName = new char[dwDataLen + 1];
					memcpy((char*)chName,(char*)m_pData + m_pCurDataPos,dwDataLen);
					chName[dwDataLen] = '\0';
					m_pCurDataPos += dwDataLen;

					CString strData = CString(chName);
					pvData = strData;
					delete []chName;
					bSuccess = TRUE;
				}break;
			case VT_EMPTY:
				{
					
				}break;
			case VT_NULL:
				{
					
				}break;
			default:
				{
					// 当前索引退回上一次位置
					m_pCurDataPos -= 4;
					bSuccess = FALSE;
				}break;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(WORD &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_UI2)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 2);
				m_pCurDataPos += 2;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(DWORD &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_UI4)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 4);
				m_pCurDataPos += 4;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(LPVOID pData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_VOID)
			{
				// 先构造长度
				DWORD dwDataLen = 0;
				memcpy(&dwDataLen,(char*)m_pData + m_pCurDataPos,  4);
				m_pCurDataPos += 4;

				// 构造值
				memcpy(&pData,(char*)m_pData + m_pCurDataPos, dwDataLen);
				m_pCurDataPos += dwDataLen;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(BOOL &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_BOOL)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 1);
				m_pCurDataPos += 1;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(BYTE &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_UI1)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 1);
				m_pCurDataPos += 1;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(char &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_I1)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 1);
				m_pCurDataPos += 1;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(short &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_I2)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos, 2);
				m_pCurDataPos += 2;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(long &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_I4)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos,4);
				m_pCurDataPos += 4;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(float &wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_R4)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos,4);
				m_pCurDataPos += 4;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(double wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_R8)
			{
				// 构造值
				memcpy(&wData,(char*)m_pData + m_pCurDataPos,8);
				m_pCurDataPos += 8;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(COleDateTime& wData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_DATE)
			{
				// 构造值
				memcpy(&wData.m_dt,(char*)m_pData + m_pCurDataPos,8);
				m_pCurDataPos += 8;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}
BOOL CDataParser::GetData(CString& strData)
{
	BOOL bSuccess = FALSE;
	if(m_pData != NULL)
	{	
		// 当前位置小于总数据长度时
		if(m_pCurDataPos < m_uDataSize)
		{
			EnterCriticalSection(&CRITICAL_LOCK);
			
			// 先构造数据类型
			DWORD dwDataVT = VT_NULL;
			memcpy(&dwDataVT,(char*)m_pData + m_pCurDataPos,  4);
			m_pCurDataPos += 4;
			
			if(dwDataVT == VT_BSTR)
			{
				// 先构造长度
				DWORD dwDataLen = 0;
				memcpy(&dwDataLen,(char*)m_pData + m_pCurDataPos,  4);
				m_pCurDataPos += 4;
				
				char *chName = new char[dwDataLen + 1];

				// 构造值
				memcpy((char*)chName,(char*)m_pData + m_pCurDataPos, dwDataLen);
				chName[dwDataLen] = '\0';
				strData = CString(chName);
				delete []chName;

				m_pCurDataPos += dwDataLen;
				bSuccess = TRUE;
			}
			else
			{
				// 当前索引退回上一次位置
				m_pCurDataPos -= 4;
				bSuccess = FALSE;
			}
			LeaveCriticalSection(&CRITICAL_LOCK);
		}
	}
	return bSuccess;
}


c++中内存拷贝函数(C++ memcpy)详解

原型:void*memcpy(void*dest, const void*src,unsigned int count);  功能:由src所指内存区域复制count个字节到dest所指内存区域。 ...
  • sszgg2006
  • sszgg2006
  • 2012年09月17日 21:30
  • 51920

C++如何将两块内存的数据合并到一块内存

内存数据的拼接,在开发中有时候也会遇到。 内存数据p1,内存数据p2,拼接为内存数据p. p1拷贝到p的前半部分,p2拷贝到p的后半部分。 可以使用memcpy来进行数据的拷贝拼接,关键是要控制好拼接...
  • u011417605
  • u011417605
  • 2015年12月19日 14:28
  • 1511

sql语句 一个表 用户产生两条不同类型的数据 拼接成一条数据

SELECT * FROM exp1_profit_alloc_c03 AS c03_0 INNER JOIN exp1_profit_alloc_c03 AS c03_1 ON c03_0.uid ...
  • liuxin_0725
  • liuxin_0725
  • 2017年05月26日 16:46
  • 334

常见对象_把数组拼接成指定格式的字符串案例

package cn.itcast_07; /* * 把数组拼接成字符串 */ public class StringBufferTest2 { public static void main...
  • L1585931143
  • L1585931143
  • 2016年11月15日 21:40
  • 674

1217: Problem C

题目描述: 题目描述 对于给定的字符序列,从左至右将所有的数字字符取出拼接成一个无符号整数(字符序列长度小于100,拼接出的整数小于2^31,),计算并输出该整数的最大因子(如果是素数,则其...
  • z_x_b5
  • z_x_b5
  • 2013年03月14日 12:41
  • 602

把数组中的数字拼接起来组成最小的数

例如输入数组{3,32,321},则拼接起来的最小数为321323。 假设有两个数m和n,我们定义mnm,则有m>n。 m和n都是int范围内的数字,它们拼接起来的数字很可能超出int所能表示的范...
  • ChengNuoDeYongYuan
  • ChengNuoDeYongYuan
  • 2016年08月24日 19:12
  • 1322

.NET下的内存分配机制

    对于任何对象的产生,都伴随着内存空间的分配,那么本文将初步介绍一下.NET下,是如何进行内存分配的。  这里部分引用了Anytao《你必须知道的.NET》和happyhippy在《类型实例的创...
  • lerit
  • lerit
  • 2009年08月13日 10:33
  • 8298

常见对象-把int数组拼接字符串的案例

package cn.itcast_07; /* * 需求:把数组中的数据按照指定个格式拼接成一个字符串 * 举例: * int[] arr = {1,2,3}; * 输出结果: *...
  • ZHOU_VIP
  • ZHOU_VIP
  • 2017年05月11日 21:10
  • 371

把数组按指定的格式拼接成一个字符串

package day11;/** * java基础:StringBuffer * Author:知而无涯 * Description:把数组按指定的格式拼接成一个字符串 * Date...
  • qq_26963433
  • qq_26963433
  • 2017年11月03日 16:40
  • 138

封装GSON解析的实体类,快速解析

返回的json数据 有的是对象,有的是集合, 用Jsonobject一个个去解析实在是很麻烦, 可以通过泛型抽出父类做一个通用的解析类, 以我现在正在做的这个项目举例: 这是后台返回的数据格式...
  • zz592962131
  • zz592962131
  • 2016年03月16日 12:43
  • 1139
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:对不同数据类型拼接成一个内存块的封装
举报原因:
原因补充:

(最多只允许输入30个字)