Logo Search packages:      
Sourcecode: pcsc-lite version File versions  Download package

LONG SCardReconnect ( SCARDHANDLE  hCard,
DWORD  dwShareMode,
DWORD  dwPreferredProtocols,
DWORD  dwInitialization,
LPDWORD  pdwActiveProtocol 

This function reestablishes a connection to a reader that was previously connected to using SCardConnect().

In a multi application environment it is possible for an application to reset the card in shared mode. When this occurs any other application trying to access certain commands will be returned the value SCARD_W_RESET_CARD. When this occurs SCardReconnect() must be called in order to acknowledge that the card was reset and allow it to change it's state accordingly.

[in] hCard Handle to a previous call to connect.
[in] dwShareMode Mode of connection type: exclusive/shared.
  • SCARD_SHARE_SHARED - This application will allow others to share the reader.
  • SCARD_SHARE_EXCLUSIVE - This application will NOT allow others to share the reader.
[in] dwPreferredProtocols Desired protocol use.
  • SCARD_PROTOCOL_T0 - Use the T=0 protocol.
  • SCARD_PROTOCOL_T1 - Use the T=1 protocol.
  • SCARD_PROTOCOL_RAW - Use with memory type cards.
dwPreferredProtocols is a bit mask of acceptable protocols for the connection. You can use (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1) if you do not have a preferred protocol.
[in] dwInitialization Desired action taken on the card/reader.
  • SCARD_LEAVE_CARD - Do nothing.
  • SCARD_RESET_CARD - Reset the card (warm reset).
  • SCARD_UNPOWER_CARD - Unpower the card (cold reset).
  • SCARD_EJECT_CARD - Eject the card.
[out] pdwActiveProtocol Established protocol to this connection.
Error code.
Return values:
SCARD_E_INVALID_HANDLE Invalid hCard handle.
SCARD_E_NOT_READY Could not allocate the desired port.
SCARD_E_INVALID_VALUE Invalid sharing mode, requested protocol, or reader name.
SCARD_E_READER_UNAVAILABLE The reader has been removed.
SCARD_E_UNSUPPORTED_FEATURE Protocol not supported.
SCARD_E_SHARING_VIOLATION Someone else has exclusive rights.
 DWORD dwActiveProtocol, dwSendLength, dwRecvLength;
 LONG rv;
 BYTE pbRecvBuffer[10];
 BYTE pbSendBuffer[] = {0xC0, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00};
 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
 rv = SCardConnect(hContext, "Reader X", SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
 dwSendLength = sizeof(pbSendBuffer);
 dwRecvLength = sizeof(pbRecvBuffer);
 rv = SCardTransmit(hCard, SCARD_PCI_T0, pbSendBuffer, dwSendLength, &pioRecvPci, pbRecvBuffer, &dwRecvLength);
 / * Card has been reset by another application * /
 if (rv == SCARD_W_RESET_CARD)
   rv = SCardReconnect(hCard, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, SCARD_RESET_CARD, &dwActiveProtocol);

Definition at line 784 of file winscard_clnt.c.

References rxSharedSegment::data, reconnect_struct::dwInitialization, reconnect_struct::dwPreferredProtocols, reconnect_struct::dwShareMode, reconnect_struct::hCard, PCSCLITE_CLIENT_ATTEMPTS, PCSCLITE_MAX_READERS_CONTEXTS, reconnect_struct::pdwActiveProtocol, _psContextMap::psChannelMap, psContextMap, _psChannelMap::readerName, SCARD_EJECT_CARD, SCARD_LEAVE_CARD, SCARD_PROTOCOL_ANY_OLD, SCARD_PROTOCOL_RAW, SCARD_PROTOCOL_T0, SCARD_PROTOCOL_T1, SCARD_RESET_CARD, SCARD_UNPOWER_CARD, SCardCheckDaemonAvailability(), SHMClientRead(), SYS_MutexLock(), SYS_MutexUnLock(), and WrapSHMWrite().

      LONG rv;
      reconnect_struct scReconnectStruct;
      sharedSegmentMsg msgStruct;
      int i;
      DWORD dwContextIndex, dwChannelIndex;


      if (dwInitialization != SCARD_LEAVE_CARD &&
            dwInitialization != SCARD_RESET_CARD &&
            dwInitialization != SCARD_UNPOWER_CARD &&
            dwInitialization != SCARD_EJECT_CARD)
            return SCARD_E_INVALID_VALUE;

      if (!(dwPreferredProtocols & SCARD_PROTOCOL_T0) &&
            !(dwPreferredProtocols & SCARD_PROTOCOL_T1) &&
            !(dwPreferredProtocols & SCARD_PROTOCOL_RAW) &&
            !(dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD))
            return SCARD_E_INVALID_VALUE;

      if (pdwActiveProtocol == NULL)
            return SCARD_E_INVALID_PARAMETER;

      if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
            return SCARD_E_NO_SERVICE;

       * Make sure this handle has been opened
      rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);

      if (rv == -1)
            return SCARD_E_INVALID_HANDLE;


      for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
            char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;

            /* by default r == NULL */
            if (r && strcmp(r, (readerStates[i])->readerName) == 0)

            return SCARD_E_READER_UNAVAILABLE;

      scReconnectStruct.hCard = hCard;
      scReconnectStruct.dwShareMode = dwShareMode;
      scReconnectStruct.dwPreferredProtocols = dwPreferredProtocols;
      scReconnectStruct.dwInitialization = dwInitialization;
      scReconnectStruct.pdwActiveProtocol = *pdwActiveProtocol;

      rv = WrapSHMWrite(SCARD_RECONNECT, psContextMap[dwContextIndex].dwClientID,
            PCSCLITE_CLIENT_ATTEMPTS, (void *) &scReconnectStruct);

      if (rv == -1)
            return SCARD_E_NO_SERVICE;

       * Read a message from the server
      rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);

      memcpy(&scReconnectStruct, &msgStruct.data, sizeof(scReconnectStruct));

      if (rv == -1)
            return SCARD_F_COMM_ERROR;

      *pdwActiveProtocol = scReconnectStruct.pdwActiveProtocol;


      return scReconnectStruct.rv;

Generated by  Doxygen 1.6.0   Back to index