Passing callback pointer from VB .NET to C dll

D

dfrench

I'm trying to call an InitS function in a C dll from VB.NET.
The InitS function requires pointers to 2 callback functions (I
think).
I'm getting error when calling the function: Attempted to read or
write protected memory. This is often an indication that other memory
is corrupt.
Anyone have any ideas what is going wrong, and pointers in right
direction would be appreciated.

Many thanks
Dermot

C code
typedef void (/*CALLBACK*/ CALLCONV *ERRORFCT)(const DWORD,const
DWORD);
PORTEE_DLLCV_LB int CALLCONV InitS(FARPROC fpFinsihed,FARPROC
fpError);

My VB.NET attempt

I'm trying to use delegates to get this working:

Public Class Class2
Public Delegate Sub ScanFinishedDelegate(ByVal long1 As Long,
ByVal long2 As Long)
Public Delegate Sub ScannerErrorDelegate(ByVal long1 As Long,
ByVal long2 As Long)
Private Declare Function ScInit Lib "C:\vbcode\InitS.dll" (ByVal
test As ScanFinishedDelegate, ByVal test2 As ScannerErrorDelegate) As
Integer

Sub InitialiseScanner()
Try
Dim delScanFinished As ScanFinishedDelegate = AddressOf
ScanFinishedCallback
Dim delError As ScannerErrorDelegate = AddressOf
ScannerErrorCallback
Class2.ScInit(delScanFinished, delError)
' Error I'm Getting after previous line:
'Attempted to read or write protected memory. This is
often an indication that other memory is corrupt.
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try

End Sub

Private Shared Sub ScanFinishedCallback(ByVal long1 As Long, ByVal
long2 As Long)
' Must return in timely fashion no msgbox's
'MsgBox(string1)
'MsgBox(string2)
Debug.WriteLine(long1)
End Sub


Private Shared Sub ScannerErrorCallback(ByVal long1 As Long, ByVal
long2 As Long)
' Must return in timely fashion no msgbox's
'MsgBox(string1)
'MsgBox(string2)
Debug.WriteLine(long1)
End Sub

End Class
 
M

Mattias Sjögren

Public Class Class2
Public Delegate Sub ScanFinishedDelegate(ByVal long1 As Long,
ByVal long2 As Long)
Public Delegate Sub ScannerErrorDelegate(ByVal long1 As Long,
ByVal long2 As Long)

Try changing the parameter types to Integer or UInteger.


Mattias
 
M

Mattias Sjögren

Tried changing parameter types, same error message.
Any other ideas?

Do you know what CALLCONV resolves to in the C code? Maybe you have a
calling convention mismatch.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top