0

Is it anyhow possible to get the handle (hWnd) for window in a different user session?

I know there is no easy way, maybe it is impossible, but maybe someone knows how this can be done

Markus1980Wien
  • 471
  • 1
  • 5
  • 15
  • I don't see what you would use it for. Unlike kernel objects, user window objects are local to a session, and the handle for a window cannot be duplicated. Also, in practice only threads on the same desktop can post and send messages to each other, which means their processes have to be connected to the same window station, which is only accessible by processes in the same session as the window station. – Eryk Sun Jul 24 '19 at 19:15
  • The use case for this requirement is a window that should be visible on the lock-screen. Every time a user locks his workstation, a dialog should appear (on the lock-screen) which asks him "Do you also want to logout in the time-tracking app" I was able to accomplish this with a "sheduled task" that starts whenever the workstation is locked. This sheduled task starts an application (as current logged on user) which displays a MessageBox with MB_SERVICE_NOTIFICATION flag set. This way the MessageBox is visible on the lock-screen. But I want to close the messageBox automatically after 30 secs. – Markus1980Wien Jul 24 '19 at 21:34
  • And for automatically closing the messageBox after 30 seconds, I need to be able to send the "No-Button" was clicked to the message box. Therefore I need the Handle for the button. Which works totally fine, if the MessageBox is visible on a normal screen, but does not work if the MessageBox is visible on the LockScreen, because I am not able to find the handle for the button. – Markus1980Wien Jul 24 '19 at 21:43
  • I just found out, that there is a undocummented MessageBoxTimeout function in User32.dll. I think with this function I can solve the "Do you also want to logout in the time-tracking-app" problem – Markus1980Wien Jul 24 '19 at 22:27
  • The lock screen is on the Winlogon secure desktop, which is not necessarily in another session, depending on the sequence of events (e.g. just lock vs lock, switch to another user's session, and lock). – Eryk Sun Jul 24 '19 at 22:32

1 Answers1

2

You cannot use HWNDs across session boundaries. So no, you cannot obtain an HWND for a window in another session.

If you need to do something in another session, you will have to start a new process inside that session and let that process do the work you need, and then it can communicate back to your process if needed, using a suitable IPC mechanism such as a pipe, socket, etc.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770