Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1098 lines
24 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1999
  3. Module Name:
  4. RdrState
  5. Abstract:
  6. This file contains the outline implementation of the Smartcard Common
  7. dialog CSCardReaderState class. This class encapsulates Smartcard
  8. Reader information.
  9. Author:
  10. Chris Dudley 3/3/1997
  11. Environment:
  12. Win32, C++ w/Exceptions, MFC
  13. Revision History:
  14. Chris Dudley 5/13/1997
  15. Notes:
  16. --*/
  17. /////////////////////////////////////////////////////////////////////////////
  18. //
  19. // Includes
  20. //
  21. #include "stdafx.h"
  22. #include "rdrstate.h"
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28. /////////////////////////////////////////////////////////////////////////////
  29. //
  30. // Local Macros
  31. //
  32. #ifdef _DEBUG
  33. #define TRACE_STR(name,sz) \
  34. TRACE(_T("CmnUILb.lib: %s: %s\n"), name, sz)
  35. #define TRACE_CODE(name,code) \
  36. TRACE(_T("CmnUILb.lib: %s: error = 0x%x\n"), name, code)
  37. #define TRACE_CATCH(name,code) TRACE_CODE(name,code)
  38. #define TRACE_CATCH_UNKNOWN(name) TRACE_STR(name,_T("An unidentified exception has occurred!"))
  39. #else
  40. #define TRACE_STR(name,sz) ((void)0)
  41. #define TRACE_CODE(name,code) ((void)0)
  42. #define TRACE_CATCH(name,code) ((void)0)
  43. #define TRACE_CATCH_UNKNOWN(name) ((void)0)
  44. #endif // _DEBUG
  45. /////////////////////////////////////////////////////////////////////////////
  46. //
  47. // CSCardReaderState Implementation
  48. //
  49. /*++
  50. void CheckCard:
  51. Routine sets an internal flag if the given card name is the currently
  52. in this reader.
  53. Arguments:
  54. LPCTSTR - string containing the card name or names if multistring.
  55. Return Value:
  56. A LONG value indicating the status of the requested action. Please
  57. see the Smartcard header files for additional information.
  58. Author:
  59. Chris Dudley 3/10/1997
  60. Revision History:
  61. Chris Dudley 5/13/1997
  62. Notes:
  63. --*/
  64. LONG CSCardReaderState::CheckCard( LPCTSTR szCardName )
  65. {
  66. // Locals
  67. LPCTSTR szCards = szCardName;
  68. LPCTSTR szCard = m_sCardName;
  69. LONG lReturn = SCARD_S_SUCCESS;
  70. try
  71. {
  72. m_fCardLookup = FALSE;
  73. m_fChecked = FALSE;
  74. //
  75. // Set "Lookup" flag if card name is one we're looking for
  76. //
  77. if (0 == MStringCount(szCards))
  78. {
  79. // if we havn't indicated preferred card names, any
  80. // card name is considered...
  81. m_fCardLookup = TRUE;
  82. }
  83. else
  84. {
  85. szCards = FirstString(szCards);
  86. while ((szCards != NULL) && (!m_fCardLookup))
  87. {
  88. m_fCardLookup = ( _stricmp(szCards, szCard) == 0 );
  89. szCards = NextString(szCards);
  90. }
  91. }
  92. // If there's a match, does the card pass the check?
  93. if (m_fCardLookup)
  94. {
  95. // Call the user's callbacks if they're available
  96. if (IsCallbackValid())
  97. {
  98. lReturn = UserConnect(&m_hCard);
  99. if (SCARDFAILED(lReturn))
  100. {
  101. throw (lReturn);
  102. }
  103. lReturn = UserCheck(); // this is where m_fChecked gets set.
  104. if (SCARDFAILED(lReturn))
  105. {
  106. throw (lReturn);
  107. }
  108. lReturn = UserDisconnect();
  109. if (SCARDFAILED(lReturn))
  110. {
  111. throw (lReturn);
  112. }
  113. }
  114. // Otherwise the card automatically checks out OK!
  115. else
  116. {
  117. m_fChecked = TRUE;
  118. }
  119. }
  120. }
  121. catch (LONG err) {
  122. lReturn = err;
  123. TRACE_CATCH(_T("CheckCard"), err);
  124. }
  125. catch (...) {
  126. lReturn = (LONG) SCARD_F_UNKNOWN_ERROR;
  127. TRACE_CATCH_UNKNOWN(_T("CheckCard"));
  128. }
  129. return lReturn;
  130. }
  131. /*++
  132. LONG Connect:
  133. Attempts to connect to the reader
  134. Arguments:
  135. pHandle - pointer to an SCARDHANDLE that will be returned.
  136. dwShareMode - preferred share.
  137. dwProtocols - preferred protocol
  138. dwActiveProtocol - returned actual protocol in use.
  139. pszReaderName - returned name of reader connecting to.
  140. pszCardName - returned name of card connecting to.
  141. Return Value:
  142. A LONG value indicating the status of the requested action.
  143. See the Smartcard header files for additional information.
  144. Author:
  145. Chris Dudley 3/11/1997
  146. Revision History:
  147. Chris Dudley 5/13/1997
  148. --*/
  149. LONG CSCardReaderState::Connect(SCARDHANDLE *pHandle,
  150. DWORD dwShareMode,
  151. DWORD dwProtocols,
  152. DWORD *pdwActiveProtocol,
  153. CTextString *pszReaderName, //=NULL
  154. CTextString *pszCardName //=NUL
  155. )
  156. {
  157. // Locals
  158. LONG lReturn = SCARD_S_SUCCESS;
  159. try
  160. {
  161. // Check Params
  162. if (NULL == pHandle)
  163. {
  164. throw (LONG)SCARD_E_INVALID_VALUE;
  165. }
  166. if (NULL == pdwActiveProtocol)
  167. {
  168. throw (LONG)SCARD_E_INVALID_VALUE;
  169. }
  170. if (!IsCardInserted())
  171. {
  172. throw (LONG)SCARD_F_INTERNAL_ERROR;
  173. }
  174. if (!IsContextValid())
  175. {
  176. throw (LONG)SCARD_F_INTERNAL_ERROR;
  177. }
  178. // Clear handle
  179. *pHandle = NULL;
  180. if (!m_fConnected)
  181. {
  182. // Attempt to connect
  183. lReturn = SCardConnect( m_hContext,
  184. (LPCTSTR)m_sReaderName,
  185. dwShareMode,
  186. dwProtocols,
  187. &m_hCard,
  188. pdwActiveProtocol);
  189. if (SCARDFAILED(lReturn))
  190. throw (lReturn);
  191. // Return reader/card names
  192. if (pszReaderName != NULL)
  193. (*pszReaderName) = m_sReaderName;
  194. if (pszCardName != NULL)
  195. (*pszCardName) = m_sCardName;
  196. }
  197. *pHandle = m_hCard;
  198. m_fConnected = TRUE;
  199. }
  200. catch(LONG lErr)
  201. {
  202. lReturn = lErr;
  203. TRACE_CATCH(_T("Connect"), lReturn);
  204. }
  205. catch(...)
  206. {
  207. lReturn = SCARD_F_UNKNOWN_ERROR;
  208. TRACE_CATCH_UNKNOWN(_T("Connect"));
  209. }
  210. return lReturn;
  211. }
  212. /*++
  213. LONG GetReaderCardInfo:
  214. Provides a way for user to set two CTextStrings to the reader and card name.
  215. This function is essentially a dummy connect routine -- it is used when the caller
  216. does not wish to actually connect to a card selected by the user, but would like
  217. to be able to know which card was selected, providing the same UI.
  218. Note that this is only of interest to the Common Dialog.
  219. Arguments:
  220. pszReaderName - the Reader;
  221. pszCardName - the Card;
  222. Return Value:
  223. A LONG value indicating the status of the requested action.
  224. ALWAYS SCARD_S_SUCESS.
  225. Author:
  226. Amanda Matlosz 3/24/98
  227. Revision History:
  228. --*/
  229. LONG CSCardReaderState::GetReaderCardInfo(CTextString* pszReaderName,
  230. CTextString* pszCardName)
  231. {
  232. if (NULL != pszReaderName)
  233. {
  234. (*pszReaderName) = m_sReaderName;
  235. }
  236. if (NULL != pszCardName)
  237. {
  238. (*pszCardName) = m_sCardName;
  239. }
  240. return SCARD_S_SUCCESS;
  241. }
  242. /*++
  243. LONG GetReaderInfo:
  244. Retrieves the current state information of the object and returns.
  245. Arguments:
  246. pReaderInfo - pointer to LPSCARD_READERINFO structure
  247. Return Value:
  248. A LONG value indicating the status of the requested action. Please
  249. see the Smartcard header files for additional information.
  250. Author:
  251. Chris Dudley 3/7/1997
  252. Revision History:
  253. Chris Dudley 5/13/1997
  254. --*/
  255. LONG CSCardReaderState::GetReaderInfo( LPSCARD_READERINFO pReaderInfo )
  256. {
  257. // Locals
  258. LONG lReturn = SCARD_S_SUCCESS;
  259. try {
  260. // Check params, etc..
  261. if (NULL == pReaderInfo)
  262. {
  263. throw (LONG)SCARD_E_INVALID_PARAMETER;
  264. }
  265. if (!IsStateValid())
  266. {
  267. throw (LONG)SCARD_F_INTERNAL_ERROR;
  268. }
  269. // Setup the struct
  270. ::ZeroMemory( (LPVOID)pReaderInfo, (DWORD)sizeof(SCARD_READERINFO));
  271. pReaderInfo->sReaderName = m_sReaderName;
  272. pReaderInfo->fCardLookup = m_fCardLookup;
  273. if (IsCardInserted())
  274. {
  275. pReaderInfo->sCardName = m_sCardName;
  276. pReaderInfo->fCardInserted = TRUE;
  277. pReaderInfo->fChecked = m_fChecked;
  278. }
  279. // Set the atr
  280. ::CopyMemory( &(pReaderInfo->rgbAtr),
  281. &(m_ReaderState.rgbAtr),
  282. m_ReaderState.cbAtr);
  283. pReaderInfo->dwAtrLength = m_ReaderState.cbAtr;
  284. //
  285. // Set the state
  286. //
  287. // NO CARD
  288. if(m_ReaderState.dwEventState & SCARD_STATE_EMPTY)
  289. {
  290. pReaderInfo->dwState = SC_STATUS_NO_CARD;
  291. }
  292. // CARD in reader: SHARED, EXCLUSIVE, FREE, UNKNOWN ?
  293. else if(m_ReaderState.dwEventState & SCARD_STATE_PRESENT)
  294. {
  295. if (m_ReaderState.dwEventState & SCARD_STATE_MUTE)
  296. {
  297. pReaderInfo->dwState = SC_STATUS_UNKNOWN;
  298. }
  299. else if (m_ReaderState.dwEventState & SCARD_STATE_INUSE)
  300. {
  301. if(m_ReaderState.dwEventState & SCARD_STATE_EXCLUSIVE)
  302. {
  303. pReaderInfo->dwState = SC_STATUS_EXCLUSIVE;
  304. }
  305. else
  306. {
  307. pReaderInfo->dwState = SC_STATUS_SHARED;
  308. }
  309. }
  310. else
  311. {
  312. pReaderInfo->dwState = SC_SATATUS_AVAILABLE;
  313. }
  314. }
  315. // READER ERROR? at this point, something's gone wrong
  316. else // if(m_ReaderState.dwEventState & SCARD_STATE_UNAVAILABLE)
  317. {
  318. pReaderInfo->dwState = SC_STATUS_ERROR;
  319. }
  320. }
  321. catch(LONG lErr)
  322. {
  323. lReturn = lErr;
  324. TRACE_CATCH(_T("GetReaderInfo"), lReturn);
  325. }
  326. catch(...)
  327. {
  328. lReturn = SCARD_F_UNKNOWN_ERROR;
  329. TRACE_CATCH_UNKNOWN(_T("GetReaderInfo"));
  330. }
  331. return lReturn;
  332. }
  333. /*++
  334. LONG GetReaderState:
  335. Retrieves the current state information of the object and returns in
  336. given SCARD_READERSTATE struct.
  337. Arguments:
  338. pReaderState - pointer to SCARD_READERSTATE_A or SCARD_READERSTATE_W struct
  339. Return Value:
  340. A LONG value indicating the status of the requested action. Please
  341. see the Smartcard header files for additional information.
  342. Author:
  343. Chris Dudley 3/7/1997
  344. Revision History:
  345. Chris Dudley 5/13/1997
  346. Amanda Matlosz 2/01/98 A/W code cleanup
  347. --*/
  348. LONG CSCardReaderState::GetReaderState( LPSCARD_READERSTATE pReaderState )
  349. {
  350. LONG lReturn = SCARD_S_SUCCESS;
  351. try
  352. {
  353. // Check params, etc..
  354. if (NULL == pReaderState)
  355. {
  356. throw (LONG)SCARD_E_INVALID_PARAMETER;
  357. }
  358. if (!IsStateValid())
  359. {
  360. throw (LONG)SCARD_F_INTERNAL_ERROR;
  361. }
  362. // Setup the struct
  363. ::ZeroMemory( (LPVOID)pReaderState,
  364. (DWORD)sizeof(SCARD_READERSTATE));
  365. pReaderState->szReader = m_sReaderName;
  366. pReaderState->dwEventState = m_ReaderState.dwEventState;
  367. pReaderState->dwCurrentState = m_ReaderState.dwCurrentState;
  368. pReaderState->cbAtr = m_ReaderState.cbAtr;
  369. ::CopyMemory( (LPVOID)pReaderState->rgbAtr,
  370. (CONST LPVOID)m_ReaderState.rgbAtr,
  371. (DWORD)pReaderState->cbAtr );
  372. }
  373. catch(LONG lErr)
  374. {
  375. lReturn = lErr;
  376. TRACE_CATCH(_T("FirstReader"), lReturn);
  377. }
  378. catch(...)
  379. {
  380. lReturn = SCARD_F_UNKNOWN_ERROR;
  381. TRACE_CATCH_UNKNOWN(_T("FirstReader"));
  382. }
  383. return lReturn;
  384. }
  385. /*++
  386. BOOL IsCallbackValid:
  387. This routine checks the user callback functions.
  388. Arguments:
  389. None
  390. Return Value:
  391. TRUE if calbacks are valid. FALSE otherwise.
  392. Author:
  393. Chris Dudley 3/15/1997
  394. Revision History:
  395. Chris Dudley 5/13/1997
  396. --*/
  397. BOOL CSCardReaderState::IsCallbackValid ( void )
  398. {
  399. // Locals
  400. BOOL fValid = FALSE;
  401. fValid = (((m_lpfnConnectA != NULL) || (m_lpfnConnectW != NULL)) &&
  402. (m_lpfnCheck != NULL) &&
  403. (m_lpfnDisconnect != NULL) );
  404. return fValid;
  405. }
  406. /*++
  407. BOOL IsCardInserted:
  408. This routine determines if a card is inserted into this object's reader.
  409. Arguments:
  410. None
  411. Return Value:
  412. TRUE if card inserted. FALSE otherwise.
  413. Author:
  414. Chris Dudley 3/3/1997
  415. Revision History:
  416. Chris Dudley 5/13/1997
  417. --*/
  418. BOOL CSCardReaderState::IsCardInserted ( void )
  419. {
  420. // Locals
  421. BOOL fReturn = FALSE;
  422. if (!IsStateValid())
  423. return fReturn;
  424. // Check for card in appropriate struct
  425. fReturn = (m_ReaderState.dwEventState & SCARD_STATE_PRESENT);
  426. return fReturn;
  427. }
  428. /*++
  429. BOOL IsStateValid:
  430. This routine determines if the information in the object is in a valid/usable
  431. state.
  432. Arguments:
  433. None
  434. Return Value:
  435. TRUE if state information is valid. FALSE otherwise.
  436. Author:
  437. Chris Dudley 3/3/1997
  438. Revision History:
  439. Chris Dudley 5/13/1997
  440. --*/
  441. BOOL CSCardReaderState::IsStateValid ( void )
  442. {
  443. // Locals
  444. BOOL fReturn = TRUE;
  445. fReturn = (IsContextValid()) && (NULL != m_ReaderState.szReader);
  446. return fReturn;
  447. }
  448. /*++
  449. void SetContext:
  450. Sets the card context
  451. Arguments:
  452. hContext - card context handle
  453. Return Value:
  454. None.
  455. Author:
  456. Chris Dudley 3/3/1997
  457. Revision History:
  458. Chris Dudley 5/13/1997
  459. --*/
  460. void CSCardReaderState::SetContext( SCARDCONTEXT hContext )
  461. {
  462. // Locals
  463. // Store it
  464. m_hContext = hContext;
  465. }
  466. /*++
  467. LONG SetReaderState:
  468. Sets the internal SCARD_READERSTATE structure for the reader.
  469. Arguments:
  470. lpfnConnectA - pointer to user's connect callback function (ANSI).
  471. lpfnConnectW - pointer to user's conenct callback function (UNICODE).
  472. lpfnCheck - pointer to user's check callback function.
  473. lpfnDisconnect - pointer to user's disconnect callback function.
  474. Return Value:
  475. A LONG value indicating the status of the requested action. Please
  476. see the Smartcard header files for additional information.
  477. Author:
  478. Chris Dudley 3/5/1997
  479. Revision History:
  480. Chris Dudley 5/13/1997
  481. --*/
  482. // ANSI
  483. LONG CSCardReaderState::SetReaderState( LPOCNCONNPROCA lpfnConnectA, // = NULL
  484. LPOCNCHKPROC lpfnCheck, // = NULL
  485. LPOCNDSCPROC lpfnDisconnect, // = NULL
  486. LPVOID lpUserData // = NULL
  487. )
  488. {
  489. LONG lReturn = SCARD_S_SUCCESS;
  490. LPSTR szCardName = NULL;
  491. DWORD dwNumChar = SCARD_AUTOALLOCATE;
  492. try
  493. {
  494. // Check Param,etc.
  495. if (!IsContextValid() || !IsStateValid())
  496. {
  497. throw (LONG)SCARD_F_INTERNAL_ERROR;
  498. }
  499. // Get current status of this reader
  500. lReturn = SCardGetStatusChangeA(m_hContext,
  501. (DWORD)0,
  502. &m_ReaderState,
  503. (DWORD)1);
  504. if(SCARDFAILED(lReturn))
  505. {
  506. throw (lReturn);
  507. }
  508. // Check for inserted card and get card name
  509. if (IsCardInserted())
  510. {
  511. // Is the reader in a state where this is useful?
  512. if ((m_ReaderState.dwEventState & SCARD_STATE_UNAVAILABLE) ||
  513. (m_ReaderState.dwEventState & SCARD_STATE_MUTE) )
  514. {
  515. m_sCardName = szCardName;
  516. }
  517. else
  518. {
  519. lReturn = SCardListCardsA( m_hContext,
  520. (LPCBYTE)m_ReaderState.rgbAtr,
  521. NULL,
  522. (DWORD) 0,
  523. (LPSTR)&szCardName,
  524. &dwNumChar);
  525. if(SCARDFAILED(lReturn))
  526. {
  527. throw (lReturn);
  528. }
  529. // Save the name of the card
  530. m_sCardName = szCardName;
  531. }
  532. }
  533. // Set the current state
  534. m_lpfnConnectW = NULL;
  535. m_ReaderState.dwCurrentState = m_ReaderState.dwEventState;
  536. m_lpfnConnectA = lpfnConnectA;
  537. m_lpfnCheck = lpfnCheck;
  538. m_lpfnDisconnect = lpfnDisconnect;
  539. m_lpUserData = lpUserData;
  540. }
  541. catch(LONG lErr)
  542. {
  543. lReturn = lErr;
  544. TRACE_CATCH(_T("SetReaderState"),lReturn);
  545. }
  546. catch(...)
  547. {
  548. lReturn = SCARD_F_UNKNOWN_ERROR;
  549. TRACE_CATCH_UNKNOWN(_T("SetReaderState"));
  550. }
  551. // Clean Up
  552. if (NULL != szCardName)
  553. {
  554. SCardFreeMemory(m_hContext, (LPVOID)szCardName);
  555. }
  556. return lReturn;
  557. }
  558. // UNICODE
  559. LONG CSCardReaderState::SetReaderState( LPOCNCONNPROCW lpfnConnectW, // = NULL
  560. LPOCNCHKPROC lpfnCheck, // = NULL
  561. LPOCNDSCPROC lpfnDisconnect, // = NULL
  562. LPVOID lpUserData // = NULL
  563. )
  564. {
  565. LONG lReturn = SCARD_S_SUCCESS;
  566. LPWSTR szCardName = NULL;
  567. DWORD dwNumChar = SCARD_AUTOALLOCATE;
  568. try
  569. {
  570. // Check Param
  571. if (!IsContextValid() || !IsStateValid())
  572. {
  573. throw (LONG)SCARD_F_INTERNAL_ERROR;
  574. }
  575. // Get current status of this reader
  576. lReturn = SCardGetStatusChange(m_hContext,
  577. (DWORD) 0,
  578. &m_ReaderState,
  579. (DWORD) 1);
  580. if(SCARDFAILED(lReturn))
  581. {
  582. throw (lReturn);
  583. }
  584. // Check for inserted card and get card name
  585. if(IsCardInserted())
  586. {
  587. // Is the reader in a state where this is useful?
  588. if ((m_ReaderState.dwEventState & SCARD_STATE_UNAVAILABLE) ||
  589. (m_ReaderState.dwEventState & SCARD_STATE_MUTE))
  590. {
  591. m_sCardName = szCardName;
  592. }
  593. else
  594. {
  595. lReturn = SCardListCardsW( m_hContext,
  596. (LPCBYTE)m_ReaderState.rgbAtr,
  597. NULL,
  598. (DWORD)0,
  599. (LPWSTR)&szCardName,
  600. &dwNumChar);
  601. if (SCARDFAILED(lReturn))
  602. throw (lReturn);
  603. // Save the name of the card
  604. m_sCardName = szCardName;
  605. }
  606. }
  607. // Set the current state
  608. m_lpfnConnectA = NULL;
  609. m_ReaderState.dwCurrentState = m_ReaderState.dwEventState;
  610. m_lpfnConnectW = lpfnConnectW;
  611. m_lpfnCheck = lpfnCheck;
  612. m_lpfnDisconnect = lpfnDisconnect;
  613. m_lpUserData = lpUserData;
  614. }
  615. catch(LONG lErr)
  616. {
  617. lReturn = lErr;
  618. TRACE_CATCH(_T("SetReaderState -- UNICODE"),lReturn);
  619. }
  620. catch(...)
  621. {
  622. lReturn = (LONG) SCARD_F_UNKNOWN_ERROR;
  623. TRACE_CATCH_UNKNOWN(_T("SetReaderState -- UNICODE"));
  624. }
  625. // Clean Up
  626. if (NULL != szCardName)
  627. {
  628. SCardFreeMemory(m_hContext, (LPVOID)szCardName);
  629. }
  630. return lReturn;
  631. }
  632. /*++
  633. void StoreName:
  634. Stores a name for the reader associated with this object.
  635. Arguments:
  636. szGroupName - Group name in ANSI or UNICODE
  637. Return Value:
  638. None.
  639. Author:
  640. Chris Dudley 3/3/1997
  641. --*/
  642. void CSCardReaderState::StoreName( LPCTSTR szReaderName )
  643. {
  644. // Store it
  645. m_sReaderName = szReaderName;
  646. m_ReaderState.szReader = m_sReaderName;
  647. }
  648. /*++
  649. LONG UserCheck:
  650. Attempts to check a card using the user callback routine.
  651. Arguments:
  652. None.
  653. Return Value:
  654. A LONG value indicating the status of the requested action. Please
  655. see the Smartcard header files for additional information.
  656. Author:
  657. Chris Dudley 3/11/1997
  658. Revision History:
  659. Chris Dudley 5/13/1997
  660. Notes:
  661. --*/
  662. LONG CSCardReaderState::UserCheck( void )
  663. {
  664. // Locals
  665. LONG lReturn = SCARD_S_SUCCESS;
  666. try {
  667. // Check Params, etc.
  668. if (!IsContextValid())
  669. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  670. if (!IsCardInserted())
  671. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  672. if (!IsCardConnected())
  673. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  674. if (!IsCallbackValid())
  675. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  676. if (!m_fConnected) {
  677. lReturn = UserConnect( &m_hCard );
  678. if (FAILED(lReturn))
  679. throw (lReturn);
  680. };
  681. // Attempt to Check
  682. m_fChecked = m_lpfnCheck ( m_hContext,
  683. m_hCard,
  684. m_lpUserData);
  685. }
  686. catch (LONG err) {
  687. lReturn = err;
  688. TRACE_CATCH(_T("UserCheck"), err);
  689. }
  690. catch (...) {
  691. lReturn = (LONG) SCARD_F_UNKNOWN_ERROR;
  692. TRACE_CATCH_UNKNOWN(_T("UserCheck"));
  693. }
  694. return lReturn;
  695. }
  696. /*++
  697. LONG UserConnect:
  698. Attempts to connect to the reader using a user callback function.
  699. Arguments:
  700. pCard - pointer to a SCARDHANDLE
  701. Return Value:
  702. A LONG value indicating the status of the requested action. Please
  703. see the Smartcard header files for additional information.
  704. Author:
  705. Chris Dudley 3/11/1997
  706. Revision History:
  707. Chris Dudley 5/13/1997
  708. Notes:
  709. --*/
  710. LONG CSCardReaderState::UserConnect(LPSCARDHANDLE pCard,
  711. CTextString *pszReaderName, //=NULL
  712. CTextString *pszCardName //=NULL
  713. )
  714. {
  715. LONG lReturn = SCARD_S_SUCCESS;
  716. try
  717. {
  718. // Check Params, etc.
  719. if (!IsContextValid())
  720. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  721. if (!IsCardInserted())
  722. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  723. if (!IsCallbackValid())
  724. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  725. // Clear handle
  726. (*pCard) = NULL;
  727. if (!m_fConnected)
  728. {
  729. if (NULL != m_lpfnConnectA)
  730. {
  731. m_hCard = m_lpfnConnectA( m_hContext,
  732. (LPSTR)((LPCSTR)m_sReaderName),
  733. (LPSTR)((LPCSTR)m_sCardName),
  734. m_lpUserData);
  735. if ( m_hCard == NULL )
  736. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  737. // Return reader/card names
  738. if (pszReaderName != NULL)
  739. (*pszReaderName) = m_sReaderName;
  740. if (pszCardName != NULL)
  741. (*pszCardName) = m_sCardName;
  742. }
  743. else if (NULL != m_lpfnConnectW)
  744. {
  745. m_hCard = m_lpfnConnectW( m_hContext,
  746. (LPWSTR)((LPCWSTR)m_sReaderName),
  747. (LPWSTR)((LPCWSTR)m_sCardName),
  748. m_lpUserData);
  749. if ( m_hCard == NULL )
  750. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  751. // Return reader/card names
  752. if (pszReaderName != NULL)
  753. (*pszReaderName) = (LPCWSTR)m_sReaderName; // force the unicode version
  754. if (pszCardName != NULL)
  755. (*pszCardName) = (LPCWSTR)m_sCardName; // force the unicode version
  756. }
  757. else
  758. {
  759. throw ( (LONG) SCARD_F_INTERNAL_ERROR ); // should never have gotten here!
  760. }
  761. }
  762. *pCard = m_hCard;
  763. m_fConnected = TRUE;
  764. }
  765. catch(LONG err)
  766. {
  767. lReturn = err;
  768. TRACE_CATCH(_T("UserConnect"), err);
  769. }
  770. catch(...)
  771. {
  772. lReturn = (LONG) SCARD_F_UNKNOWN_ERROR;
  773. TRACE_CATCH_UNKNOWN(_T("UserConnect"));
  774. }
  775. return lReturn;
  776. }
  777. /*++
  778. LONG UserDisconnect:
  779. Attempts to disconnect a card using the user callback routine.
  780. Arguments:
  781. None.
  782. Return Value:
  783. A LONG value indicating the status of the requested action. Please
  784. see the Smartcard header files for additional information.
  785. Author:
  786. Chris Dudley 3/16/1997
  787. Revision History:
  788. Chris Dudley 5/13/1997
  789. Notes:
  790. --*/
  791. LONG CSCardReaderState::UserDisconnect( void )
  792. {
  793. // Locals
  794. LONG lReturn = SCARD_S_SUCCESS;
  795. try {
  796. // Check Params, etc.
  797. if (!IsContextValid())
  798. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  799. if (!IsCardInserted())
  800. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  801. if (!IsCardConnected())
  802. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  803. if (!IsCallbackValid())
  804. throw ( (LONG) SCARD_F_INTERNAL_ERROR );
  805. if (m_fConnected)
  806. {
  807. // Attempt to Disconnect
  808. m_lpfnDisconnect ( m_hContext,
  809. m_hCard,
  810. m_lpUserData);
  811. // Clear handle
  812. m_hCard = NULL;
  813. m_fConnected = FALSE;
  814. };
  815. }
  816. catch (LONG err) {
  817. lReturn = err;
  818. TRACE_CATCH(_T("UserDisconnect"), err);
  819. }
  820. catch (...) {
  821. lReturn = (LONG) SCARD_F_UNKNOWN_ERROR;
  822. TRACE_CATCH_UNKNOWN(_T("UserDisconnect"));
  823. }
  824. return lReturn;
  825. }