none
Windows 2016 - GetServiceDisplayNameW

    Question

  • Hi,

    We run the following code in order to retrieve the name of a specific Windows service. Under normal circumstance, the code works fine, but with Windows 2016, it does not work a reboot time. If I reboot the server, GetServiceDisplayNameW() returns "MyServiceName" into serviceDisplayName variable instead of the service "display name". To work around the problem, we have configure the service with "Delayed Start", but I am wondering if anyone has an idea of the problem.

    Note: If we simply restart the service instead of rebooting the service, everything works fine.

    Thanks!

    SC_HANDLE hHandle;
    wchar_t serviceDisplayName[256];
    DWORD size = 0;
        
    // Get from the installed service
    hHandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (hHandle)
    {
      if (::GetServiceDisplayNameW(hHandle, "MyServiceName", serviceDisplayName, &size))
         wprintf(L"Service Display Name. %s", serviceDisplayName);
      }
      else 
      {
        wprintf(L"Last Error: %s", GetLastErrorAsString());
      }
    }

    else
    {
      wprintf(L"Last Error: %s", GetLastErrorAsString());
    }

    Monday, February 11, 2019 7:00 PM

All replies

  • SC_HANDLE hHandle;
    wchar_t serviceDisplayName[256];
    DWORD size = 0;
        
    // Get from the installed service
    hHandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (hHandle)
    {
      if (::GetServiceDisplayNameW(hHandle, "MyServiceName", serviceDisplayName, &size))
         wprintf(L"Service Display Name. %s", serviceDisplayName);
      }
      else 
      {
        wprintf(L"Last Error: %s", GetLastErrorAsString());
      }
    }

    else
    {
      wprintf(L"Last Error: %s", GetLastErrorAsString());
    }

    I don't think this is really the code being executed.

    GetServiceDisplayNameW wants a LPCWSTR for the service name, not a narrow string like "MyServiceName" and the DWORD parameter that is supposed to specify the size in characters of the buffer is set to 0.

    Monday, February 11, 2019 7:08 PM
  • Sorry, I have tried to removed unnecessary code from my app to help.

    The code actually looks like this :

    SC_HANDLE hHandle ;
    wchar_t serviceDisplayName[256];
    DWORD size = 256/2;
    wstring serviceName;
    
    serviceName = L"MyServiceName";
    
    hHandle  = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (hHandle )
    {
      if (GetServiceDisplayNameW(hHandle , serviceName, serviceDisplayName, &size))
    

    Monday, February 11, 2019 7:23 PM
  • Hello,

    Thanks for posting here.

    If possible, could you please provide more detailed steps or code for us to reproduce your issue, that will help us learn more about your problems and find solutions for you.

    Best Wishes,

    Jack


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 20, 2019 9:35 AM
    Moderator
  • To better illustrate the problem, we can use following application:
    https://code.msdn.microsoft.com/windowsapps/CppWindowsService-cacf4948

    We simply have to make the following update to the code:

    CppWindowsService.cpp - Modify line 38 to 45:

    // Service start options.
    #define SERVICE_START_TYPE       SERVICE_AUTO_START
    
    // List of service dependencies - "dep1\0dep2\0\0"
    #define SERVICE_DEPENDENCIES     NULL
    
    // The name of the account under which the service should run
    #define SERVICE_ACCOUNT          NULL
    


    ServiceBase.cpp - Insert the following after line 155 (m_name = (pszServiceName == NULL) ? L"" : pszServiceName;)

    	DWORD size = 256 / 2;
    	wchar_t displayName[256];
    	SC_HANDLE schSCManager = NULL;
    	
    	schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    	if (schSCManager)
    	{
    		if (GetServiceDisplayNameW(schSCManager, m_name, displayName, &size))
    		{
    			wchar_t szMessage[260];
    			StringCchPrintf(szMessage, ARRAYSIZE(szMessage), L"Service Display Name retrieved succesfully: %s", displayName);
    			WriteEventLogEntry(szMessage, EVENTLOG_SUCCESS);
    		}
    
    		else
    		{
    			WriteEventLogEntry(L"Unable to retrieve Service Display Name", EVENTLOG_ERROR_TYPE);
    		}
    	}

    Then, you can reproduce the problem with the following steps:

    1. Compile the code and copy CppWindowsService.exe on a Windows 2016 box.
    2. Open a command prompt with administrative privileges and run: CppWindowsService.exe -install
    3. Reboot the box.
    4. Open Event Viewer and search for CppWindowsService. You should see and event with the following text: Service Display Name retrieved succesfully: CppWindowsService
    5. Then restart the Windows service (CppWindowsService) and look at the Event Viewer again. You should see and event with the following text: Service Display Name retrieved succesfully: CppWindowsService Sample Service

    Thanks!

    23 hours 20 minutes ago
  • I don't believe this is a coding issue.  Implementing the suggested changes to the CppWindowsService code resulted in the proper service display name being present in the event log upon rebooting my Windows 8.1 system.

    You might consider opening a support case with Microsoft regarding Windows 2016.

    13 hours 57 minutes ago
  • Yes, you are right, the problem seems to only occur with Windows 2016. I don't know how to open ticket with Microsoft for an issue like this. In fact, I don't believe we have maintenance contract. That's why I was relying on the forum.
    12 hours 51 minutes ago