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

白昼怎懂夜的黑 提交于 2019-12-04 06:10:11

问题


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?


回答1:


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).




回答2:


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);    
}


来源:https://stackoverflow.com/questions/11436106/how-do-i-get-the-service-display-name-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!