//Header file
1 #pragmaOnce2 classVariantconvert3 {4 Public:5Variantconvert (void);6~variantconvert (void);7 8 Public:9 voidShort2variant ( ShortSdata,variant &va);Ten //VARIANT Short2var (shortsdata); One voidLong2variant (Longldata,variant&va); A voidFloat2variant (floatFData, variant&va); - voidDouble2variant (DoubleDdata,variant &va); - voidUnchar2variant (unsignedCharChdata,variant &va); the voidArraydouble2variant (Double*parray,intIdim1num,intIdim2num,variant &Vaarray); - voidArraydouble2variant (Double*parray,intIdimlength,variant &Vaarray); - voidArraylong2variant (Long*parray,intIdim1num,intidim2num,variant&Vaarray); - voidArraylong2variant (Long*parray,intIdimlength,variant &Vaarray); + //void Arrayunchar2variant (byte *parray,int idim1num,int idim2num,variant &vaarray); - voidArrayunchar2variant (byte*parray,intIdimlength,variant &Vaarray); + Public: A //void Variant2data (VARIANT vadata, void&data); at BOOLVariant2short (VARIANT Vadata, Short&sData); - BOOLVariant2long (VARIANT Vadata,Long&lData); - BOOLVariant2float (VARIANT Vadata,float&fData); - BOOLVariant2double (VARIANT Vadata,Double&dData); - voidVariant2array (VARIANT vaarray,uint &idim,Long&idim1num,Long&idim2num,lpvoid *pData); - voidVariant2array (VARIANT vaarray,uint &ndim,Long&ldimlenght,void**pData); in};
Implementation file
#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::d ouble2variant (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];//Two-dimensional need to define two elements rgsabound[0].llbound = 0; The Nether of one dimension (the lower bound is the lowest value of subscript) Rgsabound[0].celements=idim1num; The number of elements of one dimension rgsabound[1].llbound = 0; Two-dimensional nether rgsabound[1].celements =idim2num; The number of elements of the two-dimensional long lindex[2]; Define INDEX PSA = safearraycreate (vt_r8,2, rgsabound); Allocates space for a two-dimensional array 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 as an index in the SAFEARRAY. }}VAARRAY.VT =vt_array| VT_R8; Type Vaarray.parray =PSA; Assignment}//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]; Two-dimensional need to define two elements rgsabound[0].llbound = 0; One-dimensional nether rgsabound[0].celements=idim1num; The number of elements of one dimension rgsabound[1].llbound = 0; Two-dimensional nether rgsabound[1].celements =idim2num; The number of elements of the two-dimensional long lindex[2]; Definition Index PSA = safearraycreate (VT_I4, 2, rgsabound); Allocates space for (long k = 0; k < idim1num; k++) {lindex[0] =k in a two-dimensional array, for (long num = 0; num < idim2num;num++) {lindex[1] =num; SafeArrayPutElement (PSA, Lindex,&parray[k*idim2num+num]); Lindex as an index in the 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]; Two-dimensional need to define two elements rgsabound[0].llbound = 0; One-dimensional nether rgsabound[0].celements=idim1num; The number of elements in one dimension RgsabOund[1].llbound = 0; Two-dimensional nether rgsabound[1].celements =idim2num; The number of elements of the two-dimensional long lindex[2]; Definition Index PSA = safearraycreate (VT_UI1, 2, rgsabound); Allocates space for a two-dimensional array 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 as an index in the SAFEARRAY. }}VAARRAY.VT =vt_array| VT_UI1; Vaarray is a safe array type, each element is unsigned char type 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;//Bre ak;//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 (V ADATA.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) {/////Get Dimension//uint IDim =safearraygetdim (vastartmark.parray); IDim = Safearraygetdim (Vaarray.parray); if (IDim! = 2) {return;} Get the element size, this nothing with UINT uelesize = Safearraygetelemsize (Vaarray.parray);//obtain a one-dimensional lower bound and an upper bound long ndim1ubound; Long Ndim1lbound; Safearraygetubound (Vaarray.parray, 1, &ndim1ubound); Safearraygetlbound (Vaarray.parray, 1, &ndim1lbound);///Get the lower bound and upper bounds of the two-dimensional long ndim2ubound; Long Ndim2lbound; Safearraygetubound (Vaarray.parray, 2, &ndim2ubound); Bound +1; Long Ndim2count = ndim2ubound-ndim2lbound + 1;idim1num = ndim1ubound-ndim1lbound + 1;idim2num = ndim2ubound-ndim2l Bound +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) {////Get dimension//UINT IDim = Safearraygetdim (Vastartmark.parray); if (NDim! = 1) {return;} Get the element size, this nothing with UINT uelesize = Safearraygetelemsize (Vaarray.parray);//obtain a one-dimensional lower bound and an upper bound long ndim1ubound; Long Ndim1lbound; Safearraygetubound (Vaarray.parray, 1, &ndim1ubound); Safearraygetlbound (Vaarray.parray, 1, &ndim1lbound);///////compute the number of elements, allocating space using//long Ndim1count = Ndim1ubound-ndim1lboun D +1; Long Ndim2count = ndim2ubound-ndim2lbound + 1;ldimlenght = ndim1ubound-ndim1lbound +1;//convert int x = 0; int y = 0; Long lindex[1];for (Long nDim1 = Ndim1lbound; nDim1 < ndim1ubound; ndim1++) {safearraygetelement (Vaarray.parray, lInd EX,&PDATA[X]); } x + +; }
Conversion between common data and variant---collated from the web