xref: /reactos/base/applications/sc/description.c (revision 5100859e)
1 /*
2  * PROJECT:     ReactOS Services
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        base/applications/sc/description.c
5  * PURPOSE:     Query/Set the service description
6  * COPYRIGHT:   Copyright 2016 Eric Kohl
7  *
8  */
9 
10 #include "sc.h"
11 
12 BOOL QueryDescription(LPCTSTR ServiceName)
13 {
14     SC_HANDLE hManager = NULL;
15     SC_HANDLE hService = NULL;
16     BOOL bResult = TRUE;
17     DWORD cbBytesNeeded = 0;
18     LPSERVICE_DESCRIPTION pServiceDescription = NULL;
19 
20 #ifdef SCDBG
21     _tprintf(_T("service to show description - %s\n\n"), ServiceName);
22 #endif
23 
24     hManager = OpenSCManager(NULL,
25                              NULL,
26                              SC_MANAGER_CONNECT);
27     if (hManager == NULL)
28     {
29         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
30         bResult = FALSE;
31         goto done;
32     }
33 
34     hService = OpenService(hManager, ServiceName, SERVICE_QUERY_CONFIG);
35     if (hService == NULL)
36     {
37         _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
38         bResult = FALSE;
39         goto done;
40     }
41 
42     if (!QueryServiceConfig2(hService,
43                              SERVICE_CONFIG_DESCRIPTION,
44                              NULL,
45                              0,
46                              &cbBytesNeeded))
47     {
48         if (cbBytesNeeded == 0)
49         {
50             _tprintf(_T("[SC] QueryServiceConfig2 FAILED %lu:\n\n"), GetLastError());
51             bResult = FALSE;
52             goto done;
53         }
54     }
55 
56     pServiceDescription = HeapAlloc(GetProcessHeap(), 0, cbBytesNeeded);
57     if (pServiceDescription == NULL)
58     {
59         SetLastError(ERROR_OUTOFMEMORY);
60         _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
61         bResult = FALSE;
62         goto done;
63     }
64 
65     if (!QueryServiceConfig2(hService,
66                              SERVICE_CONFIG_DESCRIPTION,
67                              (LPBYTE)pServiceDescription,
68                              cbBytesNeeded,
69                              &cbBytesNeeded))
70     {
71         _tprintf(_T("[SC] QueryServiceConfig2 FAILED %lu:\n\n"), GetLastError());
72         bResult = FALSE;
73         goto done;
74     }
75 
76     _tprintf(_T("[SC] QueryServiceConfig2 SUCCESS\n\n"));
77 
78     _tprintf(_T("SERVICE_NAME: %s\n"), ServiceName);
79     _tprintf(_T("        DESCRIPTION        : %s\n"),
80              (pServiceDescription->lpDescription) ? pServiceDescription->lpDescription : _T(""));
81 
82 done:
83     if (bResult == FALSE)
84         ReportLastError();
85 
86     if (pServiceDescription != NULL)
87         HeapFree(GetProcessHeap(), 0, pServiceDescription);
88 
89     if (hService)
90         CloseServiceHandle(hService);
91 
92     if (hManager)
93         CloseServiceHandle(hManager);
94 
95     return bResult;
96 }
97 
98 
99 BOOL SetDescription(LPCTSTR ServiceName, LPCTSTR Description)
100 {
101     SC_HANDLE hManager = NULL;
102     SC_HANDLE hService = NULL;
103     BOOL bResult = TRUE;
104     SERVICE_DESCRIPTION ServiceDescription;
105 
106 #ifdef SCDBG
107     _tprintf(_T("service to set description - %s\n\n"), ServiceName);
108 #endif
109 
110     hManager = OpenSCManager(NULL,
111                              NULL,
112                              SC_MANAGER_CONNECT);
113     if (hManager == NULL)
114     {
115         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
116         bResult = FALSE;
117         goto done;
118     }
119 
120     hService = OpenService(hManager, ServiceName, SERVICE_CHANGE_CONFIG);
121     if (hService == NULL)
122     {
123         _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
124         bResult = FALSE;
125         goto done;
126     }
127 
128     ServiceDescription.lpDescription = (LPTSTR)Description;
129 
130     if (!ChangeServiceConfig2(hService,
131                               SERVICE_CONFIG_DESCRIPTION,
132                               (LPBYTE)&ServiceDescription))
133     {
134         _tprintf(_T("[SC] ChangeServiceConfig2 FAILED %lu:\n\n"), GetLastError());
135         bResult = FALSE;
136         goto done;
137     }
138 
139     _tprintf(_T("[SC] ChangeServiceConfig2 SUCCESS\n\n"));
140 
141 done:
142     if (bResult == FALSE)
143         ReportLastError();
144 
145     if (hService)
146         CloseServiceHandle(hService);
147 
148     if (hManager)
149         CloseServiceHandle(hManager);
150 
151     return bResult;
152 }
153