### Access Windows Libraries in Java

This is an example of accessing the Windows Kernel32 using Java Native Access (JNA).
Information JNA:
https://jna.java.net/javadoc/platform/overview-summary.html
Principle
The Windows Kernel32 is accessed by defining an interface.
Based upon this interface, a class is defined to access the functions from the Windows Kernel32 library instance.
Information about Windows Kernel32 usage for Java:
https://jna.java.net/javadoc/platform/com/sun/jna/platform/win32/Kernel32.html
Requirements:
Java Native Access (JNA) is a must required. Download JNA version 4.1.0:
https://github.com/twall/jna
In the meantime newer versions might be available.
The JNA API Documentation:
http://twall.github.io/jna/4.1.0/
Notes
Not everything required to develop libraries is explained here.
If you want to know more on how to develop a library, read "How to create a B4J Library using Java".
Set up Java Class Path and B4J Additional Library Folder
For Java Development:
Ensure to copy the JNA file (in this case jna-4.1.0.jar) in your class path.
Ensure JRE and B4J jCore are in the class path.
Example
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="C:/Daten/b4j/libraries/jna-4.1.0.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="C:/Program Files (x86)/Anywhere Software/B4J/Libraries/jCore.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Ensure the BADocLet is installed.
For B4J Development:
Copy the file jna-4.1.0.jar to the B4j additional libraries folder
Add to the Project Attributes in the B4J Project File: #AdditionalJar: jna-4.1.0
IMPORTANT for development
Add @Hide to all the types that should not be exposed. In this case for the interface Kernel32.
If this is not the case, then error messages, like duplicate type autowrite etc. occur.
Acknowledgement:
See http://stackoverflow.com/questions/3434719/how-to-get-the-remaining-battery-life-in-a-windows-system
Example retrieving the Battery state of a Laptop running Windows 8.1.
Define Interface to Kernel32
@Hide
@BA.DesignerName("Build 20140727")
@BA.Version(1.02F)
@BA.Author("Robert Linn")
@BA.ShortName("jRLWinBatteryInfo")
@BA.DependsOn(values={"jna-4.1.0"})
public interface Kernel32 extends StdCallLibrary {
@Hide
Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class);
@Hide
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
@Hide
protected List getFieldOrder() {
@Hide
ArrayList fields = new ArrayList();
fields.add("ACLineStatus");
fields.add("BatteryFlag");
fields.add("BatteryFullLifeTime");
fields.add("BatteryLifePercent");
fields.add("BatteryLifeTime");
fields.add("Reserved1");
return fields;
}
}
/**
* Fill the structure.
* */
@Hide
public int GetSystemPowerStatus(SYSTEM_POWER_STATUS result);
}
Define Public Functions to be used by B4J
Note: Only one example is given.
/**
* The AC power status
*/
public String getACLineStatusString() {
Kernel32.SYSTEM_POWER_STATUS batteryStatus = new Kernel32.SYSTEM_POWER_STATUS();
Kernel32.INSTANCE.GetSystemPowerStatus(batteryStatus);
switch (batteryStatus.ACLineStatus) {
case (0): return "Offline";
case (1): return "Online";
default: return "Unknown";
}
}
Compile ensuring jar and xml file are in the B4J additional libraries folder.
Create B4J Project
Create a B4J Project or amend existing B4J Project.
In the B4J IDE Library Manager, this new B4J Library should be listed jRLWinBatteryInfo.
To use a method, define and execute
Dim wbi As jRLWinBatteryInfo
Log("ACLineStatus: " & wbi.ACLineStatusString)
Hints
Using System.out.println() in the Java Library will show the output in the B4J Log or in the
command line window (when executing via java -jar jrlwinbatteryinfoex.jar

Changed: 2018-06-07 09:15, Category: Libraries, File: T6C214M1554.htm, ID: 1554