Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetPerformanceInfo(), GetTickCount64(), and SetErrorMode() #640

Merged
merged 3 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Features
* [#621](https://github.com/java-native-access/jna/pull/621): Added TYPEFLAGS-constants for `wTypeFlags` in `com.sun.jna.platform.win32.OaIdl.TYPEATTR` - [@SevenOf9Sleeper](https://github.com/SevenOf9Sleeper).
* [#625](https://github.com/java-native-access/jna/pull/625): Make conversion to/from java to/from VARIANT in `com.sun.jna.platform.win32.COM.util.Convert` more flexible and dependable - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#639](https://github.com/java-native-access/jna/pull/639): Add getloadavg() to OS X and Unix - [@dbwiddis](https://github.com/dbwiddis).
* [#640](https://github.com/java-native-access/jna/pull/640): Add `com.sun.jna.platform.win32.Psapi.GetPerformanceInfo()`, `com.sun.jna.platform.win32.Kernel32.GetTickCount64()`, and `com.sun.jna.platform.win32.Kernel32.SetErrorMode()` - [@dbwiddis](https://github.com/dbwiddis).
* [#642](https://github.com/java-native-access/jna/pull/642): COM calls with variable number of arguments (varargs) are now supported - [@SevenOf9Sleeper](https://github.com/SevenOf9Sleeper).

Bug Fixes
Expand Down
21 changes: 21 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ boolean ReadFile(HANDLE hFile, byte[] lpBuffer, int nNumberOfBytesToRead,
*/
int GetTickCount();

/**
* The GetTickCount64 function retrieves the number of milliseconds that
* have elapsed since the system was started.
*
* @return Number of milliseconds that have elapsed since the system was
* started.
*/
long GetTickCount64();

/**
* The GetCurrentThreadId function retrieves the thread identifier of the
* calling thread.
Expand Down Expand Up @@ -3326,4 +3335,16 @@ boolean GetVolumePathNamesForVolumeName(String lpszVolumeName,
* not contain module information.
*/
boolean Module32NextW(HANDLE hSnapshot, Tlhelp32.MODULEENTRY32W lpme);

/**
* Controls whether the system will handle the specified types of serious
* errors or whether the process will handle them.
* @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v=vs.85).aspx">MSDN</a>
*
* @param umode
* The process error mode.
* @return The return value is the previous state of the error-mode bit
* flags.
*/
int SetErrorMode(int umode);
}
48 changes: 47 additions & 1 deletion contrib/platform/src/com/sun/jna/platform/win32/Psapi.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HMODULE;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.ptr.IntByReference;
Expand All @@ -32,7 +34,7 @@
*/
public interface Psapi extends StdCallLibrary {
Psapi INSTANCE = Native.loadLibrary("psapi", Psapi.class, W32APIOptions.DEFAULT_OPTIONS);

/**
* Retrieves the fully qualified path for the file containing the specified
* module.
Expand Down Expand Up @@ -234,6 +236,24 @@ public interface Psapi extends StdCallLibrary {
*/
int GetProcessImageFileName(HANDLE hProcess, char[] lpImageFileName, int nSize);


/**
* Retrieves the performance values contained in the
* {@link PERFORMANCE_INFORMATION} structure.
*
* @param pPerformanceInformation
* A pointer to a {@link PERFORMANCE_INFORMATION} structure that
* receives the performance information.
* @param cb
* The size of the {@link PERFORMANCE_INFORMATION} structure, in
* bytes.
* @return If the function succeeds, the return value is TRUE. If the
* function fails, the return value is FALSE. To get extended error
* information, call {@link Kernel32Util#getLastErrorMessage()}.
* @see <a href="http://msdn.microsoft.com/en-us/library/ms683210(VS.85).aspx">MSDN</a>
*/
boolean GetPerformanceInfo(PERFORMANCE_INFORMATION pPerformanceInformation, int cb);

class MODULEINFO extends Structure {
public static final List<String> FIELDS = createFieldsOrder("lpBaseOfDll", "SizeOfImage", "EntryPoint");

Expand All @@ -246,4 +266,30 @@ protected List<String> getFieldOrder() {
return FIELDS;
}
}

class PERFORMANCE_INFORMATION extends Structure {
public static final List<String> FIELDS = createFieldsOrder("cb", "CommitTotal", "CommitLimit",
"CommitPeak", "PhysicalTotal", "PhysicalAvailable", "SystemCache", "KernelTotal", "KernelPaged",
"KernelNonpaged", "PageSize", "HandleCount", "ProcessCount", "ThreadCount");

public DWORD cb;
public SIZE_T CommitTotal;
public SIZE_T CommitLimit;
public SIZE_T CommitPeak;
public SIZE_T PhysicalTotal;
public SIZE_T PhysicalAvailable;
public SIZE_T SystemCache;
public SIZE_T KernelTotal;
public SIZE_T KernelPaged;
public SIZE_T KernelNonpaged;
public SIZE_T PageSize;
public DWORD HandleCount;
public DWORD ProcessCount;
public DWORD ThreadCount;

@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
}
15 changes: 15 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ public void testGetTickCount() throws InterruptedException {
assertTrue(tick2 > tick1 || tick3 > tick2);
}

public void testGetTickCount64() throws InterruptedException {
long tick1 = Kernel32.INSTANCE.GetTickCount64();
Thread.sleep(10);
long tick2 = Kernel32.INSTANCE.GetTickCount64();

assertTrue(tick2 > tick1);
}

public void testGetVersion() {
DWORD version = Kernel32.INSTANCE.GetVersion();
assertTrue("Version high should be non-zero: 0x" + Integer.toHexString(version.getHigh().intValue()), version.getHigh().intValue() != 0);
Expand Down Expand Up @@ -1143,4 +1151,11 @@ public void testModule32NextW() {
}
}
}

public void testSetErrorMode() {
// Set bit flags to 0x0001
int previousMode = Kernel32.INSTANCE.SetErrorMode(0x0001);
// Restore to previous state; 0x0001 is now "previous"
assertEquals(Kernel32.INSTANCE.SetErrorMode(previousMode), 0x0001);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Psapi.MODULEINFO;
import com.sun.jna.platform.win32.Psapi.PERFORMANCE_INFORMATION;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.HMODULE;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinNT.HANDLE;
Expand Down Expand Up @@ -224,4 +226,11 @@ public void testGetProcessImageFileName() {
}
}
}

@Test
public void testGetPerformanceInfo() {
PERFORMANCE_INFORMATION perfInfo = new PERFORMANCE_INFORMATION();
assertTrue(Psapi.INSTANCE.GetPerformanceInfo(perfInfo, perfInfo.size()));
assertTrue(perfInfo.ProcessCount.intValue() > 0);
}
}