Why does GetWindowThreadProcessId return 0 when called from a service?

后端 未结 3 442
再見小時候
再見小時候 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:44

    You could also enable the option "Allow service to interact with desktop" and see if that works. Otherwise I would have to agree with onbugz comment above.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-01 05:55

    A Windows Service has no UI, so it has no window.

    0 讨论(0)
提交回复
热议问题