David MacDonald
asked on
C++ DLL callback with VB.Net
Hi there,
i have a unmanaged DLL that I use to control a Biometric reader with my app. I'm coding with VB.net. I used a vb6 example to interface with the DLL. Here is the original code to link with the dll function :
Public Declare Function AET60_SetCallback Lib "Librairie externe\AET60.dll" ( _
ByVal CallbackFunction As Long) As Long
Here is the new one in vb.net :
<DllImport("Librairie externe\AET60.dll", EntryPoint:="AET60_SetCall back", _
SetLastError:=True, CallingConvention:=Calling Convention .Cdecl)> _
Public Shared Function AET60_SetCallback(ByVal CallbackFunction As MyCallBack) As Integer
End Function
Here is my delegate :
<UnmanagedFunctionPointer( CallingCon vention.Cd ecl)> _
Public Delegate Function MyCallBack(ByRef Param As Integer, _
ByVal GuiState As Integer, _
ByVal Response As Byte, _
ByVal Msg As Integer, _
ByVal Progress As Byte, _
ByRef SampleBuffer As FBITMAP) As Integer
And my callback function :
Private Function CALLBACK_FUNCTION(ByRef Param As Integer, _
ByVal GuiState As Integer, _
ByVal Response As Byte, _
ByVal Msg As Integer, _
ByVal Progress As Byte, _
ByRef SampleBuffer As FBITMAP) As Integer
Dim StatusMsg As String
StatusMsg = New String(vbNullChar, 255)
If (GuiState And AET60_MESSAGE_PROVIDED) <> 0 Then
Call AET60_GetMessage(Msg, StatusMsg)
End If
CALLBACK_FUNCTION = 0
End Function
The data structure concerned by the declaration :
'========================= ========== ========== ========== ========== =====
' data structure definition
'========================= ========== ========== ========== ========== =====
Public Structure FDATA
Dim Length As Integer
Dim Data() As Byte
End Structure
Public Structure FBITMAP
Dim Width As Integer
Dim Height As Integer
Dim Bitmap As FDATA
End Structure
And the line where I plug everything :
del = New MyCallBack(AddressOf CALLBACK_FUNCTION)
retcode = AET60_SetCallback(del)
Everything runs smoothly until I call un dll function that should trigger the callback ... i then receive a "Unhandled exception of type 'System.ArgumentNullExcept ion' from a unknown module.
If i skip the callback regiostration, everything runs smoothly but i can't have sny status update on the status of my thuimbprint reader ...
Can anyone give me a hand ?
i have a unmanaged DLL that I use to control a Biometric reader with my app. I'm coding with VB.net. I used a vb6 example to interface with the DLL. Here is the original code to link with the dll function :
Public Declare Function AET60_SetCallback Lib "Librairie externe\AET60.dll" ( _
ByVal CallbackFunction As Long) As Long
Here is the new one in vb.net :
<DllImport("Librairie externe\AET60.dll", EntryPoint:="AET60_SetCall
SetLastError:=True, CallingConvention:=Calling
Public Shared Function AET60_SetCallback(ByVal CallbackFunction As MyCallBack) As Integer
End Function
Here is my delegate :
<UnmanagedFunctionPointer(
Public Delegate Function MyCallBack(ByRef Param As Integer, _
ByVal GuiState As Integer, _
ByVal Response As Byte, _
ByVal Msg As Integer, _
ByVal Progress As Byte, _
ByRef SampleBuffer As FBITMAP) As Integer
And my callback function :
Private Function CALLBACK_FUNCTION(ByRef Param As Integer, _
ByVal GuiState As Integer, _
ByVal Response As Byte, _
ByVal Msg As Integer, _
ByVal Progress As Byte, _
ByRef SampleBuffer As FBITMAP) As Integer
Dim StatusMsg As String
StatusMsg = New String(vbNullChar, 255)
If (GuiState And AET60_MESSAGE_PROVIDED) <> 0 Then
Call AET60_GetMessage(Msg, StatusMsg)
End If
CALLBACK_FUNCTION = 0
End Function
The data structure concerned by the declaration :
'=========================
' data structure definition
'=========================
Public Structure FDATA
Dim Length As Integer
Dim Data() As Byte
End Structure
Public Structure FBITMAP
Dim Width As Integer
Dim Height As Integer
Dim Bitmap As FDATA
End Structure
And the line where I plug everything :
del = New MyCallBack(AddressOf CALLBACK_FUNCTION)
retcode = AET60_SetCallback(del)
Everything runs smoothly until I call un dll function that should trigger the callback ... i then receive a "Unhandled exception of type 'System.ArgumentNullExcept
If i skip the callback regiostration, everything runs smoothly but i can't have sny status update on the status of my thuimbprint reader ...
Can anyone give me a hand ?
ASKER
Hi, i just tried it ... it tells me that the type MyCalllBack cannot be converted ton SYstem.InPtr ....
Darn ... and to say i knew nothing of delegates last week :)
With "AddressOf del" as param, it tells me that AddressOf needs a method name instead .
Darn ... and to say i knew nothing of delegates last week :)
With "AddressOf del" as param, it tells me that AddressOf needs a method name instead .
Hi there.
Nope, I just checked and I was wrong. Change it back to this:
<DllImport("Librairie externe\AET60.dll", EntryPoint:="AET60_SetCall back", _
SetLastError:=True, CallingConvention:=Calling Convention .Cdecl)> _
Public Shared Function AET60_SetCallback(ByVal CallbackFunction As MyCallBack) As Integer
End Function
Try this instead:
retcode = AET60_SetCallback(AddressO f CALLBACK_FUNCTION)
You don't need to do 'del = New....', simply pass the address of the CALLBACK_FUNCTION you already have.
Try that.
Cheers.
Jas.
Nope, I just checked and I was wrong. Change it back to this:
<DllImport("Librairie externe\AET60.dll", EntryPoint:="AET60_SetCall
SetLastError:=True, CallingConvention:=Calling
Public Shared Function AET60_SetCallback(ByVal CallbackFunction As MyCallBack) As Integer
End Function
Try this instead:
retcode = AET60_SetCallback(AddressO
You don't need to do 'del = New....', simply pass the address of the CALLBACK_FUNCTION you already have.
Try that.
Cheers.
Jas.
ASKER
Hi Jas,
still with the null exception ... just for pointers, i tried changing byval for byref on the MyCallBack param ... i get this error :
An unhandled exception of type 'System.AccessViolationExc eption' occurred in AbonnementBiometrie.exe
So i guess Byval is alright ... But i really don't understand why it's not working ...
still with the null exception ... just for pointers, i tried changing byval for byref on the MyCallBack param ... i get this error :
An unhandled exception of type 'System.AccessViolationExc
So i guess Byval is alright ... But i really don't understand why it's not working ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi there,
it seems the problem lies in the structure definition used in the callback, the FBITMAP structure uses the FDATA strucuture wich in turn contains a Byte array ... Is it because my byte array is not initialise by default ... it sure seems like it ...
it seems the problem lies in the structure definition used in the callback, the FBITMAP structure uses the FDATA strucuture wich in turn contains a Byte array ... Is it because my byte array is not initialise by default ... it sure seems like it ...
Public Structure FDATA
Dim Length As Integer
Dim Data() As Byte
End Structure
Public Structure FBITMAP
Dim Width As Integer
Dim Height As Integer
Dim Bitmap As FDATA
End Structure
ASKER
Solved it !
it was the FDATA Byte array that was not initialized that popped the nullException ... Jas, i'm giving you the points anyway because you gave me hope ;-)
it was the FDATA Byte array that was not initialized that popped the nullException ... Jas, i'm giving you the points anyway because you gave me hope ;-)
Public Structure FDATA
Dim Length As Integer
Shared Data(0 To 256) As Byte
End Structure
This might be a long shot, but maybe you should try this:
<DllImport("Librairie externe\AET60.dll", EntryPoint:="AET60_SetCall
SetLastError:=True, CallingConvention:=Calling
Public Shared Function AET60_SetCallback(ByVal CallbackFunction As System.IntPtr) As Integer
End Function
Use the IntPtr in the declaration. Then use the function like so:
del = New MyCallBack(AddressOf CALLBACK_FUNCTION)
retcode = AET60_SetCallback(del) or retcode = AET60_SetCallback(AddressO
If this doesn't work, try using ByRef instead of ByVal in the declaration of AET60_SetCallback.
Hope this helps.
Jas.