未处理的AccessViolationException错误调用DLL到VB.Net [英] Unhandled AccessViolationException Error calling a DLL into VB.Net

查看:484
本文介绍了未处理的AccessViolationException错误调用DLL到VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从VB.net调用一个C ++编译的DLL,我遇到了一些问题。似乎有一个明显的解决方案,但我无法弄清楚。



这是C ++中的函数声明:

  MyFunction(int trailingaveragesize,double sigmasize,int myflag,int sizeSeries,double * Xdata,double * Ydata,int sizeinputparameter,int * averagePairs,double * PositionsSize,double * PnLSize) 

这是我如何在VB.Net中调用它:

 调用MyFunction(200,1,1,230,a_PriceSeries(0),a_PriceSeries(0),1,a_Averages(0),a_PositionSeries(0),a_PnLs (0))

输入矩阵的最大大小由sizeSeries(230)定义,我的所有输入矩阵的大小都是10000(只是这样我不会意外溢出),但仍然有一个未处理的AccessViolationException错误

 尝试读取或写入受保护的内存。这通常表明其他内存已损坏。 

我的问题是 - 如果我不超过我的矩阵的边界,还会有什么其他原因这个错误?是因为我只传递了我的矩阵中的第一个条目ByReference然后它试图访问该矩阵的其他元素?如果是这样,我该如何解决?



编辑:



这是我如何在VB中声明

 声明函数MyFunction LibC:\Dev\asdf.dll(ByVal trailingaveragesize As Long,ByVal sigmasize As Double ,ByVal myflag As Long,ByVal sizeSeries As Long,ByRef Xdata As Double,ByRef Ydata As Double,ByVal sizeinputparameter As Long,ByRef averagePairs As Long,ByRef PositionsSize As Double,ByRef PnLSize As Double)As Double 


解决方案

 声明函数MyFunction LibC:\ DevEndDll(ByVal trailingaveragesize As Long,_ 
ByVal sigmasize As Double,ByVal myflag As Long,ByVal sizeSeries As Long,
ByRef Xdata As Double,ByRef Ydata As Double,ByVal sizeinputparameter As Long,
ByRef averagePairs As Long,ByRef PositionsSize As Double,ByRef PnLSize As Double)As Double



<这个声明是错误的类似于vb6声明。 C代码中的 int 是vb.net中的整数,而不是Long。 Xdata和Ydata很有可能是数组,而不是一个byref。声明为ByVal Double()。另一个由refref args更难猜到。


I'm trying to call a C++-compiled DLL from VB.net and I'm running into some problems. Seems like there's an obvious solution, but I can't figure it out.

Here is the function declaration in C++:

 MyFunction(int trailingaveragesize, double sigmasize, int myflag, int sizeSeries, double *Xdata, double *Ydata, int sizeinputparameter, int *averagePairs, double *PositionsSize, double *PnLSize)

Here is how I'm calling it in VB.Net:

 Call MyFunction(200, 1, 1, 230, a_PriceSeries(0), a_PriceSeries(0), 1, a_Averages(0), a_PositionSeries(0), a_PnLs(0))

The maximum size of the input matrices are defined by sizeSeries (230), and the size of all my input matrices are 10000 (just so I won't accidentally overflow), yet still i'm getting an unhandled AccessViolationException error

 Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

My question is - If I'm not exceeding the bounds of my matrices, what other reasons would throw this error? Is it because I'm only passing the first entry in my matrices ByReference then it's trying to access other elements of that matrix? If so, how would I fix that?

EDIT:

Here's how I'm declaring it in VB

 Declare Function MyFunction Lib "C:\Dev\asdf.dll" (ByVal trailingaveragesize As Long, ByVal sigmasize As Double, ByVal myflag As Long, ByVal sizeSeries As Long, ByRef Xdata As Double, ByRef Ydata As Double, ByVal sizeinputparameter As Long, ByRef averagePairs As Long, ByRef PositionsSize As Double, ByRef PnLSize As Double) As Double

解决方案

Declare Function MyFunction Lib "C:\Dev\asdf.dll" (ByVal trailingaveragesize As Long, _
  ByVal sigmasize As Double, ByVal myflag As Long, ByVal sizeSeries As Long, 
  ByRef Xdata As Double, ByRef Ydata As Double, ByVal sizeinputparameter As Long, 
  ByRef averagePairs As Long, ByRef PositionsSize As Double, ByRef PnLSize As Double) As Double

The declaration is simply wrong, this resembles a vb6 declaration. An int in C code is an Integer in vb.net, not a Long. The Xdata and Ydata are highly likely to be arrays, not a byref double. Declare them as ByVal Double(). The other byref args are harder to guess.

这篇关于未处理的AccessViolationException错误调用DLL到VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆