1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61:
| [DllImport("CTRSCT32.DLL", CharSet = CharSet.Auto, SetLastError = true)] public static extern sbyte CT_data(ushort ctn, ref byte dad, ref byte sad, ushort lenc, byte[] command, ref ushort lenr, IntPtr response);
[DllImport("CTRSCT32.DLL", CharSet = CharSet.Auto, SetLastError = true)] public static extern sbyte CT_close(ushort ctn);
[DllImport("CTRSCT32.DLL", CharSet = CharSet.Auto, SetLastError = true)] public static extern sbyte CT_init(ushort TerminalNumber, ushort COMPort);
private byte[] ExecCommand(byte[] command, ushort CardTerminalNumber, ushort COMPort, ushort MaxResponseLength, bool ToHost, bool ToReader) {
sbyte initret = CT_init(CardTerminalNumber, COMPort); if (initret != 0) throw new Exception("Fehler beim Iniziailisieren. ErrorCode = " + initret);
ushort responseLength = MaxResponseLength; IntPtr responseBuffer = Marshal.AllocHGlobal(responseLength);
byte SourceAddress = (ToHost ? (byte)2 : (byte)5); byte DestinationAddress = (ToReader ? (byte)1 : (byte)0);
if (command.Length > ushort.MaxValue) throw new ArgumentOutOfRangeException("Der Command ist zu lang!");
sbyte Commandret = CT_data(CardTerminalNumber, ref DestinationAddress, ref SourceAddress, (ushort)command.Length, command, ref responseLength, responseBuffer); sbyte Closeret = CT_close(CardTerminalNumber);
if (Commandret != 0) throw new Exception("Fehler beim ausführen des Commands. ErrorCode = " + Commandret );
if(Closeret != 0) throw new Exception("Fehler beim schließen der Verbindung. ErrorCode = " + Closeret ); byte[] response = new byte[responseLength]; Marshal.Copy(responseBuffer, response, 0, responseLength); Marshal.FreeHGlobal(responseBuffer); return response; } |