public void fillReader() { int ReaderCount = 255; byte[] retData; byte[] sReaderGroup = null; string[] readerList; string readerStr = string.Empty; readerList = null; int retCode; IntPtr hContext = new IntPtr(); if (doCardPolling) { throw new Exception("Card polling already started"); } 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)); } retCode = PcscProvider.SCardListReaders(hContext, null, null, ref ReaderCount); if (retCode != PcscProvider.SCARD_S_SUCCESS) { throw new Exception("List Reader Failed - " + PcscProvider.GetScardErrMsg(retCode)); } retData = new byte[ReaderCount]; retCode = PcscProvider.SCardListReaders(hContext, sReaderGroup, retData, ref ReaderCount); if (retCode != PcscProvider.SCARD_S_SUCCESS) { throw new Exception("List Reader Failed - " + PcscProvider.GetScardErrMsg(retCode)); } //Convert retData(Hexadecimal) value to String readerStr = System.Text.ASCIIEncoding.ASCII.GetString(retData).Trim('\0'); readerList = readerStr.Split('\0'); foreach (string rdr in readerList) { _readers.Add(rdr); } }
public CHIP_TYPE getChipType() { int rdrLen = 0, retCode, protocol = pcscConnection.activeProtocol; int pdwSate = 0, atrLen = 33; byte[] atr = new byte[100]; CHIP_TYPE cardType = CHIP_TYPE.UNKNOWN; retCode = PcscProvider.SCardStatus(pcscConnection.cardHandle, pcscConnection.readerName, ref rdrLen, ref pdwSate, ref protocol, atr, ref atrLen); if (retCode != PcscProvider.SCARD_S_SUCCESS) { throw new PcscException(retCode); } pcscConnection.activeProtocol = protocol; if (atr.Length < 33) { return(CHIP_TYPE.UNKNOWN); } Array.Resize(ref atr, atrLen); if (atr[13] == 0x00 && atr[14] == 0x01) { cardType = CHIP_TYPE.MIFARE_1K; } else if (atr[13] == 0x00 && atr[14] == 0x02) { cardType = CHIP_TYPE.MIFARE_4K; } else if (atr[13] == 0x00 && atr[14] == 0x03) { cardType = CHIP_TYPE.MIFARE_ULTRALIGHT; } else { cardType = CHIP_TYPE.UNKNOWN; } return(cardType); }
public PcscException(int errCode) { _errorCode = errCode; _message = PcscProvider.GetScardErrMsg(errCode); }
void bw_DoWork(object sender, DoWorkEventArgs e) { try { int retCode; IntPtr hContext = new IntPtr(); CardPollingEventArg arg = new CardPollingEventArg(); arg.reader = e.Argument.ToString(); arg.status = CARD_STATUS.UNKNOWN; retCode = PcscProvider.SCardEstablishContext(PcscProvider.SCARD_SCOPE_USER, 0, 0, ref hContext); if (retCode != PcscProvider.SCARD_S_SUCCESS) { throw new Exception("Unable to establish context", new Exception(PcscProvider.GetScardErrMsg(retCode))); } BackgroundWorker bwOwner = (BackgroundWorker)sender; while (!bwOwner.CancellationPending) { PcscProvider.SCARD_READERSTATE state = new PcscProvider.SCARD_READERSTATE(); state.szReader = e.Argument.ToString(); retCode = PcscProvider.SCardGetStatusChange(hContext, 3000, ref state, 1); if (retCode != 0) { if (arg.status != CARD_STATUS.ERROR) { arg.status = CARD_STATUS.ERROR; CardPollingErrorEventArg errorArg = new CardPollingErrorEventArg(); errorArg.errorCode = retCode; errorArg.errorMessage = PcscProvider.GetScardErrMsg(retCode); errorArg.reader = e.Argument.ToString(); bwOwner.ReportProgress(-1, errorArg); } } else { //state.dwCurrentState >>= 32; if ((state.dwEventState & PcscProvider.SCARD_STATE_PRESENT) == PcscProvider.SCARD_STATE_PRESENT) { if (arg.status != CARD_STATUS.CARD_FOUND) { arg.status = CARD_STATUS.CARD_FOUND; arg.atr = state.rgbAtr.Take(state.cbAtr).ToArray(); bwOwner.ReportProgress(0, arg); } } else { if (arg.status != CARD_STATUS.CARD_NOT_FOUND) { arg.status = CARD_STATUS.CARD_NOT_FOUND; bwOwner.ReportProgress(0, arg); } } } System.Threading.Thread.Sleep(500); } PcscProvider.SCardReleaseContext(hContext); } catch (Exception ex) { } }