求教,请问在服务程序中如何知道Windows已登录?

ljianq 2006-01-13 09:24:14
如题.
...全文
134 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒋晟 2006-01-15
  • 打赏
  • 举报
回复
下面的代码列出控制台用户的用户名
Topic in microsoft.public.vc.language
FindWindow() to terminal service clients

BOOL ShowConsoleUsernameFromSystem(void)
{
HDESK hdeskCurrent;
HDESK hdeskTest;
HDESK hdesk;
HWINSTA hwinstaCurrent;
HWINSTA hwinsta;
// Save the current Window station
hwinstaCurrent = GetProcessWindowStation();
if (hwinstaCurrent == NULL)
return FALSE;
// Save the current desktop
hdeskCurrent = GetThreadDesktop(GetCurrentThreadId());
if (hdeskCurrent == NULL)
return FALSE;
// Obtain a handle to WinSta0 - service must be running
// in the LocalSystem account
hwinsta = OpenWindowStation("winsta0", FALSE,
WINSTA_ACCESSCLIPBOARD |
WINSTA_ACCESSGLOBALATOMS |
WINSTA_CREATEDESKTOP |
WINSTA_ENUMDESKTOPS |
WINSTA_ENUMERATE |
WINSTA_EXITWINDOWS |
WINSTA_READATTRIBUTES |
WINSTA_READSCREEN |
WINSTA_WRITEATTRIBUTES);
if (hwinsta == NULL)
return FALSE;
// Set the windowstation to be winsta0
if (!SetProcessWindowStation(hwinsta))
return FALSE;
// Get the desktop
hdeskTest = GetThreadDesktop(GetCurrentThreadId());
if (hdeskTest == NULL)
return FALSE;
// Get the default desktop on winsta0
hdesk = OpenDesktop("default", 0, FALSE,
DESKTOP_CREATEMENU |
DESKTOP_CREATEWINDOW |
DESKTOP_ENUMERATE |
DESKTOP_HOOKCONTROL |
DESKTOP_JOURNALPLAYBACK |
DESKTOP_JOURNALRECORD |
DESKTOP_READOBJECTS |
DESKTOP_SWITCHDESKTOP |
DESKTOP_WRITEOBJECTS);
if (hdesk == NULL)
return FALSE;
// Set the desktop to be "default"
if (!SetThreadDesktop(hdesk))
return FALSE;
// Get registry entries
{//really, this is a separate function, just inserted here for ease:
char szUserName[256]={0};
char szDomainName[256]={0};
DWORD type,size;
HKEY hKeyWinlogon = 0;
// Open the registry key to WinLogon entries:
RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
0,
KEY_READ,
&hKeyWinlogon );


// Get the strings from entries:
size = sizeof(szUserName);
RegQueryValueEx(
hKeyWinlogon,
"DefaultUserName",
0L,
&type,
(LPBYTE)szUserName,
&size);
size = sizeof(szDomainName);
RegQueryValueEx(
hKeyWinlogon,
"DefaultDomainName",
0L,
&type,
(LPBYTE)szDomainName,
&size);


RegCloseKey(hKeyWinlogon);
szUserName[120]='\0';
szDomainName[120]='\0';
strcat(szDomainName,"\\");
strcat(szDomainName,szUserName);
MessageBox(NULL, (char *)szDomainName, "User at Console:", MB_OK);
}//end of inserted function
// Reset the Window station and desktop
if (!SetProcessWindowStation(hwinstaCurrent))
return FALSE;


if (!SetThreadDesktop(hdeskCurrent))
return FALSE;
// Close the windowstation and desktop handles
if (!CloseWindowStation(hwinsta))
return FALSE;


if (!CloseDesktop(hdesk))
return FALSE;
return TRUE;
}
对于Windows终端服务器,WinSta0可能不是登录用户的windows station,这时你要调用WTSRegisterSessionNotification来截获用户登录消息。在这之前,需要判断终端服务的状态。
参见
http://msdn.microsoft.com/library/en-us/termserv/termserv/detecting_the_terminal_services_environment.asp
ljianq 2006-01-14
  • 打赏
  • 举报
回复
自己顶
ljianq 2006-01-13
  • 打赏
  • 举报
回复
自己顶
hyj_828 2006-01-13
  • 打赏
  • 举报
回复
你要干啥?

7,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧