Service programs (services) generally can not interact with the user, so he is generally unable to display the window. To interact with the user (such as the display window), we must open the user WinSta0 and desktop, and the following code opens the above two and displays a dialog:
BOOL Cservicemodule::showmsgdlg (void)
{
Hdesk hdeskcurrent;
Hdesk Hdesk;
Hwinsta hwinstacurrent;
Hwinsta Hwinsta;
Hwinstacurrent = Getprocesswindowstation ();
if (hwinstacurrent = = NULL) {
LogEvent (_t ("Get window Station err"));
return FALSE;
}
Hdeskcurrent = Getthreaddesktop (GetCurrentThreadID ());
if (hdeskcurrent = = NULL) {
LogEvent (_t ("Get window Desktop Err"));
return FALSE;
}
Open winsta0
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) {
LogEvent (_t ("open window Station err"));
return FALSE;
}
if (! SetProcessWindowStation (Hwinsta)) {
LogEvent (_t ("Set window station err"));
return FALSE;
}
Open Desktop
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) {
LogEvent (_t ("Open Desktop Err"));
return FALSE;
}
SetThreadDesktop (hdesk);
In this step, we get the right to interact with the user, such as the display window.
Cmsgdlg dlgmsg;
Show a dialog
Dlgmsg.domodal ();
if (! SetProcessWindowStation (hwinstacurrent))
return FALSE;
if (! SetThreadDesktop (hdeskcurrent))
return FALSE;
if (! Closewindowstation (Hwinsta))
return FALSE;
if (! Closedesktop (Hdesk))
return FALSE;
return TRUE;
}
Running: Compiling the build Guiser.exe, running Guiser/service, and then starting the service in the Control Panel's service, a dialog box will appear!
The above program runs through the Win2000 Professional + VC6.0 + Sp5.0 + Platform SDK 2001.8 compilation Run
If compile does not pass, please download the latest platform SDK
This article supporting source code