Interacting with the user in a service

zhaozj2021-02-16  65

For a noninteractive service application to interact with the user, it must open the user's window station ( "WinSta0") and desktop ( "Default"). By default, only the logged-on user and service applications running in the LocalSystem account are granted access to the user's window station and desktop. This means that services running in other accounts must either impersonate the user when opening the interactive window station and desktop, or have access granted to those accounts by the user.

For compatibility with versions of Windows that support multiple users and / or multiple desktops per user, the user's window station and desktop names should not be hard-coded. Client applications should pass the names of their window station and desktop to the service as part of The Request to the Service.

This code sample displays a message on the user's desktop in response to an RPC request from one of the user's applications. Note that a global variable dwGuiThreadId in this example is also used in the logoff handling example. If the thread that displays the message box terminates After IT IS Done with The Message Box, IT IS Not Necessary To Save and Restore The Thread's Desktop. It is Necessary To Restore The Process's Window Station.

DWORD dwGuiThreadId = 0; int UserMessageBox (RPC_BINDING_HANDLE h, LPSTR lpszWindowStation, LPSTR lpszDesktop, LPSTR lpszText, LPSTR lpszTitle, UINT fuStyle) {DWORD dwThreadId; HWINSTA hwinstaSave; HDESK hdeskSave; HWINSTA hwinstaUser; HDESK hdeskUser; int result; // Ensure connection to Service Window Station and Desktop, And // Save Their Handles.

GetDesktopWindow (); hwinstaSave = GetProcessWindowStation (); dwThreadId = GetCurrentThreadId (); hdeskSave = GetThreadDesktop (dwThreadId); // Impersonate the client and connect to the User's // window station and desktop.RpcImpersonateClient (h); hwinstaUser = OpenWindowStation (lpszWindowStation , FALSE, MAXIMUM_ALLOWED); if (hwinstaUser == NULL) {RpcRevertToSelf (); return 0;} SetProcessWindowStation (hwinstaUser); hdeskUser = OpenDesktop (lpszDesktop, 0, FALSE, MAXIMUM_ALLOWED); RpcRevertToSelf (); if (hdeskUser == NULL ) {SetProcessWindowStation (HwinStasave); ClosewindowStation (HWINSTAUSER); RETURN 0;} setthreadDesktop (hdeskuser); // Display Message Box.

DWGUITHREADID = DWTHREADID; Result = MessageBox (NULL, LPSZTEXT, LPSZTILE, FUSZTYLE); DWGUITHREADID = 0; // Restore Window Station and Desktop.

SetthreadDesktop (HDesksave); setProcessWindowStation (HwinStasave); CloseDesktop (HDESKUSER); ClosewindowStation (HwinStause); Return Result;

转载请注明原文地址:https://www.9cbs.com/read-20763.html

New Post(0)