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.

255 lines
7.3 KiB

  1. /*++
  2. *
  3. * Component: hidserv.dll
  4. * File: hidserv.c
  5. * Purpose: main entry and NT service routines.
  6. *
  7. * Copyright (C) Microsoft Corporation 1997,1998. All rights reserved.
  8. *
  9. * WGJ
  10. --*/
  11. #include "hidserv.h"
  12. TCHAR HidservDisplayName[] = TEXT("HID Input Service");
  13. VOID
  14. InitializeGlobals()
  15. /*++
  16. Routine Description:
  17. Since .dll might be unloaded/loaded into the same process, so must reinitialize global vars
  18. --*/
  19. {
  20. PnpEnabled = FALSE;
  21. hNotifyArrival = 0;
  22. INITIAL_WAIT = 500;
  23. REPEAT_INTERVAL = 150;
  24. hInstance = 0;
  25. hWndHidServ = 0;
  26. cThreadRef = 0;
  27. hMutexOOC = 0;
  28. hService = 0;
  29. }
  30. void
  31. StartHidserv(
  32. void
  33. )
  34. /*++
  35. Routine Description:
  36. Cal the SCM to start the NT service.
  37. --*/
  38. {
  39. SC_HANDLE hSCM;
  40. SC_HANDLE hService;
  41. BOOL Ret;
  42. INFO(("Start HidServ Service."));
  43. // Open the SCM on this machine.
  44. hSCM = OpenSCManager( NULL,
  45. NULL,
  46. SC_MANAGER_CONNECT);
  47. if (hSCM) {
  48. // Open this service for DELETE access
  49. hService = OpenService( hSCM,
  50. HidservServiceName,
  51. SERVICE_START);
  52. if (hService) {
  53. // Start this service.
  54. Ret = StartService( hService,
  55. 0,
  56. NULL);
  57. // Close the service and the SCM
  58. CloseServiceHandle(hService);
  59. }
  60. CloseServiceHandle(hSCM);
  61. }
  62. }
  63. void
  64. InstallHidserv(
  65. HWND hwnd,
  66. HINSTANCE hInstance,
  67. LPSTR szCmdLine,
  68. int iCmdShow
  69. )
  70. /*++
  71. Routine Description:
  72. Install the NT service to Auto-start with no dependencies.
  73. --*/
  74. {
  75. SC_HANDLE hService;
  76. SC_HANDLE hSCM;
  77. // Open the SCM on this machine.
  78. hSCM = OpenSCManager( NULL,
  79. NULL,
  80. SC_MANAGER_CREATE_SERVICE);
  81. if (hSCM) {
  82. // Service exists, set to autostart
  83. hService = OpenService(hSCM,
  84. HidservServiceName,
  85. SERVICE_ALL_ACCESS);
  86. if (hService) {
  87. QUERY_SERVICE_CONFIG config;
  88. DWORD junk;
  89. HKEY hKey;
  90. LONG status;
  91. if (ChangeServiceConfig(hService,
  92. SERVICE_NO_CHANGE,
  93. SERVICE_AUTO_START,
  94. SERVICE_NO_CHANGE,
  95. NULL, NULL, NULL,
  96. NULL, NULL, NULL,
  97. HidservDisplayName)) {
  98. // Wait until we're configured correctly.
  99. while (QueryServiceConfig(hService,
  100. &config,
  101. sizeof(config),
  102. &junk)) {
  103. if (config.dwStartType == SERVICE_AUTO_START) {
  104. break;
  105. }
  106. }
  107. }
  108. CloseServiceHandle(hService);
  109. }
  110. }
  111. // Go ahead and start the service for no-reboot install.
  112. StartHidserv();
  113. }
  114. DWORD
  115. WINAPI
  116. ServiceHandlerEx(
  117. DWORD fdwControl, // requested control code
  118. DWORD dwEventType, // event type
  119. LPVOID lpEventData, // event data
  120. LPVOID lpContext // user-defined context data
  121. )
  122. /*++
  123. Routine Description:
  124. Handle the service handler requests as required by the app.
  125. This should virtually always be an async PostMessage. Do not
  126. block this thread.
  127. --*/
  128. {
  129. PWTSSESSION_NOTIFICATION sessionNotification;
  130. switch (fdwControl) {
  131. case SERVICE_CONTROL_INTERROGATE:
  132. INFO(("ServiceHandler Request SERVICE_CONTROL_INTERROGATE (%x)", fdwControl));
  133. SetServiceStatus(hService, &ServiceStatus);
  134. return NO_ERROR;
  135. case SERVICE_CONTROL_CONTINUE:
  136. INFO(("ServiceHandler Request SERVICE_CONTROL_CONTINUE (%x)", fdwControl));
  137. //SET_SERVICE_STATE(SERVICE_START_PENDING);
  138. //PostMessage(hWndMmHid, WM_MMHID_START, 0, 0);
  139. return NO_ERROR;
  140. case SERVICE_CONTROL_PAUSE:
  141. INFO(("ServiceHandler Request SERVICE_CONTROL_PAUSE (%x)", fdwControl));
  142. //SET_SERVICE_STATE(SERVICE_PAUSE_PENDING);
  143. //PostMessage(hWndMmHid, WM_MMHID_STOP, 0, 0);
  144. return NO_ERROR;
  145. case SERVICE_CONTROL_STOP:
  146. INFO(("ServiceHandler Request SERVICE_CONTROL_STOP (%x)", fdwControl));
  147. SET_SERVICE_STATE(SERVICE_STOP_PENDING);
  148. PostMessage(hWndHidServ, WM_CLOSE, 0, 0);
  149. return NO_ERROR;
  150. case SERVICE_CONTROL_SHUTDOWN:
  151. INFO(("ServiceHandler Request SERVICE_CONTROL_SHUTDOWN (%x)", fdwControl));
  152. SET_SERVICE_STATE(SERVICE_STOP_PENDING);
  153. PostMessage(hWndHidServ, WM_CLOSE, 0, 0);
  154. return NO_ERROR;
  155. case SERVICE_CONTROL_SESSIONCHANGE:
  156. INFO(("ServiceHandler Request SERVICE_CONTROL_SESSIONCHANGE (%x)", fdwControl));
  157. sessionNotification = (PWTSSESSION_NOTIFICATION)lpEventData;
  158. PostMessage(hWndHidServ, WM_WTSSESSION_CHANGE, dwEventType, (LPARAM)sessionNotification->dwSessionId);
  159. return NO_ERROR;
  160. default:
  161. WARN(("Unhandled ServiceHandler code, (%x)", fdwControl));
  162. }
  163. return ERROR_CALL_NOT_IMPLEMENTED;
  164. }
  165. VOID
  166. WINAPI
  167. ServiceMain(
  168. DWORD dwArgc,
  169. LPWSTR * lpszArgv
  170. )
  171. /*++
  172. Routine Description:
  173. The main thread for the Hid service.
  174. --*/
  175. {
  176. HANDLE initDoneEvent;
  177. HANDLE threadHandle;
  178. InitializeGlobals();
  179. initDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  180. if (!initDoneEvent) {
  181. goto ServiceMainError;
  182. }
  183. ServiceStatus.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
  184. ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  185. ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_SESSIONCHANGE;
  186. ServiceStatus.dwWin32ExitCode = NO_ERROR;
  187. ServiceStatus.dwServiceSpecificExitCode = NO_ERROR;
  188. ServiceStatus.dwCheckPoint = 0;
  189. ServiceStatus.dwWaitHint = 0;
  190. hService =
  191. RegisterServiceCtrlHandlerEx(HidservServiceName,
  192. ServiceHandlerEx,
  193. NULL);
  194. if (!hService) {
  195. goto ServiceMainError;
  196. }
  197. SET_SERVICE_STATE(SERVICE_START_PENDING);
  198. threadHandle = CreateThread(NULL, // pointer to thread security attributes
  199. 0, // initial thread stack size, in bytes (0 = default)
  200. HidServMain, // pointer to thread function
  201. initDoneEvent, // argument for new thread
  202. 0, // creation flags
  203. &MessagePumpThreadId); // pointer to returned thread identifier
  204. if (!threadHandle) {
  205. goto ServiceMainError;
  206. }
  207. WaitForSingleObject(initDoneEvent, INFINITE);
  208. CloseHandle(threadHandle);
  209. ServiceMainError:
  210. if (initDoneEvent) {
  211. CloseHandle(initDoneEvent);
  212. }
  213. }