public void sendCardControl() { byte[] sendBuff, recvbuff; int sendLen, recvLen, returnCode, actualLength = 0; PcscProvider.SCARD_IO_REQUEST ioRequest; ioRequest.dwProtocol = pdwActiveProtocol_; ioRequest.cbPciLength = 8; if (apduCommand.data == null) { throw new Exception("No data specified"); } sendBuff = new byte[apduCommand.data.Length]; recvLen = apduCommand.lengthExpected; Array.Copy(apduCommand.data, 0, sendBuff, 0, apduCommand.data.Length); sendLen = sendBuff.Length; apduCommand.statusWord = new byte[2]; recvbuff = new byte[recvLen]; sendCommandTriggerEvent(new TransmitApduEventArg(sendBuff)); returnCode = PcscProvider.SCardControl(hCard_, operationControlCode, sendBuff, sendLen, recvbuff, recvbuff.Length, ref actualLength); if (returnCode == 0) { apduCommand.actualLengthReceived = actualLength; receivedCommandTriggerEvent(new TransmitApduEventArg(recvbuff.Take(actualLength).ToArray())); apduCommand.response = new byte[actualLength]; Array.Copy(recvbuff, 0, apduCommand.response, 0, actualLength); if (actualLength > 1) { Array.Copy(recvbuff, actualLength - 2, apduCommand.statusWord, 0, 2); } //if (apdu.actualLengthReceived >= 2) //{ // apdu.receiveData = new byte[actualLength - 2]; // Array.Copy(recvbuff, 0, apdu.receiveData, 0, actualLength - 2); //} } else { throw new PcscException(returnCode); } }
public void disconnect() { int returnValue = PcscProvider.SCardDisconnect(hCard_, PcscProvider.SCARD_UNPOWER_CARD); if (returnValue != PcscProvider.SCARD_S_SUCCESS) { throw new PcscException(returnValue); } }
protected void releaseContext() { int retCode = PcscProvider.SCardReleaseContext(hContext_); if (retCode != PcscProvider.SCARD_S_SUCCESS) { throw new PcscException(retCode); } }
protected void establishContext() { int retCode; retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext_); if (retCode != PcscProvider.SCARD_S_SUCCESS) { throw new Exception("Unable to establish context - " + PcscProvider.GetScardErrMsg(retCode)); } }
public void connect(string readerName, int preferedProtocol, int shareMode) { int returnCode; returnCode = PcscProvider.SCardConnect(hContext_, readerName, shareMode, preferedProtocol, ref hCard_, ref pdwActiveProtocol_); if (returnCode != PcscProvider.SCARD_S_SUCCESS) { lastError = returnCode; throw new PcscException(returnCode); } shareMode_ = shareMode; pProtocol_ = preferedProtocol; _readerName = readerName; }
public virtual byte[] getAtr() { int rdrLen = 0, retCode, protocol = activeProtocol; int pdwSate = 0, atrLen = 33; byte[] atr = new byte[100]; retCode = PcscProvider.SCardStatus(cardHandle, readerName, ref rdrLen, ref pdwSate, ref protocol, atr, ref atrLen); if (retCode != 0) { throw new PcscException(retCode); } return(atr.Take(atrLen).ToArray()); }
public string[] getReaderList() { byte[] returnData; byte[] sReaderGroup = null; string[] readerList = new string[0]; string readerString = string.Empty; int returnCode; IntPtr hContext = new IntPtr(); int readerCount = 255; returnCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext); if (returnCode != PcscProvider.SCARD_S_SUCCESS) { lastError = returnCode; return(readerList); } returnCode = PcscProvider.SCardListReaders(hContext_, null, null, ref readerCount); if (returnCode != PcscProvider.SCARD_S_SUCCESS) { lastError = returnCode; return(readerList); } returnData = new byte[readerCount]; returnCode = PcscProvider.SCardListReaders(hContext_, sReaderGroup, returnData, ref readerCount); if (returnCode != PcscProvider.SCARD_S_SUCCESS) { return(readerList); } readerString = System.Text.ASCIIEncoding.ASCII.GetString(returnData).Trim('\0'); readerList = readerString.Split('\0'); return(readerList); }
public void sendCommand() { byte[] sendBuff, recvBuff; int sendLen, recvLen, returnCode; PcscProvider.SCARD_IO_REQUEST ioRequest; ioRequest.dwProtocol = pdwActiveProtocol_; ioRequest.cbPciLength = 8; if (apduCommand.data == null) { sendBuff = new byte[5]; } else { sendBuff = new byte[5 + apduCommand.data.Length]; } recvLen = apduCommand.lengthExpected + 2; Array.Copy(new byte[] { apduCommand.instructionClass, apduCommand.instructionCode, apduCommand.parameter1, apduCommand.parameter2, apduCommand.parameter3 }, sendBuff, 5); if (apduCommand.data != null) { Array.Copy(apduCommand.data, 0, sendBuff, 5, apduCommand.data.Length); } sendLen = sendBuff.Length; apduCommand.statusWord = new byte[2]; recvBuff = new byte[recvLen]; sendCommandTriggerEvent(new TransmitApduEventArg(sendBuff)); returnCode = PcscProvider.SCardTransmit(hCard_, ref ioRequest, sendBuff, sendLen, ref ioRequest, recvBuff, ref recvLen); if (returnCode == 0) { receivedCommandTriggerEvent(new TransmitApduEventArg(recvBuff.Take(recvLen).ToArray())); if (recvLen > 1) { Array.Copy(recvBuff, recvLen - 2, apduCommand.statusWord, 0, 2); } if (recvLen > 2) { apduCommand.response = new byte[recvLen - 2]; Array.Copy(recvBuff, 0, apduCommand.response, 0, recvLen - 2); } } else { throw new PcscException(returnCode); } }