public static bool CardPresent(string readerName) { int retCode; IntPtr hContext = new IntPtr(); 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))); } PcscProvider.SCARD_READERSTATE state = new PcscProvider.SCARD_READERSTATE(); state.szReader = readerName; retCode = PcscProvider.SCardGetStatusChange(hContext, 3000, ref state, 1); if (retCode != 0) { throw new Exception("Unable to get status", new Exception(PcscProvider.GetScardErrMsg(retCode))); } else { //state.dwCurrentState >>= 32; if (((Int32)(state.dwEventState) & PcscProvider.SCARD_STATE_PRESENT) == PcscProvider.SCARD_STATE_PRESENT) { return(true); } else { return(false); } } }
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)); } }