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

LONG SCardReleaseContext ( SCARDCONTEXT  hContext  ) 

This function destroys a communication context to the PC/SC Resource Manager. This must be the last function called in a PC/SC application.

Parameters:
[in] hContext Connection context to be closed.
Returns:
Connection status.
Return values:
SCARD_S_SUCCESS Successful (SCARD_S_SUCCESS)
SCARD_E_NO_SERVICE The server is not runing (SCARD_E_NO_SERVICE)
SCARD_E_INVALID_HANDLE Invalid hContext handle (SCARD_E_INVALID_HANDLE)
SCARD_F_COMM_ERROR An internal communications error has been detected (SCARD_F_COMM_ERROR)
 SCARDCONTEXT hContext;
 LONG rv;
 ...
 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
 rv = SCardReleaseContext(hContext);

Definition at line 576 of file winscard_clnt.c.

References rxSharedSegment::data, release_struct::hContext, PCSCLITE_CLIENT_ATTEMPTS, PCSCLITE_MCLIENT_ATTEMPTS, psContextMap, release_struct::rv, SCARD_E_INVALID_HANDLE, SCARD_E_NO_SERVICE, SCARD_F_COMM_ERROR, SCARD_RELEASE_CONTEXT, SCARD_S_SUCCESS, SCardCheckDaemonAvailability(), SCardGetContextIndice(), SCardLockThread(), SCardRemoveContext(), SCardUnlockThread(), SHMClientRead(), and WrapSHMWrite().

{
      LONG rv;
      release_struct scReleaseStruct;
      sharedSegmentMsg msgStruct;
      LONG dwContextIndex;

      PROFILE_START

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

      rv = SCardCheckDaemonAvailability();
      if (rv != SCARD_S_SUCCESS)
      {
            /*
             * Remove the local context from the stack
             */
            (void)SCardLockThread();
            (void)SCardRemoveContext(hContext);
            (void)SCardUnlockThread();

            return rv;
      }

      (void)SYS_MutexLock(psContextMap[dwContextIndex].mMutex);

      /* check the context is still opened */
      dwContextIndex = SCardGetContextIndice(hContext);
      if (dwContextIndex == -1)
            /* the context is now invalid
             * -> another thread may have called SCardReleaseContext
             * -> so the mMutex has been unlocked */
            return SCARD_E_INVALID_HANDLE;

      scReleaseStruct.hContext = hContext;
      scReleaseStruct.rv = SCARD_S_SUCCESS;

      rv = WrapSHMWrite(SCARD_RELEASE_CONTEXT,
            psContextMap[dwContextIndex].dwClientID,
            sizeof(scReleaseStruct),
            PCSCLITE_MCLIENT_ATTEMPTS, (void *) &scReleaseStruct);

      if (rv == -1)
      {
            (void)SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);
            return SCARD_E_NO_SERVICE;
      }

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

      if (rv == -1)
      {
            (void)SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);
            return SCARD_F_COMM_ERROR;
      }

      (void)SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);

      /*
       * Remove the local context from the stack
       */
      (void)SCardLockThread();
      (void)SCardRemoveContext(hContext);
      (void)SCardUnlockThread();

      PROFILE_END(scReleaseStruct.rv)

      return scReleaseStruct.rv;
}


Generated by  Doxygen 1.6.0   Back to index