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.

340 lines
7.5 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1998 - 1999
  3. Module Name:
  4. scappdev
  5. Abstract:
  6. This module provides the device-specific operations that must be performed
  7. by the controlling resource manager application. Due to Plug 'n Play, there
  8. can't be a clean separation between device controller classes and the
  9. application driving them. This module provides the hooks to isolate these
  10. interdependencies as much as possible.
  11. Author:
  12. Doug Barlow (dbarlow) 4/3/1998
  13. Environment:
  14. Win32, C++
  15. Notes:
  16. --*/
  17. #define __SUBROUTINE__
  18. #ifndef WIN32_LEAN_AND_MEAN
  19. #define WIN32_LEAN_AND_MEAN
  20. #endif
  21. #include <windows.h>
  22. #include <winsvc.h>
  23. #include <dbt.h>
  24. #include <WinSCard.h>
  25. #include <CalMsgs.h>
  26. #include <calcom.h>
  27. #include <scardlib.h>
  28. static const GUID l_guidSmartcards
  29. = { // 50DD5230-BA8A-11D1-BF5D-0000F805F530
  30. 0x50DD5230,
  31. 0xBA8A,
  32. 0x11D1,
  33. { 0xBF, 0x5D, 0x00, 0x00, 0xF8, 0x05, 0xF5, 0x30 } };
  34. static SERVICE_STATUS_HANDLE l_hService = NULL;
  35. static DWORD l_dwType = 0;
  36. static HDEVNOTIFY l_hIfDev = NULL;
  37. /*++
  38. AppInitializeDeviceRegistration:
  39. This routine is called by a controlling application in order to enable
  40. PnP and Power Management Events.
  41. Arguments:
  42. hService supplies the handle to the service application.
  43. dwType supplies the type of handle supplied.
  44. Return Value:
  45. None
  46. Author:
  47. Doug Barlow (dbarlow) 4/3/1998
  48. --*/
  49. #undef __SUBROUTINE__
  50. #define __SUBROUTINE__ DBGT("AppInitializeDeviceRegistration")
  51. void
  52. AppInitializeDeviceRegistration(
  53. SERVICE_STATUS_HANDLE hService,
  54. DWORD dwType)
  55. {
  56. DEV_BROADCAST_DEVICEINTERFACE dbcIfFilter;
  57. //
  58. // Save off the application information.
  59. //
  60. ASSERT(NULL == l_hService);
  61. l_hService = hService;
  62. l_dwType = dwType;
  63. ASSERT(NULL == l_hIfDev);
  64. //
  65. // Register for PnP events.
  66. //
  67. ZeroMemory(&dbcIfFilter, sizeof(dbcIfFilter));
  68. dbcIfFilter.dbcc_size = sizeof(dbcIfFilter);
  69. dbcIfFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  70. // dbcIfFilter.dbcc_reserved = NULL;
  71. CopyMemory(
  72. &dbcIfFilter.dbcc_classguid,
  73. &l_guidSmartcards,
  74. sizeof(GUID));
  75. // dbcIfFilter.dbcc_name[1];
  76. l_hIfDev = RegisterDeviceNotification(
  77. l_hService,
  78. &dbcIfFilter,
  79. l_dwType);
  80. if (NULL == l_hIfDev)
  81. {
  82. CalaisWarning(
  83. __SUBROUTINE__,
  84. DBGT("Initialize device registration failed to register for PnP events: %1"),
  85. GetLastError());
  86. }
  87. }
  88. /*++
  89. AppTerminateDeviceRegistration:
  90. This routine is called by a controlling application in order to terminate
  91. PnP and Power Management Events.
  92. Arguments:
  93. None
  94. Return Value:
  95. None
  96. Author:
  97. Doug Barlow (dbarlow) 4/3/1998
  98. --*/
  99. #undef __SUBROUTINE__
  100. #define __SUBROUTINE__ DBGT("AppTerminateDeviceRegistration")
  101. void
  102. AppTerminateDeviceRegistration(
  103. void)
  104. {
  105. BOOL fSts;
  106. //
  107. // Unregister for PnP events.
  108. //
  109. if (NULL != l_hIfDev)
  110. {
  111. try
  112. {
  113. fSts = UnregisterDeviceNotification(l_hIfDev);
  114. }
  115. catch (...)
  116. {
  117. CalaisWarning(
  118. __SUBROUTINE__,
  119. DBGT("Terminate device registration failed to unregister from PnP events: Exception raised"));
  120. }
  121. if (!fSts)
  122. {
  123. CalaisWarning(
  124. __SUBROUTINE__,
  125. DBGT("Terminate device registration failed to unregister from PnP events: %1"),
  126. GetLastError());
  127. }
  128. }
  129. l_hService = NULL;
  130. l_dwType = 0;
  131. l_hIfDev = NULL;
  132. }
  133. /*++
  134. AppRegisterDevice:
  135. This routine is called by a Reader Device Object to inform the controlling
  136. application that it exists and is ready to follow the OS rules for removal.
  137. Arguments:
  138. hReader supplies the handle to the open device.
  139. szReader supplies the name of the device.
  140. ppvAppState supplies a pointer to a storage location for this application
  141. associated with this device. The use of this location is specific to
  142. the application.
  143. Return Value:
  144. None
  145. Author:
  146. Doug Barlow (dbarlow) 4/3/1998
  147. --*/
  148. #undef __SUBROUTINE__
  149. #define __SUBROUTINE__ DBGT("AppRegisterDevice")
  150. void
  151. AppRegisterDevice(
  152. HANDLE hReader,
  153. LPCTSTR szReader,
  154. LPVOID *ppvAppState)
  155. {
  156. //
  157. // Platform-specific initialization.
  158. //
  159. DEV_BROADCAST_HANDLE dbcHandleFilter;
  160. HDEVNOTIFY *phDevNotify = (HDEVNOTIFY *)ppvAppState;
  161. //
  162. // Register for PnP events.
  163. //
  164. if (NULL != l_hService)
  165. {
  166. ASSERT(NULL == *phDevNotify);
  167. ZeroMemory(&dbcHandleFilter, sizeof(dbcHandleFilter));
  168. dbcHandleFilter.dbch_size = sizeof(dbcHandleFilter);
  169. dbcHandleFilter.dbch_devicetype = DBT_DEVTYP_HANDLE;
  170. dbcHandleFilter.dbch_handle = hReader;
  171. *phDevNotify = RegisterDeviceNotification(
  172. l_hService,
  173. &dbcHandleFilter,
  174. l_dwType);
  175. if (NULL == *phDevNotify)
  176. {
  177. CalaisWarning(
  178. __SUBROUTINE__,
  179. DBGT("Register device failed to register '%2' for PnP Device removal: %1"),
  180. GetLastError(),
  181. szReader);
  182. }
  183. }
  184. }
  185. /*++
  186. AppUnregisterDevice:
  187. This routine is called when a device wants to let the controlling
  188. application know that it is officially ceasing to exist.
  189. Arguments:
  190. hReader supplies the handle to the open device.
  191. szReader supplies the name of the device.
  192. ppvAppState supplies a pointer to a storage location for this application
  193. associated with this device. The use of this location is specific to
  194. the application.
  195. Return Value:
  196. None
  197. Author:
  198. Doug Barlow (dbarlow) 4/3/1998
  199. --*/
  200. #undef __SUBROUTINE__
  201. #define __SUBROUTINE__ DBGT("AppUnregisterDevice")
  202. void
  203. AppUnregisterDevice(
  204. HANDLE hReader,
  205. LPCTSTR szReader,
  206. LPVOID *ppvAppState)
  207. {
  208. try
  209. {
  210. //
  211. // Platform-specific initialization.
  212. //
  213. BOOL fSts;
  214. HDEVNOTIFY hDevNotify = *(HDEVNOTIFY *)ppvAppState;
  215. //
  216. // Unregister from PnP events.
  217. //
  218. ASSERT(NULL != l_hIfDev);
  219. if (NULL != hDevNotify)
  220. {
  221. try
  222. {
  223. fSts = UnregisterDeviceNotification(hDevNotify);
  224. }
  225. catch (...)
  226. {
  227. CalaisWarning(
  228. __SUBROUTINE__,
  229. DBGT("Unregister device failed to unregister '%2' from PnP Device removal: EXCEPTION"),
  230. GetLastError(),
  231. szReader);
  232. fSts = TRUE;
  233. }
  234. if (!fSts)
  235. {
  236. CalaisWarning(
  237. __SUBROUTINE__,
  238. DBGT("Unregister device failed to unregister '%2' from PnP Device removal: %1"),
  239. GetLastError(),
  240. szReader);
  241. }
  242. *ppvAppState = NULL;
  243. }
  244. }
  245. catch (...)
  246. {
  247. CalaisWarning(
  248. __SUBROUTINE__,
  249. DBGT("Unregister device received unexpected exception."));
  250. }
  251. }