⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydll.cpp

📁 VBVC之间的通信
💻 CPP
字号:
   /* functions */ 
   #include "MyDll.h" // use here the name of your header file

   /*========================================================
    AddLongs_Pointer - a function to add all elements of an array passed
    as a pointer (long *)
    returns the sum of the elements
   ==========================================================*/ 

   long __declspec (dllexport) __stdcall AddLongs_Pointer(
   long *plArrayOfLongs, // the array's pointer
   long lElements) // number of elements to add
   {
      long lSum;
      long i;

      lSum = 0;

      if(lElements > 0)
      {
         for (i=0; i < lElements; i++)
            lSum = lSum + plArrayOfLongs[i];
      }
      // returning
      return(lSum);
   }

   /*===============================================================
   AddLongs_SafeAray - a function to add all elements of an one-dimensional
   array of longs
   return codes: 0 - Success
                 >0 - Failure
   =================================================================*/ 
   long __declspec (dllexport) __stdcall AddLongs_SafeArray(
      SAFEARRAY **psaArray, // the array to use
      long *plSum) // returns the sum of all elements of the array
   {
      long lElements; // number of elements in the array
      long iCount;
      HRESULT lResult; // return code for OLE functions
      long *pArrayElements; // pointer to the elements of the array

      // initializing the output
      *plSum=0;

      // checking if it is an one-dimensional array
      if ( (*psaArray)->cDims != 1 ) return(1);

      // checking if it is an array of longs
      if ( (*psaArray)->cbElements != 4 ) return(2);

      // how many elements are there in the array
      lElements=(*psaArray)->rgsabound[0].cElements;

      // locking the array before using its elements
      lResult=SafeArrayLock(*psaArray);
      if(lResult)return(3);

      // using the array
      pArrayElements=(long*) (*psaArray)->pvData;
      for (iCount=0; iCount < lElements; iCount++)
         *plSum = *plSum + pArrayElements[iCount];

      // releasing the array
      lResult=SafeArrayUnlock(*psaArray);
      if (lResult) return(4);

      // returning
      return(0);
   } 
/*
   void __stdcall ModifyStruct(struct tagTestUDT *t, long nTotalItem)
      {
         // modify the last element
         long i = nTotalItem - 1;  // the array begins from 0

         (t+i)->l = 200;

         SysFreeString((t+i)->str );  // free the string passed in
         (t+i)->str = SysAllocString(OLESTR("Changed."));
      }
 
VARIANT _stdcall retVariantArray(void) {
         COleSafeArray saRet;
         DWORD numElements[] = {10, 10}; // 10x10

         // Create the safe-array...
         saRet.Create(VT_R8, 2, numElements);

         // Initialize it with values...
         long index[2];
         for(index[0]=0; index[0]<10; index[0]++) {
            for(index[1]=0; index[1]<10; index[1]++) {
               double val = index[0] + index[1]*10;
               saRet.PutElement(index, &val);
            }
         }

         // Return the safe-array encapsulated in a VARIANT...
         return saRet.Detach();
      } 
*/

void _stdcall FillUDTVariable(MyStruct * ms)
      {
      ms->f1 = 2001;
      ms->f2 = 20012001;
      ms->f3 = 255;
      ms->f4 = L'A';
      ms->f5 = 200.1f;
      }

      void _stdcall FillUDTSafeArray(LPSAFEARRAY FAR * ppsa)
      {
      MyStruct * pdata;
      unsigned int i;
      pdata = (MyStruct*)((*ppsa)->pvData);
      for (i=0;i<((*ppsa)->rgsabound->cElements);i++,pdata++)
         FillUDTVariable(pdata);
      } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -