[英]How do I get the Service Display name in C++?
我試圖使用c ++獲取正在運行的服務的顯示名稱。 我試圖使用GetServiceDisplayName函數但它似乎沒有工作,不知道為什么。
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);
}
}
當我運行此代碼時,我永遠無法在調試器中看到szKeyName的值,它進入消息框的if塊但從不顯示消息框。 不知道為什么?
無論如何要讓它工作以獲得服務的顯示名稱或任何其他/更簡單的方法來完成該任務?
消息框在Windows Vista及更高版本中將不可見,因為更改的服務在單獨的會話( 會話0隔離 )中運行,該會話無法訪問桌面,因此您將無法看到消息框在用戶上。
在Window XP及更早版本中,您需要在服務的屬性對話框中的“ 登錄”選項卡下勾選“ Allow service to interact with desktop
復選框,以顯示要顯示的服務消息框。
相反,您可以將服務名稱寫入文件或運行接受要查詢的服務名稱的用戶應用程序,並讓它查詢並顯示服務名稱(我只是嘗試使用已發布的代碼並且它正常工作,顯示消息框)。
您需要使用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 = "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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.