Why does GetWindowThreadProcessId return 0 when called from a service?

后端 未结 3 462
再見小時候
再見小時候 2021-01-01 05:13

When using the following class in a console application, and having at least one instance of Notepad running, GetWindowThreadProcessId correctly returns a non-z

3条回答
  •  感动是毒
    2021-01-01 05:52

    A service runs in its own session, the infamous session 0 in Vista and Win7. That session isolates services from the user desktop, it runs in another session. Specifically to prevent a service that usually runs with a very privileged account (like LocalSystem) from interacting with the user. A security hole.

    Accordingly, a service cannot see the window handles owned by another session.

    Not sure why you are doing this but you typically need a helper program that presents a user interface and communicates with the service through an IPC mechanism like named pipes, sockets, .NET remoting or WCF. If you use a named pipe, prefix the pipe name with "Global\" so all sessions can see it.

提交回复
热议问题