博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用数据与VARIANT之间的转换---从网上整理
阅读量:5056 次
发布时间:2019-06-12

本文共 8314 字,大约阅读时间需要 27 分钟。

//头文件  1 #pragma once 2 class VariantConvert 3 { 4 public: 5     VariantConvert(void); 6     ~VariantConvert(void); 7  8 public: 9     void short2Variant(short sData,VARIANT &va);10     //VARIANT short2Var(shortsData);11     void long2Variant(long lData,VARIANT& va);12     void float2Variant(float fData, VARIANT& va);13     void double2Variant(double dData,VARIANT &va);14     void unChar2Variant(unsigned char chData,VARIANT &va);15     void arrayDouble2Variant(double *pArray,int iDim1Num,int iDim2Num,VARIANT &vaArray);16     void arrayDouble2Variant(double *pArray,int iDimlength,VARIANT &vaArray);17     void arraylong2Variant(long *pArray,int iDim1Num,int iDim2Num,VARIANT& vaArray);18     void arraylong2Variant(long *pArray,int iDimlength,VARIANT &vaArray);19     //void arrayunChar2Variant(byte *pArray,int iDim1Num,int iDim2Num,VARIANT &vaArray );20     void arrayunChar2Variant(byte *pArray,int iDimlength,VARIANT &vaArray);21 public:22     //void Variant2Data(VARIANT vaData, void&Data);23     bool Variant2short(VARIANT vaData, short &sData);24     bool Variant2long(VARIANT vaData, long & lData);25     bool Variant2float(VARIANT vaData,float& fData);26     bool Variant2double(VARIANT vaData,double & dData);27     void Variant2Array(VARIANT vaArray,UINT &iDim,long&iDim1Num,long &iDim2Num,LPVOID *pData);28     void Variant2Array(VARIANT vaArray,UINT &nDim,long&lDimLenght,VOID** pData);29 };

  实现文件

#include "StdAfx.h"#include "VariantConvert.h"VariantConvert::VariantConvert(void){}VariantConvert::~VariantConvert(void){}void VariantConvert::short2Variant(short sData,VARIANT& va){	va.vt=VT_I2;	va.iVal = sData;}void VariantConvert:: long2Variant(long lData,VARIANT& va){	va.vt = VT_I4;	va.lVal = lData;}void VariantConvert::float2Variant(float fData,VARIANT& va){	va.vt = VT_R4;	va.fltVal = fData;}void VariantConvert::double2Variant(double dData,VARIANT& va){	va.vt = VT_R8;	va.dblVal = dData;}void VariantConvert::unChar2Variant(unsigned char chData,VARIANT& va){	va.vt = VT_UI1;	va.bVal = chData;}void VariantConvert::arrayDouble2Variant(double *pArray,int iDim,int iDim1Num,int iDim2Num,VARIANT&vaArray)	{		SAFEARRAY *psa;	SAFEARRAYBOUND rgsabound[2];// 二维需要定义两个元素	rgsabound[0].lLbound =0; // 一维的下界(下界就是下标的最小值)	rgsabound[0].cElements=iDim1Num; // 一维的元素的个数	rgsabound[1].lLbound =0; // 二维的下界	rgsabound[1].cElements =iDim2Num; // 二维的元素的个数	long lIndex[2]; // 定义索引	psa = SafeArrayCreate(VT_R8,2, rgsabound); // 按照二维数组分配空间	for ( long k = 0; k< iDim1Num; k++)	{		lIndex[0] =k; 			for (long num = 0; num < iDim2Num;num++)			{ 				lIndex[1] =num; 				SafeArrayPutElement (psa, lIndex,&pArray[k*iDim2Num+num]); //lIndex作为SAFEARRAY中的索引。 			}	}	vaArray.vt =VT_ARRAY|VT_R8; // 类型	vaArray.parray =psa; // 赋值}// One dimension Convertvoid VariantConvert::arrayDouble2Variant(double *pArray,int iDimlength,VARIANT &vaArray){	SAFEARRAY *psa;	SAFEARRAYBOUND rgsabound[1];	rgsabound[0].lLbound=0;	rgsabound[0].cElements=iDimlength;	psa = SafeArrayCreate(VT_R8,1,rgsabound);	long lIndex[1];	for (int k=0; k < iDimlength; k++)	{		lIndex[0]=k;		SafeArrayPutElement(psa,lIndex,&pArray[k]);	}	vaArray.vt = VT_ARRAY|VT_R8;	vaArray.parray= psa;}void VariantConvert::arraylong2Variant(long *pArray,int iDim1Num,int iDim2Num,VARIANT& vaArray){	SAFEARRAY *psa;	SAFEARRAYBOUND rgsabound[2]; // 二维需要定义两个元素	rgsabound[0].lLbound =0; // 一维的下界	rgsabound[0].cElements=iDim1Num; // 一维的元素的个数	rgsabound[1].lLbound =0; // 二维的下界	rgsabound[1].cElements =iDim2Num; // 二维的元素的个数	long lIndex[2]; // 定义索引	psa = SafeArrayCreate(VT_I4, 2, rgsabound); // 按照二维数组分配空间	for ( long k = 0; k < iDim1Num; k++){		lIndex[0] =k; 			for (long num = 0; num < iDim2Num;num++ )			{ 				lIndex[1] =num; 				SafeArrayPutElement (psa, lIndex,&pArray[k*iDim2Num+num]); //lIndex作为SAFEARRAY中的索引。 			}	}	vaArray.vt = VT_ARRAY|VT_I4; 	vaArray.parray = psa; }void VariantConvert::arraylong2Variant(long *pArray,int iDimlength,VARIANT &vaArray){	SAFEARRAY *psa;	SAFEARRAYBOUND rgsabound[1];	rgsabound[0].lLbound=0;	rgsabound[0].cElements=iDimlength;	psa = SafeArrayCreate(VT_I4,1,rgsabound);	long lIndex[1];	for (int k=0; k < iDimlength; k++)	{		lIndex[0]=k;		SafeArrayPutElement(psa,lIndex,&pArray[k]);	}	vaArray.vt = VT_ARRAY|VT_I4;	vaArray.parray= psa;}//////void VariantConvert::arrayunChar2Variant(byte *pArray,int iDim1Num,int iDim2Num,VARIANT& vaArray){	SAFEARRAY *psa;	SAFEARRAYBOUND rgsabound[2]; // 二维需要定义两个元素	rgsabound[0].lLbound =0; // 一维的下界	rgsabound[0].cElements=iDim1Num; // 一维的元素的个数	rgsabound[1].lLbound =0; // 二维的下界	rgsabound[1].cElements =iDim2Num; // 二维的元素的个数	long lIndex[2]; // 定义索引	psa = SafeArrayCreate(VT_UI1, 2, rgsabound); // 按照二维数组分配空间	for ( long k = 0; k < iDim1Num; k++)	{		lIndex[0] =k;		for (long num = 0; num < iDim2Num;num++)		{ 			lIndex[1] =num; 			SafeArrayPutElement (psa, lIndex,&pArray[k*iDim2Num+num]); //lIndex作为SAFEARRAY中的索引。 		}	}	vaArray.vt =VT_ARRAY|VT_UI1; //vaArray为安全数组类型,每个元素为unsigned char 类型	vaArray.parray =psa; }//////void VariantConvert::arrayunChar2Variant(byte *pArray,int iDimlength,VARIANT &vaArray){	SAFEARRAY *psa;	SAFEARRAYBOUND rgsabound[1];	rgsabound[0].lLbound=0;	rgsabound[0].cElements=iDimlength;	psa = SafeArrayCreate(VT_UI1,1,rgsabound);	long lIndex[1];	for (int k=0; k < iDimlength; k++)	{		lIndex[0]=k;		SafeArrayPutElement(psa,lIndex,&pArray[k]);	}	vaArray.vt = VT_ARRAY|VT_UI1;	vaArray.parray= psa;}//////// void VariantConvert::Variant2Data(VARIANT vaData, void& Data)// {// switch(vaData.vt)// {//   case VT_I2://   Data =vaData.iVal;//  break;//   case VT_I4://   Data =vaData.lVal;//  break;//   case VT_R4://   Data =vaData.fltVal;//  break;//   case VT_R8://   Data =vaData.dblVal;//  break;//   case VT_UI1://   Data =vaData.bVal;//  break;//// }//}//////bool VariantConvert::Variant2short(VARIANT vaData, short& sData){	if (vaData.vt == VT_I2)	{		sData = vaData.iVal;		return true;	}	else	{		return false;	}}//////bool VariantConvert::Variant2long(VARIANT vaData, long& lData){	if (vaData.vt == VT_I4)	{		lData = vaData.iVal;		return true;	}	else	{		return false;	}}//////bool VariantConvert::Variant2float(VARIANT vaData,float& fData){	if (vaData.vt == VT_R4)	{		fData = vaData.iVal;		return true;	}	else	{		return false;	}}//////bool VariantConvert::Variant2double(VARIANT vaData,double& dData){	if (vaData.vt == VT_R4)	{		dData = vaData.iVal;		return true;	}	else	{		return false;	}}//////void VariantConvert::Variant2Array(VARIANT vaArray,UINT& iDim,long& iDim1Num,long& iDim2Num, LPVOID *pData){	//   //获得维数 	//UINT iDim =SafeArrayGetDim(vaStartMark.parray); 	iDim = SafeArrayGetDim(vaArray.parray);	if (iDim != 2)	{		return;	}	//   //获得元素大小,这个没什么用 	UINT uEleSize = SafeArrayGetElemsize(vaArray.parray );	// 获得一维的下界和上界 	long nDim1UBound; 	long nDim1LBound; 	SafeArrayGetUBound( vaArray.parray, 1, &nDim1UBound); 	SafeArrayGetLBound( vaArray.parray, 1, &nDim1LBound);	//   //获得二维的下界和上界 	long nDim2UBound; 	long nDim2LBound; 	SafeArrayGetUBound( vaArray.parray, 2, &nDim2UBound); 	SafeArrayGetLBound( vaArray.parray, 2, &nDim2LBound);	//  	//   //计算元素个数,分配空间使用 	//long nDim1Count = nDim1UBound - nDim1LBound +1; 	//long nDim2Count = nDim2UBound - nDim2LBound + 1;	iDim1Num = nDim1UBound - nDim1LBound + 1;	iDim2Num = nDim2UBound - nDim2LBound +1;		int x = 0; 	int y = 0; 	long lIndex[2];	for ( long nDim1 = nDim1LBound; nDim1 < nDim1UBound; nDim1++)	{		lIndex[0] =nDim1; 			y =0; 		for ( long nDim2 =nDim2LBound; nDim2 < nDim2UBound; nDim2++)		{ 			lIndex[1] =nDim2; 			SafeArrayGetElement (vaArray.parray, lIndex,pData[x*nDim2UBound+y]);			y++; 		} 		x++; 	}	}//////void VariantConvert::Variant2Array(VARIANT vaArray,long& lDimLenght,VOID** pData){	//   //获得维数 	//UINT iDim =SafeArrayGetDim(vaStartMark.parray); 	if (nDim != 1)	{		return;	}	//   //获得元素大小,这个没什么用 	UINT uEleSize = SafeArrayGetElemsize(vaArray.parray );	// 获得一维的下界和上界 	long nDim1UBound; 	long nDim1LBound; 	SafeArrayGetUBound( vaArray.parray, 1, &nDim1UBound); 	SafeArrayGetLBound( vaArray.parray, 1, &nDim1LBound);	//   //计算元素个数,分配空间使用 	//long nDim1Count = nDim1UBound - nDim1LBound +1; 	//long nDim2Count = nDim2UBound - nDim2LBound + 1;	lDimLenght = nDim1UBound - nDim1LBound +1;	// 转换 	int x = 0; 	int y = 0; 	long lIndex[1];	for ( long nDim1 = nDim1LBound; nDim1 < nDim1UBound; nDim1++)	{ 			SafeArrayGetElement (vaArray.parray, lIndex,&pData[x]); 	} 	x++; }

  

转载于:https://www.cnblogs.com/yuanpan666/p/5539644.html

你可能感兴趣的文章
group by order by
查看>>
Oracle学习之简单查询
查看>>
log4j配置
查看>>
linux 配置SAN存储-IPSAN
查看>>
java学习笔记之String类
查看>>
pymysql操作mysql
查看>>
Linux服务器删除乱码文件/文件夹的方法
查看>>
牛腩记账本core版本源码
查看>>
Word Break II
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
jdk从1.8降到jdk1.7失败
查看>>
一些关于IO流的问题
查看>>
mongo备份操作
查看>>
8 -- 深入使用Spring -- 3...1 Resource实现类InputStreamResource、ByteArrayResource
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
一个关于vue+mysql+express的全栈项目(六)------ 聊天模型的设计
查看>>
【知识库】-数据库_MySQL 的七种 join
查看>>
.net 写文件上传下载webservice
查看>>
noSQL数据库相关软件介绍(大数据存储时候,必须使用)
查看>>
iOS开发——缩放图片
查看>>