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 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) { } }