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

前端 未结 2 1048
予麋鹿
予麋鹿 2021-01-28 09:14

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.

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-28 09:45

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

提交回复
热议问题