[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-04-24
23
Medium Priority
?
346 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:MusicBuddha
  • 9
  • 7
  • 3
  • +1
20 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 16530782
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
 

Author Comment

by:MusicBuddha
ID: 16531013
>>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
 
LVL 86

Expert Comment

by:jkr
ID: 16531049
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:MusicBuddha
ID: 16531191
Where do I get the DLL ?
0
 

Author Comment

by:MusicBuddha
ID: 16531195
And then after I get the dll do I just add some code like


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

??
0
 
LVL 86

Expert Comment

by:jkr
ID: 16531214
>>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
 

Author Comment

by:MusicBuddha
ID: 16531258
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
 
LVL 86

Expert Comment

by:jkr
ID: 16531289
Well, start a Win32 DLL project and you're almost there.
0
 

Author Comment

by:MusicBuddha
ID: 16531665
Ok I've created the shell of a DLL in CBuilder. What's next ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 16535789
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
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 1000 total points
ID: 16548159
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
 
LVL 86

Expert Comment

by:jkr
ID: 16548222
Dan,

that was covered in MusicBuddha's previous Q on this issue.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 16548828
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
 

Author Comment

by:MusicBuddha
ID: 16716163
>>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
 
LVL 20

Expert Comment

by:Venabili
ID: 16716172
Sure
In the future - make sure you answer to the experts stating that you work on their suggestions :)
0
 

Author Comment

by:MusicBuddha
ID: 16881969
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 16888868
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
 
LVL 86

Expert Comment

by:jkr
ID: 16889757
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
 

Author Comment

by:MusicBuddha
ID: 16892943
>>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
 

Author Comment

by:MusicBuddha
ID: 16916034
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question