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
);
28 RefreshServiceList(VOID
)
31 TCHAR szNumServices
[32];
33 DWORD NumServices
= 0;
35 LPCTSTR Path
= _T("System\\CurrentControlSet\\Services\\%s");
37 ListView_DeleteAllItems(hListView
);
39 NumServices
= GetServiceList();
43 HICON hiconItem
; /* icon for list-view items */
44 HIMAGELIST hSmall
; /* image list for other views */
47 /* Create the icon image lists */
48 hSmall
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
),
49 GetSystemMetrics(SM_CYSMICON
), ILC_MASK
| ILC_COLOR16
, 1, 1);
51 /* Add an icon to each image list */
52 hiconItem
= LoadImage(hInstance
, MAKEINTRESOURCE(IDI_SM_ICON
), IMAGE_ICON
, 16, 16, 0);
53 ImageList_AddIcon(hSmall
, hiconItem
);
55 ListView_SetImageList(hListView
, hSmall
, LVSIL_SMALL
);
57 /* set the number of services in the status bar */
58 LoadString(hInstance
, IDS_SERVICES_NUM_SERVICES
, szNumServices
, 32);
59 _sntprintf(buf
, 300, szNumServices
, NumServices
);
60 SendMessage(hStatus
, SB_SETTEXT
, 0, (LPARAM
)buf
);
63 for (Index
= 0; Index
< NumServices
; Index
++)
66 LPTSTR Description
= NULL
;
68 LPTSTR LogOnAs
= NULL
;
72 /* open the registry key for the service */
73 _sntprintf(buf
, 300, Path
,
74 pServiceStatus
[Index
].lpServiceName
);
76 if( RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
80 &hKey
) != ERROR_SUCCESS
)
87 /* set the display name */
89 ZeroMemory(&item
, sizeof(LV_ITEM
));
90 item
.mask
= LVIF_TEXT
;
91 item
.pszText
= pServiceStatus
[Index
].lpDisplayName
;
92 item
.iItem
= ListView_GetItemCount(hListView
);
93 item
.iItem
= ListView_InsertItem(hListView
, &item
);
97 /* set the description */
99 ret
= RegQueryValueEx(hKey
,
105 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_FILE_NOT_FOUND
)
111 if (ret
!= ERROR_FILE_NOT_FOUND
)
113 Description
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwValueSize
);
114 if (Description
== NULL
)
119 if(RegQueryValueEx(hKey
,
126 HeapFree(GetProcessHeap(), 0, Description
);
131 item
.pszText
= Description
;
133 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
135 HeapFree(GetProcessHeap(), 0, Description
);
141 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
147 if (pServiceStatus
[Index
].ServiceStatusProcess
.dwCurrentState
== SERVICE_RUNNING
)
149 LoadString(hInstance
, IDS_SERVICES_STATUS_RUNNING
, szStatus
, 128);
150 item
.pszText
= szStatus
;
152 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
158 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
161 /* set the startup type */
163 dwValueSize
= sizeof(DWORD
);
164 if (RegQueryValueEx(hKey
,
177 LoadString(hInstance
, IDS_SERVICES_AUTO
, szStatus
, 128);
178 item
.pszText
= szStatus
;
180 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
182 else if (StartUp
== 0x03)
184 LoadString(hInstance
, IDS_SERVICES_MAN
, szStatus
, 128);
185 item
.pszText
= szStatus
;
187 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
189 else if (StartUp
== 0x04)
191 LoadString(hInstance
, IDS_SERVICES_DIS
, szStatus
, 128);
192 item
.pszText
= szStatus
;
194 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
202 if (RegQueryValueEx(hKey
,
213 LogOnAs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwValueSize
);
219 if(RegQueryValueEx(hKey
,
226 HeapFree(GetProcessHeap(), 0, LogOnAs
);
231 item
.pszText
= LogOnAs
;
233 SendMessage(hListView
, LVM_SETITEMTEXT
, item
.iItem
, (LPARAM
) &item
);
235 HeapFree(GetProcessHeap(), 0, LogOnAs
);
253 DWORD BytesNeeded
= 0;
254 DWORD ResumeHandle
= 0;
255 DWORD NumServices
= 0;
257 ScHandle
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
);
258 if (ScHandle
!= INVALID_HANDLE_VALUE
)
260 if (EnumServicesStatusEx(ScHandle
,
261 SC_ENUM_PROCESS_INFO
,
264 (LPBYTE
)pServiceStatus
,
270 /* Call function again if required size was returned */
271 if (GetLastError() == ERROR_MORE_DATA
)
273 /* reserve memory for service info array */
274 pServiceStatus
= (ENUM_SERVICE_STATUS_PROCESS
*) HeapAlloc(GetProcessHeap(), 0, BytesNeeded
);
275 if (pServiceStatus
== NULL
)
278 /* fill array with service info */
279 if (EnumServicesStatusEx(ScHandle
,
280 SC_ENUM_PROCESS_INFO
,
283 (LPBYTE
)pServiceStatus
,
290 HeapFree(GetProcessHeap(), 0, pServiceStatus
);
294 else /* exit on failure */
301 CloseServiceHandle(ScHandle
);
319 //SC_HANDLE hService;
320 //DWORD dwHandle, dwLen;
324 //TCHAR szStrFileInfo[80];
325 //TCHAR FileName[MAX_PATH];
328 //LPSERVICE_FAILURE_ACTIONS pServiceFailureActions = NULL;
329 //LPQUERY_SERVICE_CONFIG pServiceConfig = NULL;
332 hService = OpenService(ScHandle,
333 pServiceStatus[Index].lpServiceName,
335 if (hService != INVALID_HANDLE_VALUE)
337 / * check if service is required by the system* /
338 if (!QueryServiceConfig2(hService,
339 SERVICE_CONFIG_FAILURE_ACTIONS,
340 (LPBYTE)pServiceFailureActions,
344 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
346 pServiceFailureActions = (LPSERVICE_FAILURE_ACTIONS)
347 HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
348 if (pServiceFailureActions == NULL)
351 if (!QueryServiceConfig2(hService,
352 SERVICE_CONFIG_FAILURE_ACTIONS,
353 (LPBYTE)pServiceFailureActions,
357 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
361 else / * exit on failure * /
366 if (pServiceFailureActions->cActions)
368 if (pServiceFailureActions->lpsaActions[0].Type == SC_ACTION_REBOOT)
370 LoadString(hInstance, IDS_SERVICES_YES, szStatus, 128);
371 item.pszText = szStatus;
373 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
377 if (pServiceFailureActions != NULL)
379 HeapFree(GetProcessHeap(), 0, pServiceFailureActions);
380 pServiceFailureActions = NULL;
383 / * get vendor of service binary * /
385 if (!QueryServiceConfig(hService, pServiceConfig, 0, &BytesNeeded))
387 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
389 pServiceConfig = (LPQUERY_SERVICE_CONFIG)
390 HeapAlloc(GetProcessHeap(), 0, BytesNeeded);
391 if (pServiceConfig == NULL)
394 if (!QueryServiceConfig(hService,
399 HeapFree(GetProcessHeap(), 0, pServiceConfig);
403 else / * exit on failure * /
409 memset(&FileName, 0, MAX_PATH);
410 if (_tcscspn(pServiceConfig->lpBinaryPathName, _T("\"")))
412 _tcsncpy(FileName, pServiceConfig->lpBinaryPathName,
413 _tcscspn(pServiceConfig->lpBinaryPathName, _T(" ")) );
417 _tcscpy(FileName, pServiceConfig->lpBinaryPathName);
420 HeapFree(GetProcessHeap(), 0, pServiceConfig);
421 pServiceConfig = NULL;
423 dwLen = GetFileVersionInfoSize(FileName, &dwHandle);
426 lpData = (TCHAR*) HeapAlloc(GetProcessHeap(), 0, dwLen);
430 if (!GetFileVersionInfo (FileName, dwHandle, dwLen, lpData)) {
431 HeapFree(GetProcessHeap(), 0, lpData);
435 if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), &pvData, (PUINT) &BufLen))
437 wCodePage = LOWORD(*(DWORD*) pvData);
438 wLangID = HIWORD(*(DWORD*) pvData);
439 wsprintf(szStrFileInfo, _T("StringFileInfo\\%04X%04X\\CompanyName"), wCodePage, wLangID);
442 if (VerQueryValue (lpData, szStrFileInfo, (LPVOID) &lpBuffer, (PUINT) &BufLen)) {
443 item.pszText = lpBuffer;
445 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
447 HeapFree(GetProcessHeap(), 0, lpData);
451 LoadString(hInstance, IDS_SERVICES_UNKNOWN, szStatus, 128);
452 item.pszText = szStatus;
454 SendMessage(hListView, LVM_SETITEMTEXT, item.iItem, (LPARAM) &item);
456 CloseServiceHandle(hService);