开发者

Turning display off from Windows Service

I have written a Windows service which should turn the display OFF when certain conditions occur. The code I am using is :

private const int MONITOR_OFF = 2;
SendMessage(GetDesktopWindow().ToInt32(), 
   WM_SYSCOMMAND, SC_MONITORP开发者_运维技巧OWER, MONITOR_OFF);

This same code works from a Windows application but when executed from Windows service, nothing happens. There is no exception either and Marshal.GetLastWin32Error() also returns 0.

Any ideas on what I might be missing?


Because a service has no default access to the desktop, so your call to GetDesktopWindow() is returning something other than you expect; you then blindly convert that to an Int32 and use that as a window handle target of SendMessage(). Since the recipient probably isn't a valid window, it doesn't do anything.


You can try editing the properties of the service, in the services management utility to allow desktop interaction.


Services run in their own virtual desktop space (they can even have their own user profile space.) It is part of the security model built into windows. Your best bet would be to have a service that runs in the background and a user mode client that runs in the users desktop. Depending on the level of access of the user would make it possible for the user to kill the service/user mode client. But you could also query the processes to make sure the user app is still running.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜