0

I've imported the JNA libary for usage in my Java project. I have this Kernel32 class that allows me to use the library in my project:

package winsot;

import java.util.ArrayList;
import java.util.List;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;

public interface Kernel32 extends StdCallLibrary {

public Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("Kernel32", Kernel32.class);

/**
 * @see http://msdn2.microsoft.com/en-us/library/aa373232.aspx
 */
public class SYSTEM_POWER_STATUS extends Structure {
    public byte ACLineStatus;
    public byte BatteryFlag;
    public byte BatteryLifePercent;
    public byte Reserved1;
    public int BatteryLifeTime;
    public int BatteryFullLifeTime;

    @Override
    protected List<String> getFieldOrder() {
        ArrayList<String> fields = new ArrayList<String>();
        fields.add("ACLineStatus");
        fields.add("BatteryFlag");
        fields.add("BatteryLifePercent");
        fields.add("Reserved1");
        fields.add("BatteryLifeTime");
        fields.add("BatteryFullLifeTime");
        return fields;
    }

    /**
     * The AC power status
     */
    public String getACLineStatusString() {
        switch (ACLineStatus) {
            case (0): return "Offline";
            case (1): return "Online";
            default: return "Unknown";
        }
    }

    /**
     * The battery charge status
     */
    public String getBatteryFlagString() {
        switch (BatteryFlag) {
            case (1): return "High, more than 66 percent";
            case (2): return "Low, less than 33 percent";
            case (4): return "Critical, less than five percent";
            case (8): return "Charging";
            case ((byte) 128): return "No system battery";
            default: return "Unknown";
        }
    }

    /**
     * The percentage of full battery charge remaining
     */
    public String getBatteryLifePercent() {
        return (BatteryLifePercent == (byte) 255) ? "Unknown" : BatteryLifePercent + "%";
    }

    /**
     * The number of seconds of battery life remaining
     */
    public String getBatteryLifeTime() {
        return (BatteryLifeTime == -1) ? "Unknown" : BatteryLifeTime + " seconds";
    }

    /**
     * The number of seconds of battery life when at full charge
     */
    public String getBatteryFullLifeTime() {
        return (BatteryFullLifeTime == -1) ? "Unknown" : BatteryFullLifeTime + " seconds";
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("ACLineStatus: " + getACLineStatusString() + "\n");
        sb.append("Battery Flag: " + getBatteryFlagString() + "\n");
        sb.append("Battery Life: " + getBatteryLifePercent() + "\n");
        sb.append("Battery Left: " + getBatteryLifeTime() + "\n");
        sb.append("Battery Full: " + getBatteryFullLifeTime() + "\n");
        return sb.toString();
    }
}

/**
 * Fill the structure.
 */
public int GetSystemPowerStatus(SYSTEM_POWER_STATUS result);
}

I am trying to retrieve battery status details, so I run this code in another class:

Kernel32.SYSTEM_POWER_STATUS batteryStatus = new Kernel32.SYSTEM_POWER_STATUS(); //new instance of class
        System.out.println(batteryStatus);

However it seems that the JNA library is unable to access the battery information from Windows, as System.out.println(batteryStatus) returns this:

ACLineStatus: Offline
Battery Flag: Unknown
Battery Life: 0%
Battery Left: 0 seconds
Battery Full: 0 seconds

Does the JNA library no longer work under Windows 10, or am I missing something?

Thanks.

  • It works on another Windows Version? – deFreitas Nov 19 '17 at 06:43
  • I can't find any information on it explicitly not working on Windows 10, but yes, I've seen it used on Windows 7, Vista, etc. In fact the library is still being updated (as seen on their github) which would lead me to assume it should support Windows 10 – Kevin Shroff Nov 19 '17 at 21:26
  • Your code is fine and it works on Windows 10 if you actually call `Kernel32.INSTANCE.GetSystemPowerStatus(batteryStatus);` after instantiating it. – Daniel Widdis Aug 05 '19 at 04:50

0 Answers0