So I have an application and I dont want the screensaver to run while my application is running. This has turned out to be very complicated. One solution is to disable and then re-enable the screensaver but I dont like this because if the application terminates suddenly your screensaver is still switched off, and on some computers (like mine) you cant change the screensaver settings even in admin mode.<br/><br/>My solution has been to try to implement a global hook to catch those messages and ignore them. From what Ive read if you want to do this you have to put the hook callback in a seperate unmanaged C/C++ dll so that it can be injected into all running processes, which I did. The C# library then sets up the callback like so:<br/><br/>hDll = LoadLibrary(DLL_NAME);<br/>IntPtr hookProc = GetProcAddress(hDll, PROC_NAME);<br/>callbackDelegate = (HookProc)Marshal.GetDelegateForFunctionPointer(hookProc, typeof(HookProc));<br/><br/>This part seems to work as both hDll and hookProc are not 0. Then the hook is set up like so:<br/>hHook = SetWindowsHookEx(HookType.WH_GETMESSAGE, callbackDelegate, hDll, 0);<br/><br/>And finally heres the actual callback in C++:<br/>LRESULT HookProc(int code, WPARAM wParam, LPARAM lParam)<br/>{<br/> if (code == WM_SYSCOMMAND)<br/> {<br/> if (wParam == SC_SCREENSAVE || wParam == SC_MONITORPOWER)<br/> return 0;<br/> else<br/> return CallNextHookEx(NULL, code, wParam, lParam);<br/> }<br/> else<br/> return CallNextHookEx(NULL, code, wParam, lParam);<br/>};<br/><br/>The problem is that it doesnt stop the screensaver from showing. Im fairly sure it works for messages in its own process, because when I compiled with CLR I could reference it from my other projects and see it receiving messages. Unfortunately I had to compile non-CLR so it will work with all processes, which means I have to manually copy and paste into each project and cant debug it.<br/><br/>I dont know what else to try, it seems like everything is set up the way it should be. All I want is for the screensaver to not come on while my application is running. Seems like a simple request, but its turning out to be very hard to do cleanly. If anyone has any suggestions it would be much appreciated. Heres a link to the solution:<br/><br/> http://www.easy-share.com/1905169352/ScreensaverControl.zip http://www.easy-share.com/1905169352/ScreensaverControl.zip
View the full article
View the full article