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.
Test:
 SCARDCONTEXT hContext;
 LONG rv;
 ...
 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
 rv = SCardReleaseContext(hContext); 

Definition at line 421 of file winscard_clnt.c.

References rxSharedSegment::data, release_struct::hContext, psContextMap, release_struct::rv, SCardCheckDaemonAvailability(), SCardGetContextIndice(), SCardLockThread(), SCardRemoveContext(), SCardUnlockThread(), SHMClientRead(), SYS_MutexLock(), SYS_MutexUnLock(), and WrapSHMWrite().

{
      LONG rv;
      release_struct scReleaseStruct;
      sharedSegmentMsg msgStruct;
      DWORD dwContextIndex;
      PCSCLITE_THREAD_T currentTID;

      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;

      SYS_MutexLock(psContextMap[dwContextIndex].mMutex);   

      /*
       * Test if the thread that would release the context is the thread owning this context
       */
      currentTID = SYS_ThreadSelf();
      rv = SYS_ThreadEqual(&psContextMap[dwContextIndex].TID, &currentTID);
      
      if (rv == 0)
      {
            SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);
            /* Perhaps there is a better error code */
            return SCARD_F_INTERNAL_ERROR;
      }

      scReleaseStruct.hContext = hContext;
      scReleaseStruct.rv = 0;

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

      if (rv == -1)
      {
            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)
      {
            SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex); 
            return SCARD_F_COMM_ERROR;
      }
      
      SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex); 

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

      return scReleaseStruct.rv;
}


Generated by  Doxygen 1.6.0   Back to index