I’m trying to use an unmanaged dll in VB.NET. The example source code provided with the dll is in VB6 and below is my attempt to convert it to .NET. When the dll tries to do a callback I get a ‘Attempted to read or write protected memory’ exception. I really don’t care about the callback function getting actually called. My code:
<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)
Original VB6 example 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);
Can you post the original native definiton for AlertManC?
My guess though is that the data parameter of the callback function is actually an Integer vs. a Long. In VB6 I believe Long’s were actually only 32 bits vs. VB.Net where they are 64 bit. Try this
Edit
I updated the code based on the native signature you posted. Can you try this out?