加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-17 00:31:34 所属栏目:大数据 来源:网络整理
导读:以前用到的神经网络都是在matlab中调的,而项目需要,要写个神经网络的算法,系统主界面是VB写的,在VB中做网络训练速度是不照的,用VB,VC 的混合编程,但是一直被VB与Dll之间的数组传递这个难题所困扰,在网上搜了也找不到答案,据说safearray可以解决,但

以前用到的神经网络都是在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* ioutArrCount) //一维数组的传入传出 { 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,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) MsgBox CStr(m),vbOKOnly,"一维数组的元素个数" 'MsgBox CStr(res) Dim str As String Dim i As Integer For i = 0 To UBound(out) str = str + " " + CStr(out(i)) Next MsgBox str,"一维数组的元素" 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,2) str = str + " " + CStr(inputer(i,j)) Next str = str + vbCrLf Next MsgBox str,"inputer,二维数组的输入" Call TwoDimensionArrayTest(inputer(0,0),iRows + 1,iCols + 1,outputer(0,oRows,oCols) str = "" ' For i = 0 To UBound(outputer,1) For j = 0 To UBound(outputer,2) str = str + " " + CStr(outputer(i,"outputer,二维数组的输出" End Sub 笨方法,很简单,但是真的可以用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读