未处理的 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 中调用它的方式:
Call 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 错误
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
我的问题是 - 如果我没有超出矩阵的边界,还有什么其他原因会引发此错误?是否因为我只传递了矩阵 ByReference 中的第一个条目,然后它试图访问该矩阵的其他元素?如果是这样,我该如何解决这个问题?
编辑:
这是我在 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该声明完全是错误的,这类似于 vb6 声明。 C 代码中的 int 是 vb.net 中的 Integer,而不是 Long。 Xdata 和 Ydata 很可能是数组,而不是 byref double。将它们声明为 ByVal Double()。其他 byref 参数更难猜测。
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.
p-invoke 是平台调用,是使用 .NET 调用本机 API 的方式。您的声明当前未设置为传递数组,并且不应该通过 ByRef 完成。
尝试将数组变量的 ByRef 更改为 ByVal 并使用 () 声明它们以表示数组。
p-invoke is platform invoke, and is how you call into native APIs with .NET. Your declaration is not currently setup to pass arrays, and it should not be done ByRef.
Try changing the ByRef to ByVal for your array variables and declare them with the () to signify an array.
错误在于,虽然我将变量声明为
long
类型,但它应该是integer
类型。变量rc
是错误声明的:这里是我的表单
Form1
的代码(它有一个 Button 和一个 TextBox):这里是
Button.Click 的代码
(它初始化一些用于调用 dll 中函数的变量):这是我声明为 long 的变量 rc。它应该是整数
myFunction
在 myDll 中声明为整数:这是我在 Visual Basic Express 2010 中声明
MyFunction
的方式:The error is that while I was declaring a variable as type
long
but it should be of typeinteger
. The variaberc
was the one wrongly declared:Here the code of my form
Form1
(it has one Button and one TextBox):Here the code for
Button.Click
(it initialize some variables used to call a function in a dll):Here is the variable rc that I declared as long. It should be integer
myFunction
was declared as integer in myDll:This is how I declared
MyFunction
in my Visual Basic Express 2010: