280dbff3ebb6b07e485d4e858d4ad48728adf874
[reactos.git] / base / services / dcomlaunch / dcomlaunch.c
1 /*
2 * PROJECT: ReactOS RPC Subsystem Service
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: DCOMLAUNCH service
5 * COPYRIGHT: Copyright 2019 Pierre Schweitzer
6 */
7
8 /* INCLUDES *****************************************************************/
9
10 #define WIN32_NO_STATUS
11 #define _INC_WINDOWS
12 #define COM_NO_WINDOWS_H
13 #include <stdarg.h>
14 #include <windef.h>
15 #include <winbase.h>
16 #include <winreg.h>
17 #include <winsvc.h>
18
19 #define NDEBUG
20 #include <debug.h>
21
22 /* GLOBALS ******************************************************************/
23
24 static WCHAR ServiceName[] = L"dcomlaunch";
25
26 static SERVICE_STATUS_HANDLE ServiceStatusHandle;
27 static SERVICE_STATUS ServiceStatus;
28
29 /* FUNCTIONS *****************************************************************/
30
31 static VOID
32 UpdateServiceStatus(DWORD dwState)
33 {
34 ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
35 ServiceStatus.dwCurrentState = dwState;
36 ServiceStatus.dwControlsAccepted = 0;
37 ServiceStatus.dwWin32ExitCode = 0;
38 ServiceStatus.dwServiceSpecificExitCode = 0;
39 ServiceStatus.dwCheckPoint = 0;
40
41 if (dwState == SERVICE_START_PENDING ||
42 dwState == SERVICE_STOP_PENDING ||
43 dwState == SERVICE_PAUSE_PENDING ||
44 dwState == SERVICE_CONTINUE_PENDING)
45 ServiceStatus.dwWaitHint = 10000;
46 else
47 ServiceStatus.dwWaitHint = 0;
48
49 SetServiceStatus(ServiceStatusHandle,
50 &ServiceStatus);
51 }
52
53 static DWORD WINAPI
54 ServiceControlHandler(DWORD dwControl,
55 DWORD dwEventType,
56 LPVOID lpEventData,
57 LPVOID lpContext)
58 {
59 DPRINT1("ServiceControlHandler() called\n");
60
61 switch (dwControl)
62 {
63 case SERVICE_CONTROL_STOP:
64 DPRINT1(" SERVICE_CONTROL_STOP received\n");
65 UpdateServiceStatus(SERVICE_STOPPED);
66 return ERROR_SUCCESS;
67
68 case SERVICE_CONTROL_PAUSE:
69 DPRINT1(" SERVICE_CONTROL_PAUSE received\n");
70 UpdateServiceStatus(SERVICE_PAUSED);
71 return ERROR_SUCCESS;
72
73 case SERVICE_CONTROL_CONTINUE:
74 DPRINT1(" SERVICE_CONTROL_CONTINUE received\n");
75 UpdateServiceStatus(SERVICE_RUNNING);
76 return ERROR_SUCCESS;
77
78 case SERVICE_CONTROL_INTERROGATE:
79 DPRINT1(" SERVICE_CONTROL_INTERROGATE received\n");
80 SetServiceStatus(ServiceStatusHandle,
81 &ServiceStatus);
82 return ERROR_SUCCESS;
83
84 case SERVICE_CONTROL_SHUTDOWN:
85 DPRINT1(" SERVICE_CONTROL_SHUTDOWN received\n");
86 UpdateServiceStatus(SERVICE_STOPPED);
87 return ERROR_SUCCESS;
88
89 default :
90 DPRINT1(" Control %lu received\n");
91 return ERROR_CALL_NOT_IMPLEMENTED;
92 }
93 }
94
95 VOID DealWithDeviceEvent();
96
97 VOID WINAPI
98 ServiceMain(DWORD argc, LPTSTR *argv)
99 {
100 UNREFERENCED_PARAMETER(argc);
101 UNREFERENCED_PARAMETER(argv);
102
103 DPRINT("ServiceMain() called\n");
104
105 ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
106 ServiceControlHandler,
107 NULL);
108 if (!ServiceStatusHandle)
109 {
110 DPRINT1("RegisterServiceCtrlHandlerExW() failed! (Error %lu)\n", GetLastError());
111 return;
112 }
113
114 DealWithDeviceEvent();
115
116 UpdateServiceStatus(SERVICE_RUNNING);
117
118 do
119 {
120 Sleep(1);
121 } while (1);
122
123 UpdateServiceStatus(SERVICE_STOPPED);
124 }
125
126 BOOL WINAPI
127 DllMain(HINSTANCE hinstDLL,
128 DWORD fdwReason,
129 LPVOID lpvReserved)
130 {
131 switch (fdwReason)
132 {
133 case DLL_PROCESS_ATTACH:
134 DisableThreadLibraryCalls(hinstDLL);
135 break;
136
137 case DLL_PROCESS_DETACH:
138 break;
139 }
140
141 return TRUE;
142 }