VB VC 混合编程(dll)中 数组 的传入传出

       以前用到的神经网络都是在matlab中调的,而项目需要,要写个神经网络的算法,系统主界面是VB写的,在VB中做网络训练速度是不照的,用VB,VC 的混合编程,但是一直被VB与Dll之间的数组传递这个难题所困扰,在网上搜了也找不到答案,据说safearray可以解决,但是看了下比较麻烦,在 CSDN社区中也没有找到答案,但是社区中一个朋友的指点,给我找到解决问题的方法了。下面总结下共享。
    数值型数组在VB中其数据是连续存放的,相当于一维的,而在C/C++中数组可以等价于指向数组第1个元素的指针。可以用引用的方式把VB中数组的第1个元素的地址传给VC编写的DLL,在DLL中用一个指针来接收,这样就实现了VB到Dll在中数组的传递。从DLL传递数组给VB方法相同,过程相反.
    如果是二维数组,则把二维数组按照一维数组的方式传入传出,只是在使用的时候,顺便把二维数组的行和列数传递即可。
    总体思想是这样的。下面看例子。

VC中:

double _stdcall OneDimensionArrayTest(double *inArr,int nCount,double *outArr,int* utArrCount) //一维数组的传入传出
{
     int iNum=nCount;
     double *dRes=new double[iNum];
     int i;
     for(i=0;i<nCount;i++)
     {
         dRes[i]=inArr[i]*2;
     }

     for(i=0;i<nCount;i++)
     {
         outArr[i]=dRes[i];
     }
     *ioutArrCount=iNum;
     return dRes[0];
     delete []dRes;
}


void _stdcall TwoDimensionArrayTest(double *inArr,int nRows,int nCols,double *outArr,int* outRows,int *outCols) //二维数组的传入传出
{
     double *dRes=new double[nRows*nCols];
     int i;
     int j;
     for(i=0;i<nRows;i++)
     {
         for(j=0;j<nCols;j++)
         {
             dRes[nCols*i+j]=inArr[nCols*i+j]*2;
         }
     }
     for(i=0;i<nRows;i++)
     {
         for(j=0;j<nCols;j++)
         {
             outArr[nCols*i+j]=inArr[nCols*i+j]*2;
         }
     }
     *outRows=nRows;
     *outCols=nCols;
     delete [] dRes;
}


LIBRARY      "TestDll"

EXPORTS
    ; 此处可以是显式导出
    Add @1
    darray @2
    OneDimensionArrayTest @3
    TwoDimensionArrayTest @4

VB中
Declare Function OneDimensionArrayTest Lib "D:/在编程序/Dll/VBLoadDll/TestDll.dll" (ByRef inputer As Double, ByVal inLength As Long, ByRef output As Double, ByRef outLength As Long) As Double
Declare Function TwoDimensionArrayTest Lib "D:/在编程序/Dll/VBLoadDll/TestDll.dll" (ByRef inputer As Double, ByVal inRows As Long, ByVal inCols As Long, ByRef outputer As Double, ByRef outRows As Long, ByRef outCols As Long)

Private Sub cmdTest2_Click()
Dim inputer(8) As Double
Dim out(9) As Double
Dim res As Double
Dim m As Long
inputer(0) = 1.2
inputer(1) = 2.3
inputer(2) = 1
res = OneDimensionArrayTest(inputer(0), 9, out(0), m)

Dim str As String
Dim i As Integer
For i = 0 To UBound(out)
   str = str + "   " + CStr(out(i))
Next
MsgBox CStr(m), vbOKOnly, "一维数组的元素个数"
MsgBox str, vbOKOnly, "一维数组的元素"
End Sub

Private Sub cmdTest3_Click()
Dim iRows As Integer
Dim iCols As Integer
iRows = 3
iCols = 4
Dim inputer() As Double
ReDim inputer(iRows, iCols)
Dim outputer() As Double
ReDim outputer(iRows, iCols)
Dim oRows As Long
Dim oCols As Long
Dim i, j As Integer
For i = 0 To UBound(inputer, 1)
   For j = 0 To UBound(inputer, 2)
       inputer(i, j) = (i + 1) * (j + 1)
   Next
Next
Dim str As String
For i = 0 To UBound(inputer, 1)
   For j = 0 To UBound(inputer, 2)
       str = str + "   " + CStr(inputer(i, j))
   Next
   str = str + vbCrLf
Next

Call TwoDimensionArrayTest(inputer(0, 0), iRows + 1, iCols + 1, outputer(0, 0), oRows, oCols)

str = ""

For i = 0 To UBound(outputer, 1)
   For j = 0 To UBound(outputer, 2)
      str = str + "   " + CStr(outputer(i, j))
   Next
   str = str + vbCrLf
Next
MsgBox str, vbOKOnly, "inputer,二维数组的输入"
MsgBox str, vbOKOnly, "outputer,二维数组的输出"
End Sub


文章来源:http://hi.baidu.com/hfutgiser/blog/item/63418858a4f09f87810a187e.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值