internal static byte[] SendCmd(IntPtr card, byte[] sendBytes) { byte[] receivedUID = new byte[255]; API.SCARD_IO_REQUEST request = new API.SCARD_IO_REQUEST(); request.dwProtocol = API.SCARD_PCI_T1; request.cbPciLength = Marshal.SizeOf(typeof(API.SCARD_IO_REQUEST)); int outBytes = receivedUID.Length; API.SCardFunctionReturnCodes status = (API.SCardFunctionReturnCodes)API.SCardTransmit(card, ref request, ref sendBytes[0], sendBytes.Length, ref request, ref receivedUID[0], ref outBytes); if (status == API.SCardFunctionReturnCodes.SCARD_S_SUCCESS) { return(receivedUID.Take(outBytes).ToArray()); } return(null); }
static bool SendDnieCmd(IntPtr card, byte[] cmd, byte[] buffer, ref int l) { API.SCARD_IO_REQUEST request = new API.SCARD_IO_REQUEST(); request.dwProtocol = API.SCARD_PCI_T0; request.cbPciLength = Marshal.SizeOf(typeof(API.SCARD_IO_REQUEST)); API.SCardFunctionReturnCodes status = (API.SCardFunctionReturnCodes)API.SCardTransmit(card, ref request, ref cmd[0], cmd.Length, ref request, ref buffer[0], ref l); if (status == API.SCardFunctionReturnCodes.SCARD_S_SUCCESS) { if (l == 2) { bool result = (buffer)[0] == 0x61 || ((buffer)[0] == 0x90 && (buffer)[1] == 0x00); return(result); } return(true); } else { return(false); } }
static byte[] GetAtr(IntPtr card, string nameReader, uint pw) { int dwAtrLen = 0; byte[] bAtr = null; IntPtr theIntPtr = Marshal.StringToHGlobalUni(nameReader); int rl = nameReader.Length + 2; // Dos \0 al final mas int st = 0; API.SCardFunctionReturnCodes status = (API.SCardFunctionReturnCodes)API.SCardStatus(card, theIntPtr, ref rl, ref st, ref pw, bAtr, ref dwAtrLen); // Se repite y ya va por que devuelve la primera vez el tamaño if (status == API.SCardFunctionReturnCodes.SCARD_E_INSUFFICIENT_BUFFER) { status = (API.SCardFunctionReturnCodes)API.SCardStatus(card, theIntPtr, ref rl, ref st, ref pw, bAtr, ref dwAtrLen); } if (status != API.SCardFunctionReturnCodes.SCARD_S_SUCCESS || dwAtrLen != 20) { Marshal.Release(theIntPtr); return(null); } bAtr = new byte[dwAtrLen]; status = (API.SCardFunctionReturnCodes)API.SCardStatus(card, theIntPtr, ref rl, ref st, ref pw, bAtr, ref dwAtrLen); Marshal.Release(theIntPtr); if (status != API.SCardFunctionReturnCodes.SCARD_S_SUCCESS) { return(null); } return(bAtr); }