How to notify an applcation when the screensaver is turned on or off.

Hi,

How can I set my program up so that the app can be notified when the Screensaver gets turned off and then again when it comes on?

Somebody sugggested the following but I don't know how to do it.

>>"In the appliation message loop you can listen to the WM_SYSCOMMAND message with wParam as SC_SCREENSAVE to know when the screen saver is being invoked."<<

>>"System sends this message to all the running applications just before the invocation of ScreenSaver."<<

Can someone please give me a working example of this .

I am using CBuilder and already have a VCL Messagemap defined so if you can give it to me for CBuilder that would be good. But I can probably translate it from C++ it you can give it to me in that.

Thanks,

Buddha
MusicBuddhaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
This seems to be an ideal candidate for Winlogon Notification Packages (http://msdn.microsoft.com/library/en-us/secauthn/security/winlogon_notification_packages.asp). These are DLLs that export certain functions that will be called when Winlogon encounters e.g. a 'StartScreenSaver' or 'StopScreenSaver' event. These functions are set by registry entries as described in http://msdn.microsoft.com/library/en-us/secauthn/security/registry_entries.asp ("Registry Entries"), you'll need

StartScreenSaver=MyStartScreenSaverProc;
StopScreenSaver=MyStopScreenSaverProc;

You'll find sample code at http://msdn.microsoft.com/library/en-us/secauthn/security/event_handler_function_prototype.asp ("Event Handler Function Prototype")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MusicBuddhaAuthor Commented:
>>StartScreenSaver=MyStartScreenSaverProc;
>>StopScreenSaver=MyStopScreenSaverProc;

Can you give me a working example of how this should actually look based on the example code given below but changed to suit for  the screensaver?




// Here is the event handler for the Winlogon Logon event.
VOID MyStartScreenSaverProc (PWLX_NOTIFICATION_INFO pInfo)
{

    // Print the name of the handler to debug output.
    // You can replace this with more useful functionality.
    OutputDebugString (TEXT("NOTIFY:  Entering WLEventLogon.\r\n"));
}

// Here is the event handler for the Winlogon Logoff event.
VOID MyStopScreenSaverProc (PWLX_NOTIFICATION_INFO pInfo)
{

    // Print the name of the handler to debug output.
    // You can replace this with more useful functionality.
    OutputDebugString (TEXT("NOTIFY:  Entering WLEventLogff.\r\n"));
}
0
jkrCommented:
Well, these are just the registry entries, so the .reg file looks like

-------------------------->8------------------------
REGEDIT4

[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\MyNotify]
"StartScreenSaver"="MyStartScreenSaverProc"
"StopScreenSaver"="MyStopScreenSaverProc"
-------------------------->8------------------------

Save that as MyNotify.reg and register it by double-clicking, make sure that the DLL is called MyNotify.DLL.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

MusicBuddhaAuthor Commented:
Where do I get the DLL ?
0
MusicBuddhaAuthor Commented:
And then after I get the dll do I just add some code like


void MyStartScreenSaverProc()
{
ShowMessage("Screen Start");
}

??
0
jkrCommented:
>>Where do I get the DLL ?

Err, you need to create it ;o)

>>And then after I get the dll do I just add some code like

No, you'll have to find a way to communicate that to your application, but that's one step after creating the DLL.
0
MusicBuddhaAuthor Commented:
Ok I need some serious help here. I don't mind putting the points up to 500.

I have never created a DLL.  How would I go about it. I do have a copy of MS Vis Studio Enterprise 6 here which will prolly make it easier for me to use from your perspective maybe?

What do you think?
0
jkrCommented:
Well, start a Win32 DLL project and you're almost there.
0
MusicBuddhaAuthor Commented:
Ok I've created the shell of a DLL in CBuilder. What's next ?
0
jkrCommented:
You'll need to export the functions

extern "C"
__declspec(dllexport)
void MyStartScreenSaverProc()
{
OutputDebugString("Screen Start");
}

extern "C"
__declspec(dllexport)
void MyStopScreenSaverProc()
{
OutputDebugString("Screen Stop");
}

Build the DLL and place it in the system32 directory, then add the registry entries and restart your machine. You'll be able to see the output in e.g. DebugView (http://www.sysinternals.com/Utilities/DebugView.html)
0
DanRollinsCommented:
One common way for a running program to detect screensaver startup without needing to modify the registry (though otherwise, jkr's suggestion looks like a good one, especially for triggering actions in code that is not already running), is to install a WH_GETMESSAGE Windows Hook:

    Ask Dr. GUI #48
    http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraskdr/html/drgui48.asp

Screensaver shutdown?  I don't know... But I'd guess that you could probably use the same windows hook to monitor for any WM_AACTIVATE event or mouse move.  Or you could use a timer to poll...

    SystemParametersInfo( SPI_GETSCREENSAVERRUNNING,... )

...every so often.   Polling is not always best, but in this cas it might be OK -- You wouldn't need to worry much about wasted CPU cycles because the computer is probably mostly idle anyway.

-- Dan
0
jkrCommented:
Dan,

that was covered in MusicBuddha's previous Q on this issue.
0
DanRollinsCommented:
I see nothing about hooks in that question... I think it is a common misconception that all windows will recieve a SC_SCREENSAVE, but it states clearly here:

   Handling Screen Savers
   http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/programmersguide/shell_adv/scrnsave.asp

...that only the active application will get the notification.  Thus, if you want to be notified, but you are not active, you will be left in the dark.  Using a hook, you can do an HWND_BROADCAST of a registered message and that will ensure that you get a notification message.  There is additional info here:

     How To Know When Your Screen Saver Starts
     http://support.microsoft.com/?kbid=238882

=-=-=-=-=-=-=
I'm just describing options.  
I know that I personally would rather not add registery entries and use functionality that is not avialable for Win98 if I found a clear-cut alternative.  That's why I posted.

-- Dan
0
MusicBuddhaAuthor Commented:
>>Administrative Comment from Venabili

Hi,

Please leave this question open, I have not had time recently to try JKRs latest suggestion. I should be able to get to it within another 21 days.

Thanks,

MB
0
VenabiliCommented:
Sure
In the future - make sure you answer to the experts stating that you work on their suggestions :)
0
MusicBuddhaAuthor Commented:
DanRollins

>> How To Know When Your Screen Saver Starts
>>     http://support.microsoft.com/?kbid=238882

I am using Borland CBuilder 5. Do you know how I can modify that code to use it from within Builder 5 ? as it is it throws up all kinds of compile errors

Thanks,

MB
0
DanRollinsCommented:
Sorry, I don't have Builder availalble for testing.  Looking at that code, it looks like pure Win32 API calls, which any WIndows-capable compiler should be able to handle without errors.
-- Dan
0
jkrCommented:
Thank you for coming back so early, it is always nice to know that someone pays attention to their questions. I assume that because of all that attention you forgot to test the notification package?
0
MusicBuddhaAuthor Commented:
>>Thank you for coming back so early,

Sorry for taking so long with this one. You absolutely would not believe the last two months that I have had. Firstly my computer and all the backups of it that I had across a network became corrupted by a virus. So I lost all the work I had done on this program (same prog all my questions on experts have been about) including the start I had done on your suggestion with the reg entries and the DLL etc.

Then I became very ill myself for some time and was not able to do anything at all. By the time I had caught up with all the work I had to do to get my system running again and catching up on my business work and getting this pogram back to the section where I needed the screensaver notification, DanRrollins solution just looked more appealing given that as you know I have never written a DLL, don't even know how to link it into my program yet and I am not even really sure what it does ....... I could go on but to put it simply life has just been a real bitch!! Oh I forgot .... in amogst there somewhere I lost another week tracking down a faulty ram simm in my machine.

So whilst I beg you for your forgiveness at my inattention to your solution, there have been mitigating circumstances that I hope you will take into account.

If you are still willing I would like to try the notification package, I'll be working on it later tonight and will probably have a bunch of questions as I stumble over everything!

MB

0
MusicBuddhaAuthor Commented:
Jkr

Are you still able to assist me with this question even though it has been closed whilst I have stated that i am still working on it ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.