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

时间:2011-10-06 13:55:23

标签: c++ vb.net dll access-violation

我正在尝试从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错误< / p>

 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

3 个答案:

答案 0 :(得分:1)

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 args更难猜测。

答案 1 :(得分:1)

p-invoke是平台调用,是您使用.NET调用本机API的方式。您的声明当前未设置为传递数组,并且不应该完成ByRef。

尝试将ByRef更改为ByVal以获取数组变量,并使用()声明它们以表示数组。

答案 2 :(得分:0)

错误是我在将变量声明为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