如何在C ++中获取服务显示名称? [英] How do I get the Service Display name in C++?

查看:134
本文介绍了如何在C ++中获取服务显示名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用c ++获取正在运行的服务的显示名称。我试图使用GetServiceDisplayName函数,但它似乎不工作,不知道为什么。

  TTServiceBegin 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_DATASASE,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);
}


}

这个代码,我永远看不到szKeyName在我的调试器的值,它进入if块的消息框,但从不显示消息框。不知道为什么?



无论如何,要获得此服务的显示名称或任何其他/更简单的方法来完成此任务吗?

您需要使用WTSSendMessage而不是MessageBox与活动会话进行交互。  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 =这是来自服务的消息;

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

switch(dwMsgBoxRetValue)
{
case IDTIMEOUT:
//处理TimeOut ...
break;
case IDCANCEL:
//处理取消....
break;
}
}
else
{
//处理错误
}

break;
}
}

WTSFreeMemory(pSessionInfo);
}


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?

解决方案

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

这篇关于如何在C ++中获取服务显示名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆