ReactOS  0.4.12-dev-14-gd0c8636
description.c
Go to the documentation of this file.
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 
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,
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,
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  {
60  _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
61  bResult = FALSE;
62  goto done;
63  }
64 
65  if (!QueryServiceConfig2(hService,
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)
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 
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,
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,
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 }
#define _tprintf
Definition: tchar.h:506
BOOL SetDescription(LPCTSTR ServiceName, LPCTSTR Description)
Definition: description.c:99
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define SERVICE_CONFIG_DESCRIPTION
Definition: winsvc.h:65
static const WCHAR Description[]
Definition: oid.c:1103
#define OpenService
Definition: winsvc.h:576
CHAR * LPTSTR
Definition: xmlstorage.h:192
unsigned char * LPBYTE
Definition: typedefs.h:52
BOOL QueryDescription(LPCTSTR ServiceName)
Definition: description.c:12
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
smooth NULL
Definition: ftsmooth.c:416
#define QueryServiceConfig2
Definition: winsvc.h:581
#define OpenSCManager
Definition: winsvc.h:575
#define _T(x)
Definition: vfdio.h:22
unsigned int BOOL
Definition: ntddk_ex.h:94
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define SERVICE_CHANGE_CONFIG
Definition: winsvc.h:54
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:409
VOID ReportLastError(VOID)
Definition: sc.c:15
#define SERVICE_QUERY_CONFIG
Definition: winsvc.h:53
#define ChangeServiceConfig2
Definition: winsvc.h:568
#define HeapFree(x, y, z)
Definition: compat.h:394
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13