VB、VC 混合编程(dll)中数组的传入传出.doc_第1页
VB、VC 混合编程(dll)中数组的传入传出.doc_第2页
VB、VC 混合编程(dll)中数组的传入传出.doc_第3页
VB、VC 混合编程(dll)中数组的传入传出.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

VB、VC 混合编程(dll)中数组的传入传出2008年10月23日 星期四 21:39以前用到的神经网络都是在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 doubleiNum; int i; for(i=0;inCount;i+) dResi=inArri*2; for(i=0;inCount;i+) outArri=dResi; *ioutArrCount=iNum; return dRes0; delete dRes;void _stdcall TwoDimensionArrayTest(double *inArr,int nRows,int nCols,double *outArr,int* outRows,int *outCols)/二维数组的传入传出 double *dRes=new doublenRows*nCols; int i; int j; for(i=0;inRows;i+) for(j=0;jnCols;j+) dResnCols*i+j=inArrnCols*i+j*2; for(i=0;inRows;i+) for(j=0;jnCols;j+) outArrnCols*i+j=inArrnCols*i+j*2; *outRows=nRows; *outCols=nCols; delete dRes;LIBRARY TestDllEXPORTS ; 此处可以是显式导出 Add 1 darray 2 OneDimensionArrayTest 3TwoDimensionArrayTest 4VB中Declare Function OneDimensionArrayTest Lib D:在编程序DllVBLoadDllTestDll.dll (ByRef inputer As Double, ByVal inLength As Long, ByRef output As Double, ByRef outLength As Long) As DoubleDeclare Function TwoDimensionArrayTest Lib D:在编程序DllVBLoadDllTestDll.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 DoubleDim out(9) As DoubleDim res As DoubleDim m As Longinputer(0) = 1.2inputer(1) = 2.3inputer(2) = 1res = OneDimensionArrayTest(inputer(0), 9, out(0), m)MsgBox CStr(m), vbOKOnly, 一维数组的元素个数MsgBox CStr(res)Dim str As StringDim i As IntegerFor i = 0 To UBound(out) str = str + + CStr(out(i)NextMsgBox str, vbOKOnly, 一维数组的元素End SubPrivate Sub cmdTest3_Click()Dim iRows As IntegerDim iCols As IntegeriRows = 3iCols = 4Dim inputer() As DoubleReDim inputer(iRows, iCols)Dim outputer() As DoubleReDim outputer(iRows, iCols)Dim oRows As LongDim oCols As LongDim i, j As IntegerFor i = 0 To UBound(inputer, 1) For j = 0 To UBound(inputer, 2) inputer(i, j) = (i + 1) * (j + 1) NextNextDim str As StringFor i = 0 To UBound(inputer, 1) For j = 0 To UBound(inputer, 2) str = str + + CStr(inputer(i, j) Next str = str + vbCrLfNextMsgBox str, vbOKOnly, inputer,二维数组的输入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 UBo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论