實現文件 ...
//頭文件
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 Convert void 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++; }