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

LONG SCardConnect ( SCARDCONTEXT  hContext,
LPCTSTR  szReader,
DWORD  dwShareMode,
DWORD  dwPreferredProtocols,
LPDWORD  pdwActiveProtocol 

This function establishes a connection to the friendly name of the reader specified in szReader. The first connection will power up and perform a reset on the card.

[in] hContext Connection context to the PC/SC Resource Manager.
[in] szReader Reader name to connect to.
[in] dwShareMode Mode of connection type: exclusive or 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.
  • SCARD_SHARE_DIRECT - Direct control of the reader, even without a card. SCARD_SHARE_DIRECT can be used before using SCardControl() to send control commands to the reader even if a card is not present in 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.
[out] phCard Handle to this connection.
[out] pdwActiveProtocol Established protocol to this connection.
Error code.
Return values:
SCARD_E_INVALID_HANDLE Invalid hContext handle.
SCARD_E_INVALID_VALUE Invalid sharing mode, requested protocol, or reader name.
SCARD_E_NOT_READY Could not allocate the desired port.
SCARD_E_READER_UNAVAILABLE Could not power up the reader or card.
SCARD_E_SHARING_VIOLATION Someone else has exclusive rights.
SCARD_E_UNSUPPORTED_FEATURE Protocol not supported.
 DWORD dwActiveProtocol;
 LONG rv;
 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
 rv = SCardConnect(hContext, "Reader X", SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);

Definition at line 560 of file winscard_clnt.c.

References rxSharedSegment::data, connect_struct::dwPreferredProtocols, connect_struct::dwShareMode, connect_struct::hContext, connect_struct::pdwActiveProtocol, connect_struct::phCard, psContextMap, SCARD_PROTOCOL_ANY_OLD, SCardCheckDaemonAvailability(), SCardGetContextIndice(), SHMClientRead(), SYS_MutexLock(), SYS_MutexUnLock(), connect_struct::szReader, and WrapSHMWrite().

      LONG rv;
      connect_struct scConnectStruct;
      sharedSegmentMsg msgStruct;
      DWORD dwContextIndex;

       * Check for NULL parameters
      if (phCard == NULL || pdwActiveProtocol == NULL)
            return SCARD_E_INVALID_PARAMETER;
            *phCard = 0;

      if (szReader == NULL)
            return SCARD_E_UNKNOWN_READER;

       * Check for uninitialized strings
      if (strlen(szReader) > MAX_READERNAME)
            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 (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
            return SCARD_E_NO_SERVICE;

       * Make sure this context has been opened
      dwContextIndex = SCardGetContextIndice(hContext);
      if (dwContextIndex == -1)
            return SCARD_E_INVALID_HANDLE;


      strncpy(scConnectStruct.szReader, szReader, MAX_READERNAME);

      scConnectStruct.hContext = hContext;
      scConnectStruct.dwShareMode = dwShareMode;
      scConnectStruct.dwPreferredProtocols = dwPreferredProtocols;
      scConnectStruct.phCard = *phCard;
      scConnectStruct.pdwActiveProtocol = *pdwActiveProtocol;

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

      if (rv == -1)
            return SCARD_E_NO_SERVICE;

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

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

      if (rv == -1)
            return SCARD_F_COMM_ERROR;

      *phCard = scConnectStruct.phCard;
      *pdwActiveProtocol = scConnectStruct.pdwActiveProtocol;

      if (scConnectStruct.rv == SCARD_S_SUCCESS)
             * Keep track of the handle locally
            rv = SCardAddHandle(*phCard, dwContextIndex, (LPTSTR) szReader);
            return rv;

      return scConnectStruct.rv;

Generated by  Doxygen 1.6.0   Back to index