未处理的 AccessViolationException 将 DLL 调用到 VB.Net 时出错

发布于 2024-12-08 05:37:14 字数 1231 浏览 4 评论 0原文

我试图从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恬淡成诗 2024-12-15 05:37:14
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

该声明完全是错误的,这类似于 vb6 声明。 C 代码中的 int 是 vb.net 中的 Integer,而不是 Long。 Xdata 和 Ydata 很可能是数组,而不是 byref double。将它们声明为 ByVal Double()。其他 byref 参数更难猜测。

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.

内心激荡 2024-12-15 05:37:14

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.

陌上芳菲 2024-12-15 05:37:14

错误在于,虽然我将变量声明为 long 类型,但它应该是 integer 类型。变量 rc 是错误声明的:

这里是我的表单 Form1 的代码(它有一个 Button 和一个 TextBox):

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

这里是 Button.Click 的代码 (它初始化一些用于调用 dll 中函数的变量):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim arg1 As String
    Dim arg2 As String
    Dim ret As String

这是我声明为 long 的变量 rc。它应该是整数

    Dim rc As long
        ret = "123456789012345678901234567890123"
        arg1 = "123456"
        arg2 = "1234567890123456"
        rc = myFunction(arg1, arg2, ret)
        If (rc = 0) Then
            TextBox1.Text = ret
        Else
            TextBox1.Text = "Result not OK"
        End If
    End Sub
End Class

myFunction 在 myDll 中声明为整数:

extern __declspec(dllexport) int __stdcall myFunction(unsigned char * pszArg1, unsigned char * pszArg2, char * pszReturn)

这是我在 Visual Basic Express 2010 中声明 MyFunction 的方式:

Module Module1

    Public Declare Function myFunction Lib "C:\Users\me\MyProjects\myDll\bin\Debug\myDll.dll" (ByVal Arg1 As String, ByVal Arg2 As String, ByVal ret As String) As Integer

End Module

The error is that while I was declaring a variable as type long but it should be of type integer. The variabe rc was the one wrongly declared:

Here the code of my form Form1 (it has one Button and one TextBox):

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

Here the code for Button.Click (it initialize some variables used to call a function in a dll):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim arg1 As String
    Dim arg2 As String
    Dim ret As String

Here is the variable rc that I declared as long. It should be integer

    Dim rc As long
        ret = "123456789012345678901234567890123"
        arg1 = "123456"
        arg2 = "1234567890123456"
        rc = myFunction(arg1, arg2, ret)
        If (rc = 0) Then
            TextBox1.Text = ret
        Else
            TextBox1.Text = "Result not OK"
        End If
    End Sub
End Class

myFunction was declared as integer in myDll:

extern __declspec(dllexport) int __stdcall myFunction(unsigned char * pszArg1, unsigned char * pszArg2, char * pszReturn)

This is how I declared MyFunction in my Visual Basic Express 2010:

Module Module1

    Public Declare Function myFunction Lib "C:\Users\me\MyProjects\myDll\bin\Debug\myDll.dll" (ByVal Arg1 As String, ByVal Arg2 As String, ByVal ret As String) As Integer

End Module
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文