Fingerprint reader device - SDK use in VB.nET - dllimport and PInvokeStackImbalance Problem
- Hi everyone!
I have a fingerprint reader, WISON DR168, it comes with a CD with two .dll and a PDF that says a little about how to use the APIs...
Before all, let me tell you this is the first time i get to program using dlls, and reading a device manual to understand the correct use of this.
I'll try to post all the information in the manual referred to the function i'am trying so far, with no success at all: WIS_Diagnose.
Also, i'm running out of time, because i left this whole dll matter for the last part of this project,.. and the schedule has been ran over...
Also... forgive me for my english... je
I would like to make you people know all the documentation from this fingerprint device, because so, it is easier for you understand this situation...
But i'll copypaste the minimun i think you'll need. Here we go...:
What's next, is the explanation of the protocol, at the first page of the manual:
"the protocol consists of three parts, HEADER, DATA BODY and CHECKSUM. The largest occupied size of the DATA BODY is 560 bytes while the size of the HEADER and CHECKSUM are 14 bytes and 2 bytes respectively, totally 576 bytes for a fixed packet."Next, specifications about the wis_diagnose:
Return code definition
extern int WINAPI Wis_Diagnose (unsigned char *pwd, SYSTEMDATA CurSystemData);
API: Wis_Diagnose
-1 Device test NG
0 Device test OK
There is also a part of the manual that explains the data transmited and received, according to the protocol - the packet; i don't know if this is useful for the programming part. It is in an image... so i can't post it by here.Ok, next is the part of the code where i unsuccesfully try to use this function; i have tried many data types, and with most of them i get the same PinvokeStackImbalanced Exception:
A call to PInvoke function ...... has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.But the following strange situation occurs:
at home, where i don't have the fingerprint device for testing, i get no errors, and the msgbox that shows the result, returns -1.
Now, in the PC where it is connected the device, i get the Pinvokestack... exception, but, after continuing the debugging (F5), the msgbox executes and throws me -1; ALWAYS. I don't get to make it work and show the value 0....
There's a demo program in the CD that executes this wis_diagnose, and t works perfectly, but it's an .exe; so it is discarded to be a dll problem... (I think).
As i said, next, goes the source code. The following declarations are one of the possible combinations i tried; in all of them occurs the situation described.
----------
Imports System.Runtime.InteropServices
Public Class FrmPruebaDispositivo
Structure doordata
Dim DoorOpenSec As UInt16
Dim DoorOverSec As UInt16
Dim AlarmTime As UInt16
<MarshalAs(UnmanagedType.IUnknown)> Dim AlarmEnable 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim Reserved 'As Byte
End Structure
Structure SystemData
<MarshalAs(UnmanagedType.IUnknown)> Dim Version As Object
<MarshalAs(UnmanagedType.IUnknown)> Dim IP 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim NetMask 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim Gateway 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim BaudRate 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim DeviceID 'As Byte
Dim UserNo As UInt16
Dim LogNo As UInt32
<MarshalAs(UnmanagedType.IUnknown)> Dim AuthenMode 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim UartMode 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim VoiceEN 'As Byte
<MarshalAs(UnmanagedType.SafeArray)> Dim ServerIP() As String
<MarshalAs(UnmanagedType.IUnknown)> Dim IDLength 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim Reserved 'As ByteDim DoorData As doordata
<MarshalAs(UnmanagedType.IUnknown)> Dim DeviceName 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim HasDevice 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim ProtocolPswd 'As Byte
<MarshalAs(UnmanagedType.IUnknown)> Dim Reserved1 'As Byte
End Structure<MarshalAs(UnmanagedType.LPStr)> Dim pwd1 As String
<MarshalAs(UnmanagedType.Struct)> Dim systemdata1 As New SystemData
<MarshalAs(UnmanagedType.Struct)> Dim doordata1 As New doordata<DllImport("WisClient.dll", CallingConvention:=CallingConvention.Winapi, _
EntryPoint:="Wis_Diagnose", _
CharSet:=CharSet.Ansi)> _
Public Shared Function Diagnostico( _
<MarshalAs(UnmanagedType.LPStr)> ByVal pwd As String, _
<MarshalAs(UnmanagedType.Struct)> ByVal systemdata As SystemData) _
As Integer
' Leave the body of the function empty.
End FunctionPrivate Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
With doordata1
.AlarmEnable = "0"
.AlarmTime = 30
.DoorOpenSec = 5
.DoorOverSec = 15
End With
With systemdata1
.Version = "N4284A"
.IP = "192.168.010.008"
.NetMask = "255.255.255.000"
.Gateway = "192.168.010.001"
.BaudRate = "115200"
.DeviceID = "000"
.UserNo = 6
.LogNo = 74
.AuthenMode = "2"
.UartMode = "2"
.VoiceEN = "1"
.ServerIP = New String(0) {"192.168.010.004"}
.IDLength = "08"
.Reserved = "".DeviceName = ""
.HasDevice = "3"
.ProtocolPswd = "00000000"
.Reserved1 = ""End With
pwd1 = "00007680"
MsgBox(Diagnostico(pwd1, systemdata1))End Sub
----------
I hope you can help me, i will be really really thankful; any question i'll be answering you in-the-day.- Type modifiéRiquel_DongModérateurmardi 12 août 2008 04:37don't follow up
- Type modifiéRiquel_DongModérateurlundi 18 août 2008 02:02follow up
- Type modifiéRiquel_DongModérateurlundi 18 août 2008 02:06the last post is duplicate. http://forums.msdn.microsoft.com/en-US/clr/thread/1fdf6eef-5896-4872-a8df-f980549dc0d1/
Toutes les réponses
- Hi Emilio,
I don't have this scenario to test. Because this issue mainly relates to P/Invoke, you can have a look at this tool. The GUI version of the tool operates in 3 modes. It should give you the much help about P/Invoke call.- SigImp Search: Search for a commonly used function and translate it into managed code.
- SigImp Translate Snippet: Directly translate C code into managed PInvoke signatures.
- SigExp: Convert managed binaries into C++ Reverse PInvoke scenarios
Best regards,
Riquel
Please remember to mark the replies as answers if they help and unmark them if they provide no help. - Thanks very mucn for your help! i'll be trying what you suggest me in this week and let you know about my results. I'm pretty sure i'll need help again, so... this thread isn't over!! jejeje.
Thanks again.! - Yes, i tried it and no error messages so far!!
thanks very much!!!
but now i have another problem...:
this C data type:
unsigned char IP[4]; //IP address
, and its equivalence in VB.NET is:
'''unsigned char[4]<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=4)> _
Public IP As String
How do i represent an IP address in a string data type?
Thanks very much in advance

