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.

324 lines
9.7 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1999 - 1999
  3. Module Name:
  4. Locate
  5. Abstract:
  6. The ISCardeLocate interface provides services for locating a smart card by
  7. its name.
  8. This interface can display the smart card user interface if it is required.
  9. The following example shows a typical use of the ISCardLocate interface.
  10. The ISCardLocate interface is used to build the an ADPU.
  11. To locate a specific card using its name
  12. 1) Create an ISCardLocate interface.
  13. 2) Call ConfigureCardNameSearch to search for a smart card name.
  14. 3) Call FindCard to search for the smart card.
  15. 4) Interpret the results.
  16. 5) Release the ISCardLocate interface.
  17. Author:
  18. Doug Barlow (dbarlow) 6/24/1999
  19. Notes:
  20. ?Notes?
  21. --*/
  22. #ifndef WIN32_LEAN_AND_MEAN
  23. #define WIN32_LEAN_AND_MEAN
  24. #endif
  25. #include "stdafx.h"
  26. #include "Conversion.h"
  27. #include "Locate.h"
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CSCardLocate
  30. #undef __SUBROUTINE__
  31. #define __SUBROUTINE__ TEXT("CSCardLocate::ConfigureCardGuidSearch")
  32. STDMETHODIMP
  33. CSCardLocate::ConfigureCardGuidSearch(
  34. /* [in] */ LPSAFEARRAY pCardGuids,
  35. /* [defaultvalue][in] */ LPSAFEARRAY pGroupNames,
  36. /* [defaultvalue][in] */ BSTR bstrTitle,
  37. /* [defaultvalue][in] */ LONG lFlags)
  38. {
  39. HRESULT hReturn = S_OK;
  40. try
  41. {
  42. // TODO: Add your implementation code here
  43. breakpoint;
  44. hReturn = E_NOTIMPL;
  45. }
  46. catch (DWORD dwError)
  47. {
  48. hReturn = HRESULT_FROM_WIN32(dwError);
  49. }
  50. catch (HRESULT hError)
  51. {
  52. hReturn = hError;
  53. }
  54. catch (...)
  55. {
  56. hReturn = E_INVALIDARG;
  57. }
  58. return hReturn;
  59. }
  60. /*++
  61. CSCardLocate::ConfigureCardNameSearch:
  62. The ConfigureCardNameSearch method specifies the card names to be used in
  63. the search for the smart card.
  64. Arguments:
  65. pCardNames [in] Pointer to an OLE Automation safe array of card names in
  66. BSTR form.
  67. pGroupNames [in, defaultvalue(NULL )] Pointer to an OLE Automation safe
  68. array of names of card/reader groups in BSTR form to add to the search.
  69. bstrTitle [in, defaultvalue("")] Search common control dialog title.
  70. lFlags [in, defaultvalue(1)] Specifies when user interface is displayed:
  71. Flag Meaning
  72. SC_DLG_MINIMAL_UI Displays the dialog only if the card being searched for
  73. by the calling application is not located and available
  74. for use in a reader. This allows the card to be found,
  75. connected (either through internal dialog mechanism or
  76. the user callback functions), and returned to the
  77. calling application.
  78. SC_DLG_NO_UI Causes no UI display, regardless of the search outcome.
  79. SC_DLG_FORCE_UI Causes UI display regardless of the search outcome.
  80. Return Value:
  81. The return value is an HRESULT. A value of S_OK indicates the call was
  82. successful.
  83. Remarks:
  84. Author:
  85. Doug Barlow (dbarlow) 6/24/1999
  86. --*/
  87. #undef __SUBROUTINE__
  88. #define __SUBROUTINE__ TEXT("CSCardLocate::ConfigureCardNameSearch")
  89. STDMETHODIMP
  90. CSCardLocate::ConfigureCardNameSearch(
  91. /* [in] */ LPSAFEARRAY pCardNames,
  92. /* [defaultvalue][in] */ LPSAFEARRAY pGroupNames,
  93. /* [defaultvalue][in] */ BSTR bstrTitle,
  94. /* [defaultvalue][in] */ LONG lFlags)
  95. {
  96. HRESULT hReturn = S_OK;
  97. try
  98. {
  99. if (NULL != pCardNames)
  100. SafeArrayToMultiString(pCardNames, m_mtzCardNames);
  101. if (NULL != pGroupNames)
  102. SafeArrayToMultiString(pGroupNames, m_mtzGroupNames);
  103. if (NULL != bstrTitle)
  104. m_tzTitle = bstrTitle;
  105. m_lFlags = lFlags;
  106. }
  107. catch (DWORD dwError)
  108. {
  109. hReturn = HRESULT_FROM_WIN32(dwError);
  110. }
  111. catch (HRESULT hError)
  112. {
  113. hReturn = hError;
  114. }
  115. catch (...)
  116. {
  117. hReturn = E_INVALIDARG;
  118. }
  119. return hReturn;
  120. }
  121. /*++
  122. CSCardLocate::FindCard:
  123. The FindCard method searches for the smart card and opens a valid
  124. connection to it.
  125. Arguments:
  126. ShareMode [in, defaultvalue(EXCLUSIVE)] Mode in which to share or not share
  127. the smart card when a connection is opened to it.
  128. Values Description
  129. EXCLUSIVE No one else use this connection to the smart card.
  130. SHARED Other applications can use this connection.
  131. Protocols [in, defaultvalue(T0)] Protocol to use when connecting to the
  132. card.
  133. T0
  134. T1
  135. Raw
  136. T0|T1
  137. lFlags [in, defaultvalue(SC_DLG_NO_UI)] Specifies when user interface is
  138. displayed:
  139. Flag Meaning
  140. SC_DLG_MINIMAL_UI Displays the dialog only if the card being searched
  141. for by the calling application is not located and
  142. available for use in a reader. This allows the
  143. card to be found, connected (either through
  144. internal dialog mechanism or the user callback
  145. functions), and returned to the calling
  146. application.
  147. SC_DLG_NO_UI Causes no UI display, regardless of the search
  148. outcome.
  149. SC_DLG_FORCE_UI Causes UI display regardless of the search outcome.
  150. ppCardInfo [out, retval] Pointer to a data structure that contains/returns
  151. information about the opened smart card, if successful. Will be NULL
  152. if operation has failed.
  153. Return Value:
  154. The return value is an HRESULT. A value of S_OK indicates the call was
  155. successful.
  156. Remarks:
  157. To set the search criteria of the search, call ConfigureCardNameSearch to
  158. specify a smart card's card names or call ConfigureCardGuidSearch to
  159. specify a smart card's interfaces.
  160. Author:
  161. Doug Barlow (dbarlow) 6/24/1999
  162. --*/
  163. #undef __SUBROUTINE__
  164. #define __SUBROUTINE__ TEXT("CSCardLocate::FindCard")
  165. STDMETHODIMP
  166. CSCardLocate::FindCard(
  167. /* [defaultvalue][in] */ SCARD_SHARE_MODES ShareMode,
  168. /* [defaultvalue][in] */ SCARD_PROTOCOLS Protocols,
  169. /* [defaultvalue][in] */ LONG lFlags,
  170. /* [retval][out] */ LPSCARDINFO __RPC_FAR *ppCardInfo)
  171. {
  172. HRESULT hReturn = S_OK;
  173. try
  174. {
  175. LONG lSts;
  176. OPENCARDNAME cardInfo;
  177. ZeroMemory(&cardInfo, sizeof(cardInfo));
  178. cardInfo.dwShareMode = ShareMode;
  179. cardInfo.dwPreferredProtocols = Protocols;
  180. cardInfo.dwFlags = lFlags | m_lFlags;
  181. if ((NULL != ppCardInfo) && (NULL != *ppCardInfo))
  182. {
  183. if (NULL != (*ppCardInfo)->hContext)
  184. cardInfo.hSCardContext = (*ppCardInfo)->hContext;
  185. cardInfo.dwPreferredProtocols = (*ppCardInfo)->ActiveProtocol;
  186. cardInfo.dwShareMode = (*ppCardInfo)->ShareMode;
  187. cardInfo.hwndOwner = (HWND)(*ppCardInfo)->hwndOwner;
  188. cardInfo.lpfnConnect = (LPOCNCONNPROC)(*ppCardInfo)->lpfnConnectProc;
  189. cardInfo.lpfnCheck = (LPOCNCHKPROC)(*ppCardInfo)->lpfnCheckProc;
  190. cardInfo.lpfnDisconnect = (LPOCNDSCPROC)(*ppCardInfo)->lpfnDisconnectProc;
  191. }
  192. if (0 == cardInfo.dwPreferredProtocols)
  193. cardInfo.dwPreferredProtocols = SCARD_PROTOCOL_Tx;
  194. if (0 == cardInfo.dwShareMode)
  195. cardInfo.dwShareMode = SCARD_SHARE_EXCLUSIVE;
  196. cardInfo.dwStructSize = sizeof(OPENCARDNAME);
  197. cardInfo.lpstrGroupNames = (LPTSTR)((LPCTSTR)m_mtzGroupNames);
  198. cardInfo.nMaxGroupNames = m_mtzGroupNames.Length();
  199. cardInfo.lpstrCardNames = (LPTSTR)((LPCTSTR)m_mtzCardNames);
  200. cardInfo.nMaxCardNames = m_mtzCardNames.Length();
  201. cardInfo.rgguidInterfaces = (LPCGUID)m_bfInterfaces.Access();
  202. cardInfo.cguidInterfaces = m_bfInterfaces.Length();
  203. cardInfo.lpstrRdr = (LPTSTR)m_bfRdr.Access();
  204. cardInfo.nMaxRdr = m_bfRdr.Space() / sizeof(TCHAR);
  205. cardInfo.lpstrCard = (LPTSTR)m_bfCard.Access();
  206. cardInfo.nMaxCard = m_bfCard.Space() / sizeof(TCHAR);
  207. cardInfo.lpstrTitle = (LPCTSTR)m_tzTitle;
  208. lSts = GetOpenCardName(&cardInfo);
  209. if (SCARD_S_SUCCESS != lSts)
  210. throw (HRESULT)HRESULT_FROM_WIN32(lSts);
  211. m_bfRdr.Resize(cardInfo.nMaxRdr * sizeof(TCHAR), TRUE);
  212. m_bfCard.Resize(cardInfo.nMaxCard * sizeof(TCHAR), TRUE);
  213. if (NULL != ppCardInfo)
  214. {
  215. if (NULL == *ppCardInfo)
  216. {
  217. *ppCardInfo = &m_subCardInfo;
  218. (*ppCardInfo)->ShareMode = (SCARD_SHARE_MODES)cardInfo.dwShareMode;
  219. }
  220. if (NULL == cardInfo.hCardHandle)
  221. {
  222. lSts = SCardConnect(
  223. cardInfo.hSCardContext,
  224. cardInfo.lpstrRdr,
  225. cardInfo.dwShareMode,
  226. cardInfo.dwPreferredProtocols,
  227. &cardInfo.hCardHandle,
  228. &cardInfo.dwActiveProtocol);
  229. if (SCARD_S_SUCCESS != lSts)
  230. throw (HRESULT)HRESULT_FROM_WIN32(lSts);
  231. }
  232. (*ppCardInfo)->hCard = cardInfo.hCardHandle;
  233. (*ppCardInfo)->hContext = cardInfo.hSCardContext;
  234. (*ppCardInfo)->ActiveProtocol = (SCARD_PROTOCOLS)cardInfo.dwActiveProtocol;
  235. }
  236. }
  237. catch (DWORD dwError)
  238. {
  239. hReturn = HRESULT_FROM_WIN32(dwError);
  240. }
  241. catch (HRESULT hError)
  242. {
  243. hReturn = hError;
  244. }
  245. catch (...)
  246. {
  247. hReturn = E_INVALIDARG;
  248. }
  249. return hReturn;
  250. }