Callback function from unmanaged dll in VB.NET

I am trying to use unmanaged dll in VB.NET. An example of the source code provided with the dll is in VB6, and below is my attempt to convert it to .NET. When the DLL tries to call back, I get the exception "Attempted to read or write protected memory." I really don't care what the callback function is really called. My code is:

<DllImport("AlertMan.dll")> _
Public Shared Function AlertManC( _
    ByVal CallbackAddr As AlertManCallbackDel) As Long
End Function

Public Delegate Sub AlertManCallbackDel(ByVal data As Long)

Public Sub AlertManCallback(ByVal data As Long)       
End Sub

Public mydel As New AlertManCallbackDel(AddressOf AlertManCallback)
'protected memeory exception here
Dim IStat as Long = AlertManC(mydel) 

Sample VB6 source code:

Declare Function AlertManC _
    Lib "AlertMan.dll" _
    Alias "AlertManC" (ByVal CallbackAddr As Long) As Long

Private Sub AlertManCallback(ByVal data As Long)
End Sub

' calling code
Dim IStat As Long
IStat = AlertManC(AddressOf AlertManCallBack)

Original dll header

typedef void TACBFUNC(char *);
int AlertManC(TACBFUNC *WriteCaller cHANDLEPARM);
0
source share
3 answers

Can you publish the original native qualifier for AlertManC?

, , , . VB6 , Long 32 VB.Net, 64 .

   <DllImport("AlertMan.dll")> _
   Public Shared Function AlertManC(ByVal CallbackAddr As AlertManCallbackDel) As Long
   End Function

   Public Delegate Sub AlertManCallbackDel(ByVal data As IntPtr)


   Public Sub AlertManCallback(ByVal data As IntPtr)       
   End Sub

Edit

, , . ?

+2

:

Public Delegate Sub AlertManCallbackDel(ByRef data As Byte)

, .

, :

<DllImport("AlertMan.dll")> _
Public Shared Function AlertManC( _
    ByVal CallbackAddr As AlertManCallbackDel) As Integer
End Function

, - Integer, VB.NET 32- . VB6 Long 32- , , VB.NET.

, .

0

cdecl, # VB.NET.

You will need to change the delegate IL in order to behave correctly.

You can search CodeProject for an in-depth article.

Update:

I think that is not the right answer :) But it will leave my answer.

0
source

All Articles