Java/C++ PC Standby Detect and Prevent | CodeGuru

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, […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 6, 2004
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

Advertisement

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 comhacommonwindows*.java 
  4. Then, to run the demo, issue on the command line:
  5. java -Djava.library.path=C:tempcomhacommonwindows -classpath
    c:tempcom.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.
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.