xref: /reactos/base/applications/sc/depend.c (revision 9393fc32)
1 /*
2  * PROJECT:     ReactOS Services
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        base/applications/sc/depend.c
5  * PURPOSE:
6  * COPYRIGHT:   Copyright 2016 Eric Kohl
7  *
8  */
9 
10 #include "sc.h"
11 
EnumDepend(LPCTSTR ServiceName)12 BOOL EnumDepend(LPCTSTR ServiceName)
13 {
14     SC_HANDLE hManager = NULL;
15     SC_HANDLE hService = NULL;
16     BOOL bResult = TRUE;
17     DWORD BufferSize = 0;
18     DWORD EntriesRead = 0;
19     LPENUM_SERVICE_STATUS pBuffer = NULL;
20     DWORD i;
21 
22     hManager = OpenSCManager(NULL,
23                              NULL,
24                              SC_MANAGER_CONNECT);
25     if (hManager == NULL)
26     {
27         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
28         bResult = FALSE;
29         goto done;
30     }
31 
32     hService = OpenService(hManager, ServiceName, SERVICE_ENUMERATE_DEPENDENTS);
33     if (hService == NULL)
34     {
35         _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
36         bResult = FALSE;
37         goto done;
38     }
39 
40     if (!EnumDependentServices(hService,
41                                SERVICE_STATE_ALL,
42                                NULL,
43                                0,
44                                &BufferSize,
45                                &EntriesRead))
46     {
47         if (BufferSize == 0)
48         {
49             _tprintf(_T("[SC] EnumDependentServices FAILED %lu:\n\n"), GetLastError());
50             bResult = FALSE;
51             goto done;
52         }
53     }
54 
55     pBuffer = HeapAlloc(GetProcessHeap(), 0, BufferSize);
56     if (pBuffer == NULL)
57     {
58         SetLastError(ERROR_OUTOFMEMORY);
59         _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
60         bResult = FALSE;
61         goto done;
62     }
63 
64     if (!EnumDependentServices(hService,
65                                SERVICE_STATE_ALL,
66                                pBuffer,
67                                BufferSize,
68                                &BufferSize,
69                                &EntriesRead))
70     {
71         _tprintf(_T("[SC] EnumDependentServices FAILED %lu:\n\n"), GetLastError());
72         bResult = FALSE;
73         goto done;
74     }
75 
76     _tprintf(_T("Enum: entriesRead = %lu\n"), EntriesRead);
77 
78     for (i = 0; i < EntriesRead; i++)
79     {
80         _tprintf(_T("\n"));
81         _tprintf(_T("SERVICE_NAME: %s\n"), pBuffer[i].lpServiceName);
82         _tprintf(_T("DISPLAY_NAME: %s\n"), pBuffer[i].lpDisplayName);
83         PrintServiceStatus(&pBuffer[i].ServiceStatus);
84     }
85 
86 done:
87     if (bResult == FALSE)
88         ReportLastError();
89 
90     if (pBuffer != NULL)
91         HeapFree(GetProcessHeap(), 0, pBuffer);
92 
93     if (hService)
94         CloseServiceHandle(hService);
95 
96     if (hManager)
97         CloseServiceHandle(hManager);
98 
99     return bResult;
100 }
101