SCardListReaderGroups
实现在 winscard_clnt.c
定义如下:
3395 LONG SCardListReaderGroups(SCARDCONTEXT hContext, LPSTR mszGroups, 3396 LPDWORD pcchGroups) 3397 { 3398 LONG rv = SCARD_S_SUCCESS; 3399 LONG dwContextIndex; 3400 char *buf = NULL; 3401 3402 PROFILE_START 3403 3404 /* Multi-string with two trailing /0 */ 3405 const char ReaderGroup[] = "SCard$DefaultReaders/0"; 3406 const int dwGroups = sizeof(ReaderGroup); 3407 3408 rv = SCardCheckDaemonAvailability(); 3409 if (rv != SCARD_S_SUCCESS) 3410 return rv; 3411 3412 /* 3413 * Make sure this context has been opened 3414 */ 3415 dwContextIndex = SCardGetContextIndice(hContext); 3416 if (dwContextIndex == -1) 3417 return SCARD_E_INVALID_HANDLE; 3418 3419 (void)SYS_MutexLock(psContextMap[dwContextIndex].mMutex); 3420 3421 /* check the context is still opened */ 3422 dwContextIndex = SCardGetContextIndice(hContext); 3423 if (dwContextIndex == -1) 3424 /* the context is now invalid 3425 * -> another thread may have called SCardReleaseContext 3426 * -> so the mMutex has been unlocked */ 3427 return SCARD_E_INVALID_HANDLE; 3428 3429 if (SCARD_AUTOALLOCATE == *pcchGroups) 3430 { 3431 buf = malloc(dwGroups); 3432 if (NULL == buf)
3433 { 3434 rv = SCARD_E_NO_MEMORY; 3435 goto end; 3436 } 3437 if (NULL == mszGroups) 3438 { 3439 rv = SCARD_E_INVALID_PARAMETER; 3440 goto end; 3441 } 3442 *(char **)mszGroups = buf; 3443 } 3444 else 3445 { 3446 buf = mszGroups; 3447 3448 if ((NULL != mszGroups) && (*pcchGroups < dwGroups)) 3449 { 3450 rv = SCARD_E_INSUFFICIENT_BUFFER; 3451 goto end; 3452 } 3453 } 3454
3455 if (buf) 3456 memcpy(buf, ReaderGroup, dwGroups); 3457 3458 end: 3459 *pcchGroups = dwGroups; 3460 3461 (void)SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex); 3462 3463 PROFILE_END(rv) 3464 3465 return rv; 3466 } |
先说明下, SCardListReaderGroups 调用成功, mszGroups 将返回类似
"SCard$DefaultReaders//0Group 2//0//0" 的多字符串。最后有两个 0.
3408 行, SCardCheckDaemonAvailability 做什么,不说了。上面提了 n 次了。
如果表示震撼,说明了需要重新来过。
3415~3427 行 说明了什么。 SCardGetContextIndice 在前面解释了。重复检查两次,
原因正如注释所说的, SYS_MutexLock 之前,如果 SCardReleaseContext 发生了。
则世界改变了。 Don't panic.
Take it easy.