i wrote this post for asking you to help me. My requiremente is to read and write to a smart card (sle4428) using a scard32.dll in c# context. The reader is SCR 3310 of SCM Microsystem
First of all I have a vb.net code which run correctlty and i'm able to read and write data. The problem is whern i try to do the same thing using c#. My first attemps was to translate the native call "SCardComand" in c# but fail. The second attemps was to build a dll from the VB.net code and use it in a c# context but still fail.
And if im writing this post is because i ve no more ideas.
follow i 'm provide to you the native call in VB.net
<DllImport("SCARD32", EntryPoint:="SCardComand", SetLastError:=True, CallingConvention:=CallingConvention.Winapi, CharSet:=CharSet.Ansi, ExactSpelling:=True)> _
Public Shared Function SCardComand(ByRef handle As Integer,
<MarshalAs(UnmanagedType.VBByRefStr)> ByRef cmd As String,
ByRef cmdLen As Integer,
<MarshalAs(UnmanagedType.VBByRefStr)> ByRef dataIn As String,
ByRef dataInLen As Integer,
<MarshalAs(UnmanagedType.VBByRefStr)> ByRef dataOut As String,
ByRef dataOutLen As Integer) As Integer
End Function
and here my translation to c#.
[DllImport("SCARD32.DLL")]
static extern UInt32 SCardComand(IntPtr handle,
[MarshalAs(UnmanagedType.LPTStr)] ref String cmd,
IntPtr cmdLen,
[MarshalAs(UnmanagedType.LPTStr)] ref String dataIn,
IntPtr dataInLen,
[MarshalAs(UnmanagedType.L开发者_如何学PythonPTStr)] out String dataOut,
IntPtr dataOutLen);
And for istance if i run this command in c# int Ret = SCardComand(0, "Card,MemVerifyPin,FFFFF", 0,"",0, "", 0);
Obtain 16384 which means nothing for me.
Please if someone have an idea of how proceed...
The first thing I notice is your C# is wrong
[DllImport("SCARD32.DLL")] static extern UInt32 SCardComand(IntPtr handle, [MarshalAs(UnmanagedType.LPTStr)] ref String cmd, IntPtr cmdLen, [MarshalAs(UnmanagedType.LPTStr)] ref String dataIn, IntPtr dataInLen, [MarshalAs(UnmanagedType.LPTStr)] out String dataOut, IntPtr dataOutLen);
should be:
[DllImport("SCARD32.DLL")] static extern UInt32 SCardComand(int handle, [MarshalAs(UnmanagedType.LPTStr)] ref String cmd, IntPtr cmdLen, [MarshalAs(UnmanagedType.LPTStr)] ref String dataIn, int dataInLen, [MarshalAs(UnmanagedType.LPTStr)] out String dataOut, int dataOutLen);
The reason for this is that the working VB.net code does not use IntPtr for handle, dataInLen and dataOutLen and obviously IntPtr != integer. The reason you are getting 16384 is because you would have to Marshall a IntPtr to an integer to actually get the correct answer.
If my suggestion does not work then post the documentation for this command, one would need to know how the prototype is declared within the C/C++ code to suggest an alternative more then likely.
精彩评论