wiki:KrakenWinApi

Kraken WinApi

Kraken WinApi wraps the win32 API using JNI (Java Native Interface).

Author

How to Build DLL

  • Open winapi.sln solution file at kraken-winapi/src/main/c/winapi and build all
  • Copy winapi.dll to java.library.path

Download DLL

API Reference

Registry

RegistryKey class has following members:

public class RegistryKey {
	// root keys
	public static RegistryKey currentUser();
	public static RegistryKey localMachine();
	public static RegistryKey users();
	public static RegistryKey performanceData();

	// accessors
	public RegistryKey openSubKey(String path);
	public Object getValue(String name);
	public String[] getSubKeyNames();
	public String[] getValueNames();
	public int getSubKeyCount();
	public int getValueCount();
	public void close();
}

List all name/value pairs in the specified registry key:

RegistryKey key = null;
try {
	key = RegistryKey.currentUser().openSubKey("SOFTWARE\\Microsoft\\Internet Explorer\\Security");
	for (String name : key.getValueNames()) {
		System.out.println(name + ": " + key.getValue(name));
	}
} finally {
	if (key != null)
		key.close();
}

result:
Sending_Security: Medium
Viewing_Security: High
Safety Warning Level: Query

Process

Process contains all process related informations.

public class Process {
	// returns process count
	public static int getProcessorCount();

	// returns all process list
	public static List<Process> getProcesses();

	// calculates cpu usages of all processes and returns <pid, cpu-usage> pairs
	public static Map<Integer, Integer> getCpuUsages(int interval) throws InterruptedException;

	// properties
	public int getPid();
	public String getName();
	public long getPrivilegedProcessorTime();
	public long getUserProcessorTime();
	public long getWorkingSet();
	public long getPagedMemorySize();
	public long getNonPagedMemorySize();
	public long getPageFault();
	public long getPrivateWorkingSet();

	// reload all timestamps and memory counters
	public void refresh();
}

Usage example:

for (Process p : Process.getProcesses()) {
	System.out.println(p);
}

result:
pid=0, name=System Idle Process
pid=4, name=System
pid=2252, name=Dwm.exe
pid=3060, name=Explorer.EXE
pid=3940, name=TSVNCache.exe
pid=4224, name=msnmsgr.exe
pid=1524, name=eclipse.exe
pid=4712, name=javaw.exe
pid=2744, name=conime.exe
pid=156, name=chrome.exe
pid=6736, name=javaw.exe
..omitted..

ARP Cache

ArpCache.getArpEntries() invokes  GetIpNetTable() and returns ARP entries:

public class ArpCache {
	public static ArpEntry[] getArpEntries();

..and ArpEntry has following fields:

public class ArpEntry {
	public int getAdapterIndex();

	public String getAdapterName();

	// mac address
	public byte[] getPhysicalAddress();

	public InetAddress getAddress();

	// Static, Dynamic, Invalid, and Other
	public Type getType();
}

Usage example:

for (ArpEntry entry : ArpCache.getArpEntries()) {
	System.out.println(entry);
}

result:
adapterIndex=1, adapterName=null, type=Static, ip=224.0.0.22
adapterIndex=10, adapterName=Marvell Yukon 88E8055 PCI-E Gigabit Ethernet Controller, type=Invalid, ip=192.168.0.1, mac=00:00:00:00:00:00
adapterIndex=11, adapterName=Intel(R) WiFi Link 5300 AGN, type=Dynamic, ip=192.168.0.1, mac=00:08:9f:4e:d6:b4
adapterIndex=11, adapterName=Intel(R) WiFi Link 5300 AGN, type=Static, ip=192.168.0.255, mac=ff:ff:ff:ff:ff:ff
adapterIndex=13, adapterName=Bluetooth , type=Static, ip=239.192.152.143, mac=01:00:5e:40:98:8f
adapterIndex=16, adapterName=TAP-Win32 Adapter V8, type=Static, ip=224.0.0.22, mac=01:00:5e:00:00:16
..omitted..

Routing Table

RoutingTable has only one method:

public class RoutingTable {
	public static RoutingEntry[] getRoutingEntries();
}

getRoutingEntries() invokes  GetIpForwardTable() and returns all routing entries:

public class RoutingEntry {
	// properties
	public InetAddress getDestination();
	public InetAddress getSubnet();
	public int getPolicy();
	public int getNextHop();
	public InetAddress getInterfaceAddress();
	public int getIfIndex();
	public Type getType();
	public Protocol getProtocol();
	public int getAge();
	public int getMetric1();
	public int getMetric2();
	public int getMetric3();
	public int getMetric4();
	public int getMetric5();

Usage example:

for (RoutingEntry entry : RoutingTable.getRoutingEntries()) {
	System.out.println(entry);
}

result:
destination=0.0.0.0, subnet=0.0.0.0, interface=10.0.0.1
destination=192.168.40.0, subnet=255.255.255.0, interface=192.168.40.1
destination=192.168.40.1, subnet=255.255.255.255, interface=192.168.40.1
destination=192.168.40.255, subnet=255.255.255.255, interface=192.168.40.1
destination=192.168.253.0, subnet=255.255.255.0, interface=192.168.253.1
destination=192.168.253.1, subnet=255.255.255.255, interface=192.168.253.1
destination=192.168.253.255, subnet=255.255.255.255, interface=192.168.253.1
destination=224.0.0.0, subnet=240.0.0.0, interface=127.0.0.1
..omitted..

Event Log

EventLogReader class provides following members:

public class EventLogReader implements Iterable<EventLog> {
	// requires event log source name. e.g. SYSTEM, Application, etc.
	public EventLogReader(String logName);

	// get event log source name
	public String getLogName();

	// fetch all logs at once (takes very long time and needs large memory)
	public List<EventLog> readAllEventLogs();
	public List<EventLog> readAllEventLogs(int begin);

	// fetch event log one by one (slow, but needs low memory)
	public Iterator<EventLog> iterator();
	public Iterator<EventLog> iterator(int next)
}

EventLogReader invokes  ReadEventLog() and returns event logs.

Iterate some windows event logs (from record number 10000):

EventLogReader reader = new EventLogReader("SYSTEM");
Iterator<EventLog> it = reader.iterator(10000);
for (int i = 0; i < 3; i++) {
	System.out.println(it.next());
}

result:
----- Log -----
RecordNumber  : 10000
EventId       : 7036
EventType     : Information
Generated     : Fri Feb 26 04:52:44 KST 2010
Written       : Fri Feb 26 04:52:44 KST 2010
Providername  : Service Control Manager
EventCategory : null
UserSID       : null
User          : null
Message       : The Application Experience service entered the running state.
Data          : 28 bytes
	410065004c006f006f006b00750070005300760063002f0034000000

----- Log -----
RecordNumber  : 10001
EventId       : 7036
EventType     : Information
Generated     : Fri Feb 26 05:08:19 KST 2010
Written       : Fri Feb 26 05:08:19 KST 2010
Providername  : Service Control Manager
EventCategory : null
UserSID       : null
User          : null
Message       : The Application Experience service entered the stopped state.
Data          : 28 bytes
	410065004c006f006f006b00750070005300760063002f0031000000

----- Log -----
RecordNumber  : 10002
EventId       : 7036
EventType     : Information
Generated     : Fri Feb 26 05:09:27 KST 2010
Written       : Fri Feb 26 05:09:27 KST 2010
Providername  : Service Control Manager
EventCategory : null
UserSID       : null
User          : null
Message       : The WinHTTP Web Proxy Auto-Discovery Service service entered the stopped state.
Data          : 44 bytes
	570069006e0048007400740070004100750074006f00500072006f00780079005300760063002f0031000000

Network Connection

IpGlobalProperties.getAllTcpConnections() uses  GetExtendedTcpTable() and IpGlobalProperties.getAllUdpListeners() uses  GetExtendedUdpTable() API.

List all tcp connections:

for (TcpConnectionInformation conn : IpGlobalProperties.getAllTcpConnections()) {
	System.out.println(conn);
}

result:
local=/10.0.1.11:139, remote=/0.0.0.0:0, state=Listen, pid=4
local=/10.0.1.11:11778, remote=/124.40.41.8:80, state=CloseWait, pid=352
local=/10.0.1.11:12218, remote=/124.40.41.8:80, state=CloseWait, pid=7656
local=/10.0.1.11:37006, remote=/74.125.153.19:443, state=Established, pid=2708
local=/10.0.1.11:37316, remote=/74.125.153.19:443, state=CloseWait, pid=6540
local=/10.0.1.11:39217, remote=/74.125.153.139:80, state=CloseWait, pid=3208
local=/10.0.1.11:44802, remote=/74.125.153.138:80, state=Established, pid=2708
local=/10.0.1.11:44906, remote=/74.125.153.105:80, state=Established, pid=2708
local=/10.0.1.11:44914, remote=/72.14.203.102:80, state=Established, pid=2708
local=/10.0.1.11:45097, remote=/202.131.25.24:80, state=Established, pid=2708
..omitted..

List all udp listeners:

for (UdpListenerInformation conn : IpGlobalProperties.getAllUdpListeners()) {
	System.out.println(conn);
}

result:
local=/192.168.253.1:137, pid=4
local=/192.168.253.1:138, pid=4
local=/192.168.253.1:1900, pid=1792
local=/192.168.253.1:5353, pid=1748
local=/192.168.253.1:49581, pid=1792
local=/0:0:7b:0:b804:0:379e:7901:123, pid=1208
local=/0:0:1f4:0:ec03:0:379e:7901:500, pid=1004
..omitted..

CPU Usage

SystemTime invokes  GetSystemTimes() twice with some interval.

public class SystemTime {
	// default interval is 100 milliseconds
	public SystemTime();

	// custom interval in milliseconds
	public SystemTime(int interval);

	// usage = user + kernel percent
	public int getUsage();

	// properties
	public int getIdlePercent();
	public int getKernelPercent();
	public int getUserPercent();
}

Usage example:

System.out.println(new SystemTime());

result:
idle=93, kernel=0, user=7

Memory Usage

MemoryUsage invokes  GlobalMemoryStatus() and returns physical, page, and virtual memory status.

public class MemoryStatus {
	// properties (in bytes)
	public long getTotalPhysical();
	public long getAvailablePhysical();
	public long getTotalPageFile();
	public long getAvailablePageFile();
	public long getTotalVirtual();
	public long getAvailableVirtual();
}

Usage example:

System.out.println(new MemoryStatus());

result:
Physical: 1246367744/2147483647, PageFile: 2659872768/4294967295, Virtual: 1930706944/2147352576

Performance Counter

PerformanceCounter class uses  Performance Counter Functions API.

PerformanceCounter class contains following members:

public class PerformanceCounter {
	// constructors
	public PerformanceCounter();
	public PerformanceCounter(String category, String counter);
	public PerformanceCounter(String category, String counter, String instance);
	public PerformanceCounter(String category, String counter, String instance, String machine);

	// query and close
	public double nextValue();
	public double nextValue(int interval) throws InterruptedException;
	public void close();

	// enumerate all performance machines, categories, instances, and counters.
	public static native String[] getMachines();

	public static String[] getCategories();
	public static String[] getCategories(String machine);
	public static String[] getCategories(DetailLevel detailLevel);
	public static String[] getCategories(String machine, DetailLevel detailLevel);

	public static List<String> getInstances(String category);
	public static List<String> getInstances(String category, String machine);
	public static List<String> getInstances(String category, DetailLevel detailLevel);
	public static List<String> getInstances(String category, String machine, DetailLevel detailLevel);

	public static List<String> getCounters(String category);
	public static List<String> getCounters(String category, String machine);
	public static List<String> getCounters(String category, DetailLevel detailLevel);
	public static List<String> getCounters(String category, String machine, DetailLevel detailLevel);
}

List all performance categories:

for (String category : PerformanceCounter.getCategories()) {
	System.out.println(category);
}

result:
LogicalDisk
PhysicalDisk
Server
Browser
Cache
Processor
Memory
Objects
Paging File
System
Process
Thread
Job Object
Job Object Details
..omitted..

List all performance counters in the specific category:

for (String counter : PerformanceCounter.getCounters("LogicalDisk")) {
	System.out.println(counter);
}

result:
% Free Space
Free Megabytes
Current Disk Queue Length
% Disk Time
Avg. Disk Queue Length
% Disk Read Time
Avg. Disk Read Queue Length
% Disk Write Time
Avg. Disk Write Queue Length
Avg. Disk sec/Transfer
Avg. Disk sec/Read
Avg. Disk sec/Write
Disk Transfers/sec
Disk Reads/sec
Disk Writes/sec
Disk Bytes/sec
Disk Read Bytes/sec
Disk Write Bytes/sec
Avg. Disk Bytes/Transfer
Avg. Disk Bytes/Read
Avg. Disk Bytes/Write
% Idle Time
Split IO/Sec

List all instances in the specific category:

for (String instance : PerformanceCounter.getInstances("LogicalDisk")) {
	System.out.println(instance);
}

result:
HarddiskVolume1
C:
D:
_Total

Create new performance counter and get value:

PerformanceCounter pc = null;
try {
	pc = new PerformanceCounter("LogicalDisk", "Free Megabytes", "C:");
	System.out.println(pc.nextValue());
} finally {
	if (pc != null)
		pc.close();
}

result:
16355.0

See also

History

  • 1.0.0 release (2010-12-24)