Java/C++ PC Standby Detect and Prevent

Introduction

This article shows how your Java (and C++) program can detect and refuse sleep/standby/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. Therefore, I decided to use the Windows API to detect whether 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_DENY from this message handler.

By using this API, the enclosed code is able to detect whether 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 StandByDetector object 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 whether or not you want to allow entering a standby. 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 the following on the command line:
  3. javac com\ha\common\windows\*.java 
  4. Then, to run the demo, issue on the command line:
  5. java -Djava.library.path=C:\temp\com\ha\common\windows -classpath
    c:\temp\com.ha.common.windows.StandByDetector
    
  6. Now try to put the PC in standby: Task manager->Shut Down->Stand By.
  7. You will note that the cmd shell shows the message "standby requested" and the PC does not go into standby.


Downloads

Comments

  • PC Standby mode in windows 7

    Posted by xyz on 01/20/2014 08:36am

    Does this works on windows 7. I tried downloading and using the code on my laptop having windows 7. I get a dialog box. But when i put the laptop to sleep mode . It goes to sleep mode. How do i test this on windows 7 laptop or it would be of great help if i let to know if any changes are required to be done. Thank You

    Reply
  • Beautiful Color Og Limited Ghd Rettetang gjøre kvinner vakrere

    Posted by carpinteyroygu on 06/14/2013 04:02pm

    [url=http://www.rettetangnewsnorge.com/ghd-omsorg]ghd rettetang pris[/url] Med GHD IV mini styler kan man lage krøller, vipper og bølger så vel som slett hår. Det ideelle styling redskap for deg med kort hår, pannelugg eller også for menn.Med et mer elegant design og gyllne plater, som er glatte og uformet, så de lett glir igjennom håret og gir et ultra skinnende resultat. Med det forbedrede ytre skall, er stylerne kjøligere å berøre og lettere å håndtere. [url=http://ghdrettetangtilbud1.npage.de/]Rettetang ghd[/url] GHD hår straighteners er all-round stylers som forlater håret utrolig vakkert og beundringsverdig. Disse elementene er så portabel og har noen egenskaper som gjør dem svært behagelige å reise med eller bære rundt. Dessuten gjør rettetangen en god gave til en kvinne på hennes bursdag og blir virkelig verdsatt av alle som mottar den som gave. Overrask noen behagelig på hennes bursdag med GHD Precious Gift Set. [url=http://ghdrettetangtilbud.qsite.dk/]ghd rettetang[/url] GHD rettetang er en av verdens mest populære merkene. Unikt keramisk teknologien varmer DetteGHD rettetang opp til sin maksimale temperaturen i sekunder. GHD flatt jern bruker langt infrarød varme for sikker resultater som bevarer hårets naturlige fuktigheten og glans, gir den salongen slank ferdig med nesten ingen anstrengelse. Opprette stramt krøller, vipper og bølger, i tillegg til perfekt rett linje. Den ideelle styling verktøy for urban engler med kort hår, middels og lang håret.

    Reply
  • You crave some tomato basil and mozzarella. For indoor power, these slippers are as liven up and manueverable as sneakers.

    Posted by Soaceddew on 04/20/2013 02:10am

    Has honourable released several mod color Let off Inneva Woven shoes, Nike recently with another way to lure shoes with distinguishable styling to all [url=http://northernroofing.co.uk/roofins.cfm]nike free[/url] eyes. This brings special edition Pardon Inneva Woven is a Chaste Name of works in the series, represents shoes Italian made the assurance. Latest Allowed Inneva Woven clouded and bawdy are readily obtainable in two color schemes, to hand-knit Woven vamp in addition to infiltrated Italy's [url=http://markwarren.org.uk/goodbuy.cfm]nike free run[/url] finest crafts, during the interval gives athletes arrange to the foot of relieve, the most important opportunity is the intent of Unused 5 configuration, barefoot be aware it will contribute to cannot be ignored. Nike Disburden Inneva Woven SP Pale-complexioned Characterization Order off on Walk 16 at outlets on all sides the [url=http://northernroofing.co.uk/roofins.cfm]nike free uk[/url] trade-mark on the shelves, and on bargain-priced in restricted tone, interested friends should settle fasten attention to Nike announced the news.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds