Resolved Need help with adapting C DLL function prototype to VB.Net

Jarek

New member
Joined
Nov 18, 2023
Messages
2
Programming Experience
3-5
Hi all,
I am creating the vb.net application based on C described DLL. I am trying to solve the problem with declaration and using callback function which causing AccessViolationException:

The original callback function prototype:

typedef void(* NOTIFY_FUNC) (uint32_t MsgId, uint32_t wParam, uint32_t lParam, int32_t clientHandle)

My VB.NET callback function prototype:

Delegate Sub NOTIFY_FUNC(ByVal MsgId As UInt32, ByVal wParam As UInt32, ByVal lParam As UInt32, ByVal clientHandle As IntPtr)

Original function including callback registration:

int32_t FwUpdate (const char * lpszFileName, NOTIFY_FUNC callback, int32_t clientHandle)

Parameters:
lpszFileName: Pointer to a file name to update
callback: Pointer to a callback function of the type NOTIFY_FUNC that might handle notify messages send by the DLL during update
clientHandle: Handle of the calling application. Can be set to FALSE in case the callback mechanism is not applied and parameter callback is set to NULL

My VB.net declaration:

Public Function FwUpdate(ByVal lpszFileName As String, ByVal callback As NOTIFY_FUNC, ByVal clientHandle As IntPtr) As Int32

When I call DLL function with callback disabled "FwUpdate("Filename", Nothing, Nothing)" all works fine but when I try to use callback function (see example below), every time it fails into AccessViolationException (attempt to read or write to protected memory area).

My code example:
Public Sub clbk(ByVal MsgId As UInt32, ByVal wParam As UInt32, ByVal lParam As UInt32, ByVal clientHandle As IntPtr)
'Some code in callback function
End Sub

Private Function StartFWUpdate()
Dim Ret As UInt32 = FwUpdate("Filename", AddressOf clbk, Me.Handle)
End Sub

Thanks a lot for any help to convert C DLL description to working VB.net code
 
I would try adding a calling convention to delegate definition, Cdecl or StdCall
VB.NET:
<UnmanagedFunctionPointer(CallingConvention.Cdecl)>
CallingConvention should probably also be set for DllImport for function.
 
I would try adding a calling convention to delegate definition, Cdecl or StdCall
VB.NET:
<UnmanagedFunctionPointer(CallingConvention.Cdecl)>
CallingConvention should probably also be set for DllImport for function.

Thanks a lot JohnyH! Cdecl on callback delegate is working and I am a bit more experienced again.
 
Back
Top