开发者

Unhandled AccessViolationException Error calling a DLL into VB.Net

开发者 https://www.devze.com 2023-04-11 09:29 出处:网络
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.

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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号