Bullsfan127
Bullsfan127

Reputation: 283

How do I get the Service Display name in C++?

I am trying to get the display name of the running service using c++. I was trying to use the GetServiceDisplayName function but it does not seem to be working, not sure why.

TTServiceBegin( const char *svcName, PFNSERVICE pfnService, bool *svc, PFNTERMINATE pfnTerm,
int flags, int argc, char *argv[], DWORD dynamiteThreadWaitTime )
{
SC_HANDLE serviceStatusHandle;
DWORD dwSizeNeeded = 0 ;
TCHAR* szKeyName = NULL ;

serviceStatusHandle=OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE ,SC_MANAGER_ALL_ACCESS);

GetServiceDisplayName(serviceStatusHandle,svcName, NULL, &dwSizeNeeded);
if(dwSizeNeeded)
{
    szKeyName = new char[dwSizeNeeded+1];
    ZeroMemory(szKeyName,dwSizeNeeded+1);
    if(GetServiceDisplayName(serviceStatusHandle ,svcName,szKeyName,&dwSizeNeeded)!=0)
    {
        MessageBox(0,szKeyName,"Got the key name",0);
    }


}        

When i run this code, i can never see the value of szKeyName in my debugger and it goes into the if block for the message box but never displays the message box. Not sure why?

Anyway to get this to work to get the display name of the service or any other/easier way to accomplish that task?

Upvotes: 0

Views: 1632

Answers (2)

João Augusto
João Augusto

Reputation: 2305

You need to use the WTSSendMessage instead of the MessageBox to interact with the active session.

WTS_SESSION_INFO* pSessionInfo = NULL;          
DWORD dwSessionsCount = 0;
if(WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &dwSessionsCount))
{   
    for(int i=0; i<(int)dwSessionsCount; i++)
    {
        WTS_SESSION_INFO &si = pSessionInfo[i];
        if(si.State == WTSActive)
        {                                                       
            DWORD dwIdCurrentSession = si.SessionId;

            std::string strTitle = "Hello";
            std::string strMessage = "This is a message from the service";

            DWORD dwMsgBoxRetValue = 0;
            if(WTSSendMessage(
                WTS_CURRENT_SERVER_HANDLE,
                dwIdCurrentSession,
                (char*)strTitle.c_str(),
                strTitle.size(),
                (char*)strMessage.c_str(),
                strMessage.size(),
                MB_RETRYCANCEL | MB_ICONINFORMATION | MB_TOPMOST,
                60000,
                &dwMsgBoxRetValue,
                TRUE))
            {

                switch(dwMsgBoxRetValue)
                {
                    case IDTIMEOUT:
                        // Deal with TimeOut...
                        break;
                    case IDCANCEL:          
                        // Deal With Cancel....
                        break;
                }               
            }
            else
            {
                // Deal With Error
            }

            break;
        }
    }

    WTSFreeMemory(pSessionInfo);    
}

Upvotes: 2

hmjd
hmjd

Reputation: 122001

The message box will not be visible on Windows Vista and later due to a change that has services running in a separate session (Session 0 Isolation) that does not have access to a desktop so the message box would not be visible to you, the logged on user.

On Window XP and earlier, you need to tick the Allow service to interact with desktop checkbox under the Log On tab in the service's properties dialog for your service to make message box appear.

Instead, you could write the service name out to a file or run a user application that accepts the name of the service to query and have it query and display the service name (I just tried with the posted code and it works correctly, displaying the message box).

Upvotes: 1

Related Questions