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

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

概述VB VC 混合编程(dll)中 数组传入传出        以前用到的神经网络都是在matlab中调的,而项目需要,要写个神经网络的算法,系统主界面是VB写的,在VB中做网络训练速度是不照的,用VB,VC的混合编程,但是一直被VB与Dll之间的数组传递这个难题所困扰,在网上搜了也找不到答案,据说safearray可以解决,但是看了下比较麻烦,在CSDN社区中也没有找到答案,但是社区中一个朋友 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* 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 笨方法,很简单,但是真的可以用 总结

以上是内存溢出为你收集整理的VB VC 混合编程(dll)中 数组 的传入传出全部内容,希望文章能够帮你解决VB VC 混合编程(dll)中 数组 的传入传出所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1282496.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存