2

I have ran into an issue when retrieving the number of active monitors while the exe is running as a windows service. I have tried using EnumDisplayDevices and GetSystemMetrics(SM_CMONITROS) to get the number of monitors, these two methods woulds give me the correct number of monitors when running them as console mode (meaning initiated by the user), but when I register the exe as a service and run it through the windows service, the number of monitors that was reported turned out to be incorrect.

Is there any other way to determine the number of active monitors while running as a windows service? Or any other work around? Thanks in advance!

Jet Chung
  • 21
  • 1
  • Why do you need this? Your service shouldn't be displaying UI, so why does it need to know this? – MSalters Sep 05 '13 at 08:32
  • The service acts like a status monitor that monitors the amount of displays that are currently active and report the number of the displays back to a server. – Jet Chung Sep 06 '13 at 16:16

1 Answers1

0

Services are not allowed to interact with the user desktop for security reason. Therefore they are running in their virtual desktop which has nothing to do with the physical one. You can try the following. In the service list of the service manager. Rightclick your service, go to properties and there to "Log On". There you can change the account the service is using. With the "Local System account" you can check the "Allow service to interact with desktop". I didn't try that my self. But for interacting the service has to get the access to the "real" desktop and therefore the monitor count should be right.

Martin Schlott
  • 4,369
  • 3
  • 25
  • 49
  • Thank you Martin for your answer, but I have just tried your suggestion but the problem still exist. I'm suspecting the issue lies in the area of "Log On As", but I can't be for certain. I have tried using my local user account as the log on account and password. but the problem still exist. Maybe I should find a work around to find the number of monitors. But it's hard to believe that there isn't any easier way to do this via the services :( – Jet Chung Sep 04 '13 at 15:45
  • Sorry that this did not help. The main problem is that you need a process running in the user desktop, which is than communicating with your service. I did that years ago with a named pipe, I think this is also in these days the proper way. But I think that you already thought in this way. – Martin Schlott Sep 04 '13 at 15:49
  • Yes, that is the next step unfortunately, I would have my service launches another process which will live in the user desktop, and that process would be able to get the number of active monitors and report back to my service via my communication mechanism. But I will leave this question up to see if any other people thought of a way of doing it within the windows service itself. Thanks again Martin! – Jet Chung Sep 04 '13 at 15:57