2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/sc/query.c
5 * PURPOSE: queries service info
6 * COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
11 * Allow calling of 2 options e.g.:
12 * type= driver state= inactive
17 LPTSTR QueryOpts
[] = {
26 LPSERVICE_STATUS_PROCESS
27 QueryService(LPCTSTR ServiceName
)
29 SC_HANDLE hSCManager
= NULL
;
30 LPSERVICE_STATUS_PROCESS pServiceInfo
= NULL
;
33 DWORD BytesNeeded
= 0;
36 hSCManager
= OpenSCManager(NULL
,
39 if (hSCManager
== NULL
)
45 hSc
= OpenService(hSCManager
,
47 SERVICE_QUERY_STATUS
);
51 Ret
= QueryServiceStatusEx(hSc
,
52 SC_STATUS_PROCESS_INFO
,
56 if ((Ret
!= 0) || (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
59 pServiceInfo
= (LPSERVICE_STATUS_PROCESS
)HeapAlloc(GetProcessHeap(),
62 if (pServiceInfo
== NULL
)
65 if (!QueryServiceStatusEx(hSc
,
66 SC_STATUS_PROCESS_INFO
,
74 CloseServiceHandle(hSc
);
75 CloseServiceHandle(hSCManager
);
80 if (pServiceInfo
) HeapFree(GetProcessHeap(), 0, pServiceInfo
);
81 if (hSc
) CloseServiceHandle(hSc
);
82 if (hSCManager
) CloseServiceHandle(hSCManager
);
88 EnumServices(ENUM_SERVICE_STATUS_PROCESS
**pServiceStatus
,
94 DWORD BytesNeeded
= 0;
95 DWORD ResumeHandle
= 0;
96 DWORD NumServices
= 0;
99 hSCManager
= OpenSCManager(NULL
,
101 SC_MANAGER_ENUMERATE_SERVICE
);
102 if (hSCManager
== NULL
)
108 Ret
= EnumServicesStatusEx(hSCManager
,
109 SC_ENUM_PROCESS_INFO
,
112 (LPBYTE
)*pServiceStatus
,
119 if ((Ret
== 0) && (GetLastError() == ERROR_MORE_DATA
))
121 *pServiceStatus
= (ENUM_SERVICE_STATUS_PROCESS
*)
122 HeapAlloc(GetProcessHeap(),
125 if (*pServiceStatus
!= NULL
)
127 if (EnumServicesStatusEx(hSCManager
,
128 SC_ENUM_PROCESS_INFO
,
131 (LPBYTE
)*pServiceStatus
,
145 HeapFree(GetProcessHeap(), 0, *pServiceStatus
);
152 Query(LPCTSTR
*ServiceArgs
,
156 LPENUM_SERVICE_STATUS_PROCESS pServiceStatus
= NULL
;
157 DWORD NumServices
= 0;
159 //DWORD ServiceState;
160 BOOL bServiceName
= TRUE
;
167 TmpArgs
= ServiceArgs
;
169 _tprintf(_T("Arguments:\n"));
172 _tprintf(_T(" %s\n"), *TmpArgs
);
179 /* display all running services and drivers */
182 NumServices
= EnumServices(&pServiceStatus
,
186 if (NumServices
!= 0)
188 for (i
=0; i
< NumServices
; i
++)
190 PrintService(pServiceStatus
[i
].lpServiceName
,
191 &pServiceStatus
[i
].ServiceStatusProcess
,
195 _tprintf(_T("number : %lu\n"), NumServices
);
198 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
206 TmpArgs
= ServiceArgs
;
208 OptSize
= sizeof(QueryOpts
) / sizeof(QueryOpts
[0]);
211 for (i
=0; i
< OptSize
; i
++)
213 if (!lstrcmpi(*TmpArgs
, QueryOpts
[i
]))
215 bServiceName
= FALSE
;
222 /* FIXME: parse options */
225 /* print only the service requested */
228 LPSERVICE_STATUS_PROCESS pStatus
;
229 LPCTSTR ServiceName
= *ServiceArgs
;
231 pStatus
= QueryService(ServiceName
);
234 PrintService(ServiceName
,
241 HeapFree(GetProcessHeap(), 0, pServiceStatus
);