xref: /reactos/base/applications/sc/name.c (revision 40462c92)
1 /*
2  * PROJECT:     ReactOS Services
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        base/applications/sc/name.c
5  * PURPOSE:
6  * COPYRIGHT:   Copyright 2016 Eric Kohl
7  *
8  */
9 
10 #include "sc.h"
11 
12 BOOL GetDisplayName(LPCTSTR ServiceName)
13 {
14     SC_HANDLE hManager = NULL;
15     BOOL bResult = TRUE;
16     DWORD BufferSize = 0;
17     LPTSTR pBuffer = NULL;
18 
19     hManager = OpenSCManager(NULL,
20                              NULL,
21                              SC_MANAGER_CONNECT);
22     if (hManager == NULL)
23     {
24         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
25         bResult = FALSE;
26         goto done;
27     }
28 
29     if (!GetServiceDisplayName(hManager,
30                                ServiceName,
31                                NULL,
32                                &BufferSize))
33     {
34         if (BufferSize == 0)
35         {
36             _tprintf(_T("[SC] GetServiceDisplayName FAILED %lu:\n\n"), GetLastError());
37             bResult = FALSE;
38             goto done;
39         }
40     }
41 
42     pBuffer = HeapAlloc(GetProcessHeap(), 0, (BufferSize + 1) * sizeof(TCHAR));
43     if (pBuffer == NULL)
44     {
45         SetLastError(ERROR_OUTOFMEMORY);
46         _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
47         bResult = FALSE;
48         goto done;
49     }
50 
51     BufferSize++;
52     if (!GetServiceDisplayName(hManager,
53                                ServiceName,
54                                pBuffer,
55                                &BufferSize))
56     {
57         _tprintf(_T("[SC] GetServiceDisplayName FAILED %lu:\n\n"), GetLastError());
58         bResult = FALSE;
59         goto done;
60     }
61 
62     _tprintf(_T("[SC] GetServiceDisplayName SUCCESS  Name = %s\n"), pBuffer);
63 
64 done:
65     if (bResult == FALSE)
66         ReportLastError();
67 
68     if (pBuffer != NULL)
69         HeapFree(GetProcessHeap(), 0, pBuffer);
70 
71     if (hManager)
72         CloseServiceHandle(hManager);
73 
74     return bResult;
75 }
76 
77 BOOL GetKeyName(LPCTSTR ServiceName)
78 {
79     SC_HANDLE hManager = NULL;
80     BOOL bResult = TRUE;
81     DWORD BufferSize = 0;
82     LPTSTR pBuffer = NULL;
83 
84     hManager = OpenSCManager(NULL,
85                              NULL,
86                              SC_MANAGER_CONNECT);
87     if (hManager == NULL)
88     {
89         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
90         bResult = FALSE;
91         goto done;
92     }
93 
94     if (!GetServiceKeyName(hManager,
95                            ServiceName,
96                            NULL,
97                            &BufferSize))
98     {
99         if (BufferSize == 0)
100         {
101             _tprintf(_T("[SC] GetServiceKeyName FAILED %lu:\n\n"), GetLastError());
102             bResult = FALSE;
103             goto done;
104         }
105     }
106 
107     pBuffer = HeapAlloc(GetProcessHeap(), 0, (BufferSize + 1) * sizeof(TCHAR));
108     if (pBuffer == NULL)
109     {
110         SetLastError(ERROR_OUTOFMEMORY);
111         _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
112         bResult = FALSE;
113         goto done;
114     }
115 
116     BufferSize++;
117     if (!GetServiceKeyName(hManager,
118                            ServiceName,
119                            pBuffer,
120                            &BufferSize))
121     {
122         _tprintf(_T("[SC] GetServiceKeyName FAILED %lu:\n\n"), GetLastError());
123         bResult = FALSE;
124         goto done;
125     }
126 
127     _tprintf(_T("[SC] GetServiceKeyName SUCCESS  Name = %s\n"), pBuffer);
128 
129 done:
130     if (bResult == FALSE)
131         ReportLastError();
132 
133     if (pBuffer != NULL)
134         HeapFree(GetProcessHeap(), 0, pBuffer);
135 
136     if (hManager)
137         CloseServiceHandle(hManager);
138 
139     return bResult;
140 }
141