下面的代码列出控制台用户的用户名
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;