Click here to Skip to main content
11,647,827 members (63,362 online)
Click here to Skip to main content

PC StandBy Detector for Java Apps

, 31 Mar 2004 CPOL 44.5K 935 20
Rate this:
Please Sign up or sign in to vote.
Description: Shows how to detect if the computer is going to go into sleep (stand-by) mode and allow/disallow it

Introduction

This article shows how your JAVA (and C++) program can detect and refuse sleep/stand-by/hibernate requests made by the user or the system.

Background

I wrote an application that is fed by a special PCI card, and runs 24/7. We found that if the PC went into standby mode, the PCI card was falsely activated, which was really bad. There was no easy way to uniformly disable the sleep/standby/hibernate features in Windows XP and therefore I decided to use the Windows API to detect if a standby request is made and refuse it. Luckily, the Win32 API has a message called WM_POWERBROADCAST that is sent to all applications when the PC wants to go into standby or hibernate mode. The win32 subsystem will abort the standby if you return BROADCAST_QUERY_DENYfrom this message handler.

By using this API, the enclosed code is able to detect if the computer goes into standby mode and through the utilization of JNI, notify the rest of the Java code about that event. Also, the code lets you allow or disallow the PC to enter standby mode.

Using the Code

To use the code from JAVA, simply instantiate a StandByDetectorobject and provide the constructor with a reference to a StandByRequestListener. The listener will be notified when a standby/hibernate request is made on the PC. Use the setAllowStandby()to tell the code if you want to allow entering a standby or not. Remember that the DLL (enclosed) has to be in your java.library.path path. Here is an example:

StandByDetector sd=new StandByDetector(new StandByRequestListener() {
    public void standByRequested() {
        System.out.println("standby requested");
    }
});
sd.setAllowStandby(false);

To compile and run the demo:

  1. Extract the zip file contents into c:\temp preserving the zip dir structure.
  2. Open cmd and cd into temp. Then issue on the command line:

    javac com\ha\common\windows\*.java
  3. Then, to run the demo, issue on the command line:

    java -Djava.library.path=C:\temp\com\ha\common\windows 
        -classpath c:\temp\com.ha.common.windows.StandByDetector
  4. Now try to put the PC in standby - Task manager -> Shut Down -> Stand By
  5. You will note that the cmd shell shows the message "standby requested" and the PC does not go into standby.

History

  • Initial version: 4-1-2004

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tomer Petel
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralNot working on windows 7 Pin
Suhas Bachewar14-Oct-13 0:41
memberSuhas Bachewar14-Oct-13 0:41 
GeneralRe: Not working on windows 7 Pin
Member 1053094620-Jan-14 7:15
memberMember 1053094620-Jan-14 7:15 
QuestionCorrection to the execution command Pin
Priyan19874-Jul-13 21:05
memberPriyan19874-Jul-13 21:05 
GeneralThis worksfor vb.net also Pin
slushy1000029-Nov-07 4:55
memberslushy1000029-Nov-07 4:55 
Generaljava project Pin
Anonymous4-Feb-05 23:11
sussAnonymous4-Feb-05 23:11 
Generalthanks Pin
Jerry Gao1-Feb-05 11:33
memberJerry Gao1-Feb-05 11:33 
I added a Standby detector thread into my Outlook addin, and enabled the addin to detect the standby. Your article saved me a lot of time.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150804.4 | Last Updated 1 Apr 2004
Article Copyright 2004 by Tomer Petel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid