From 47dae4bd057bf035326da2ae8e0ca69dcffb6f77 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Thu, 16 Jun 2016 21:18:59 +0000 Subject: [PATCH] [SC] Add GetKeyName command. svn path=/trunk/; revision=71651 --- reactos/base/applications/sc/name.c | 65 ++++++++++++++++++++++++++++ reactos/base/applications/sc/sc.c | 12 +++++ reactos/base/applications/sc/sc.h | 2 + reactos/base/applications/sc/usage.c | 15 +++++-- 4 files changed, 91 insertions(+), 3 deletions(-) diff --git a/reactos/base/applications/sc/name.c b/reactos/base/applications/sc/name.c index 9d8b9b65b97..f662cde38fd 100644 --- a/reactos/base/applications/sc/name.c +++ b/reactos/base/applications/sc/name.c @@ -73,3 +73,68 @@ done: return bResult; } + +BOOL GetKeyName(LPCTSTR ServiceName) +{ + SC_HANDLE hManager = NULL; + BOOL bResult = TRUE; + DWORD BufferSize = 0; + LPTSTR pBuffer = NULL; + + hManager = OpenSCManager(NULL, + NULL, + SC_MANAGER_CONNECT); + if (hManager == NULL) + { + _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError()); + bResult = FALSE; + goto done; + } + + if (!GetServiceKeyName(hManager, + ServiceName, + NULL, + &BufferSize)) + { + if (BufferSize == 0) + { + _tprintf(_T("[SC] GetServiceKeyName FAILED %lu:\n\n"), GetLastError()); + bResult = FALSE; + goto done; + } + } + + pBuffer = HeapAlloc(GetProcessHeap(), 0, (BufferSize + 1) * sizeof(TCHAR)); + if (pBuffer == NULL) + { + SetLastError(ERROR_OUTOFMEMORY); + _tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError()); + bResult = FALSE; + goto done; + } + + BufferSize++; + if (!GetServiceKeyName(hManager, + ServiceName, + pBuffer, + &BufferSize)) + { + _tprintf(_T("[SC] GetServiceKeyName FAILED %lu:\n\n"), GetLastError()); + bResult = FALSE; + goto done; + } + + _tprintf(_T("[SC] GetServiceKeyName SUCCESS Name = %s\n"), pBuffer); + +done: + if (bResult == FALSE) + ReportLastError(); + + if (pBuffer != NULL) + HeapFree(GetProcessHeap(), 0, pBuffer); + + if (hManager) + CloseServiceHandle(hManager); + + return bResult; +} diff --git a/reactos/base/applications/sc/sc.c b/reactos/base/applications/sc/sc.c index 7a394ecd61e..f58982c2242 100644 --- a/reactos/base/applications/sc/sc.c +++ b/reactos/base/applications/sc/sc.c @@ -284,6 +284,18 @@ ScControl(LPCTSTR Server, // remote machine name else GetDisplayNameUsage(); } + else if (!lstrcmpi(Command, _T("GetKeyName"))) + { + if (ArgCount > 0) + { + ServiceName = *ServiceArgs++; + ArgCount--; + + GetKeyName(ServiceName); + } + else + GetKeyNameUsage(); + } else { MainUsage(); diff --git a/reactos/base/applications/sc/sc.h b/reactos/base/applications/sc/sc.h index 6675fe5d4a4..a3f163666d4 100644 --- a/reactos/base/applications/sc/sc.h +++ b/reactos/base/applications/sc/sc.h @@ -46,6 +46,7 @@ BOOL SetDescription(LPCTSTR ServiceName, LPCTSTR Description); BOOL QueryFailure(LPCTSTR ServiceName); BOOL SetFailure(LPCTSTR *ServiceArgs, INT ArgCount); BOOL GetDisplayName(LPCTSTR ServiceName); +BOOL GetKeyName(LPCTSTR ServiceName); /* print and error functions */ VOID PrintService(LPCTSTR ServiceName, LPSERVICE_STATUS_PROCESS pStatus, BOOL bExtended); @@ -85,5 +86,6 @@ VOID SetDescriptionUsage(VOID); VOID SetConfigUsage(VOID); VOID SetFailureUsage(VOID); VOID GetDisplayNameUsage(VOID); +VOID GetKeyNameUsage(VOID); #endif /* _SC_PCH_ */ diff --git a/reactos/base/applications/sc/usage.c b/reactos/base/applications/sc/usage.c index 118a07a6898..405892e95aa 100644 --- a/reactos/base/applications/sc/usage.c +++ b/reactos/base/applications/sc/usage.c @@ -44,8 +44,8 @@ VOID MainUsage(VOID) _T("\t control : Sends a control to a service.\n") _T("\t sdshow : Displays a service's security descriptor.\n") _T("\t sdset : Sets a service's security descriptor.\n") - _T("\t GetDisplayName : Gets the DisplayName for a service.\n")); -// "\t GetKeyName : Gets the ServiceKeyName for a service.\n") + _T("\t GetDisplayName : Gets the DisplayName for a service.\n") + _T("\t GetKeyName : Gets the ServiceKeyName for a service.\n")); // "\t EnumDepend : Enumerates Service Dependencies.\n") // "\n") // "\tService Name Independant Commands:\n") @@ -266,7 +266,16 @@ VOID SetFailureUsage(VOID) VOID GetDisplayNameUsage(VOID) { _tprintf(_T("DESCRIPTION:\n") - _T(" Gets the display name associated with a particular service\n") + _T(" Gets the display name associated with a particular service.\n") _T("USAGE:\n") _T(" sc GetDisplayName \n")); } + +VOID GetKeyNameUsage(VOID) +{ + _tprintf(_T("DESCRIPTION:\n") + _T(" Gets the key name associated with a particular service, using the\n") + _T(" display name as input.\n") + _T("USAGE:\n") + _T(" sc GetKeyName \n")); +}