2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsys/system/servman/query.c
5 * PURPOSE: Query service information
6 * COPYRIGHT: Copyright 2005 Ged Murphy <gedmurphy@gmail.com>
12 extern HINSTANCE hInstance
;
13 extern HWND hListView
;
16 /* Stores the service array */
17 ENUM_SERVICE_STATUS_PROCESS
*pServiceStatus
= NULL
;
20 /* free service array */
23 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
32 sel
= ListView_GetHotItem(hListView
);
34 ZeroMemory(&item
, sizeof(LV_ITEM
));
36 item
.mask
= LVIF_TEXT
;
40 item
.cchTextMax
= 200;
42 ListView_GetItem(hListView
, &item
);
46 DisplayString(item
.pszText
);
51 RefreshServiceList(VOID
)
54 TCHAR szNumServices
[32];
56 DWORD NumServices
= 0;
58 LPCTSTR Path
= _T("System\\CurrentControlSet\\Services\\%s");
60 ListView_DeleteAllItems(hListView
);
62 NumServices
= GetServiceList();
66 HICON hiconItem
; /* icon for list-view items */
67 HIMAGELIST hSmall
; /* image list for other views */
68 TCHAR buf
[300]; /* buffer to hold key path */
69 INT NumListedServ
= 0; /* how many services were listed */
71 /* Create the icon image lists */
72 hSmall
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
),
73 GetSystemMetrics(SM_CYSMICON
), ILC_MASK
| ILC_COLOR16
, 1, 1);
75 /* Add an icon to each image list */
76 hiconItem
= LoadImage(hInstance
, MAKEINTRESOURCE(IDI_SM_ICON
), IMAGE_ICON
, 16, 16, 0);
77 ImageList_AddIcon(hSmall
, hiconItem
);
79 /* assign the image to the list view */
80 ListView_SetImageList(hListView
, hSmall
, LVSIL_SMALL
);
83 for (Index
= 0; Index
< NumServices
; Index
++)
86 LPTSTR Description
= NULL
;
88 LPTSTR LogOnAs
= NULL
;
92 /* open the registry key for the service */
93 _sntprintf(buf
, 300, Path
,
94 pServiceStatus
[Index
].lpServiceName
);
96 RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
103 /* set the display name */
105 ZeroMemory(&item
, sizeof(LV_ITEM
));
106 item
.mask
= LVIF_TEXT
;
107 item
.pszText
= pServiceStatus
[Index
].lpDisplayName
;
108 item
.iItem
= ListView_GetItemCount(hListView
);
109 item
.iItem
= ListView_InsertItem(hListView
, &item
);
113 /* set the description */
115 ret
= RegQueryValueEx(hKey
,
121 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_FILE_NOT_FOUND
&& ret
!= ERROR_INVALID_HANDLE
)
127 if (ret
!= ERROR_FILE_NOT_FOUND
)
129 Description
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwValueSize
);
130 if (Description
== NULL
)
135 if(RegQueryValueEx(hKey
,
142 HeapFree(GetProcessHeap(), 0, Description
);
147 item
.pszText
= Description
;
149 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
151 HeapFree(GetProcessHeap(), 0, Description
);
158 if (pServiceStatus
[Index
].ServiceStatusProcess
.dwCurrentState
== SERVICE_RUNNING
)
160 LoadString(hInstance
, IDS_SERVICES_STARTED
, szStatus
, 128);
161 item
.pszText
= szStatus
;
163 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
168 /* set the startup type */
170 dwValueSize
= sizeof(DWORD
);
171 if (RegQueryValueEx(hKey
,
184 LoadString(hInstance
, IDS_SERVICES_AUTO
, szStatus
, 128);
185 item
.pszText
= szStatus
;
187 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
189 else if (StartUp
== 0x03)
191 LoadString(hInstance
, IDS_SERVICES_MAN
, szStatus
, 128);
192 item
.pszText
= szStatus
;
194 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
196 else if (StartUp
== 0x04)
198 LoadString(hInstance
, IDS_SERVICES_DIS
, szStatus
, 128);
199 item
.pszText
= szStatus
;
201 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
209 if (RegQueryValueEx(hKey
,
220 LogOnAs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwValueSize
);
226 if(RegQueryValueEx(hKey
,
233 HeapFree(GetProcessHeap(), 0, LogOnAs
);
238 item
.pszText
= LogOnAs
;
240 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
242 HeapFree(GetProcessHeap(), 0, LogOnAs
);
248 NumListedServ
= ListView_GetItemCount(hListView
);
250 /* set the number of listed services in the status bar */
251 LoadString(hInstance
, IDS_NUM_SERVICES
, szNumServices
, 32);
252 _sntprintf(buf
, 300, szNumServices
, NumListedServ
);
253 SendMessage(hStatus
, SB_SETTEXT
, 0, (LPARAM
)buf
);
267 DWORD BytesNeeded
= 0;
268 DWORD ResumeHandle
= 0;
269 DWORD NumServices
= 0;
271 ScHandle
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
);
272 if (ScHandle
!= INVALID_HANDLE_VALUE
)
274 if (EnumServicesStatusEx(ScHandle
,
275 SC_ENUM_PROCESS_INFO
,
278 (LPBYTE
)pServiceStatus
,
284 /* Call function again if required size was returned */
285 if (GetLastError() == ERROR_MORE_DATA
)
287 /* reserve memory for service info array */
288 pServiceStatus
= (ENUM_SERVICE_STATUS_PROCESS
*) HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
289 if (pServiceStatus
== NULL
)
292 /* fill array with service info */
293 if (EnumServicesStatusEx(ScHandle
,
294 SC_ENUM_PROCESS_INFO
,
297 (LPBYTE
)pServiceStatus
,
304 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
308 else /* exit on failure */
315 CloseServiceHandle(ScHandle
);
333 //SC_HANDLE hService;
334 //DWORD dwHandle, dwLen;
338 //TCHAR szStrFileInfo[80];
339 //TCHAR FileName[MAX_PATH];
342 //LPSERVICE_FAILURE_ACTIONS pServiceFailureActions = NULL;
343 //LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
346 hService = OpenService(ScHandle,
347 pServiceStatus[Index].lpServiceName,
349 if (hService != INVALID_HANDLE_VALUE)
351 / * check if service is required by the system* /
352 if (!QueryServiceConfig2(hService,
353 SERVICE_CONFIG_FAILURE_ACTIONS,
354 (LPBYTE)pServiceFailureActions,
358 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
360 pServiceFailureActions = (LPSERVICE_FAILURE_ACTIONS)
361 HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
362 if (pServiceFailureActions == NULL)
365 if (!QueryServiceConfig2(hService,
366 SERVICE_CONFIG_FAILURE_ACTIONS,
367 (LPBYTE)pServiceFailureActions,
371 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
375 else / * exit on failure * /
380 if (pServiceFailureActions->cActions)
382 if (pServiceFailureActions->lpsaActions[0].Type == SC_ACTION_REBOOT)
384 LoadString(hInstance, IDS_SERVICES_YES, szStatus, 128);
385 item.pszText = szStatus;
387 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
391 if (pServiceFailureActions != NULL)
393 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
394 pServiceFailureActions = NULL;
397 / * get vendor of service binary * /
399 if (!QueryServiceConfig(hService, pServiceConfig, 0, &BytesNeeded))
401 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
403 pServiceConfig = (LPQUERY_SERVICE_CONFIG)
404 HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
405 if (pServiceConfig == NULL)
408 if (!QueryServiceConfig(hService,
413 HeapFree(GetProcessHeap(), 0, pServiceConfig);
417 else / * exit on failure * /
423 memset(&FileName, 0, MAX_PATH);
424 if (_tcscspn(pServiceConfig->lpBinaryPathName, _T("\"")))
426 _tcsncpy(FileName, pServiceConfig->lpBinaryPathName,
427 _tcscspn(pServiceConfig->lpBinaryPathName, _T(" ")) );
431 _tcscpy(FileName, pServiceConfig->lpBinaryPathName);
434 HeapFree(GetProcessHeap(), 0, pServiceConfig);
435 pServiceConfig = NULL;
437 dwLen = GetFileVersionInfoSize(FileName, &dwHandle);
440 lpData = (TCHAR*) HeapAlloc(GetProcessHeap(), 0, dwLen);
444 if (!GetFileVersionInfo (FileName, dwHandle, dwLen, lpData)) {
445 HeapFree(GetProcessHeap(), 0, lpData);
449 if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &pvData, (PUINT) &BufLen))
451 wCodePage = LOWORD(*(DWORD*) pvData);
452 wLangID = HIWORD(*(DWORD*) pvData);
453 wsprintf(szStrFileInfo, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage, wLangID);
456 if (VerQueryValue (lpData, szStrFileInfo, (LPVOID) &lpBuffer, (PUINT) &BufLen)) {
457 item.pszText = lpBuffer;
459 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
461 HeapFree(GetProcessHeap(), 0, lpData);
465 LoadString(hInstance, IDS_SERVICES_UNKNOWN, szStatus, 128);
466 item.pszText = szStatus;
468 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
470 CloseServiceHandle(hService);