From 458be462f7a00541cba36fe66e4c24424add1210 Mon Sep 17 00:00:00 2001 From: dbwiddis Date: Tue, 19 Apr 2016 14:14:06 -0700 Subject: [PATCH 1/2] Add Psapi.GetPerformanceInfo() and Kernel32.GetTickCount64(). --- CHANGES.md | 1 + .../com/sun/jna/platform/win32/Kernel32.java | 9 ++++ .../src/com/sun/jna/platform/win32/Psapi.java | 48 ++++++++++++++++++- .../sun/jna/platform/win32/Kernel32Test.java | 8 ++++ .../com/sun/jna/platform/win32/PsapiTest.java | 9 ++++ 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index a0dbd74f0f..5580900ce4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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()` and `com.sun.jna.platform.win32.Kernel32.GetTickCount64()` - [@dbwiddis](https://github.com/dbwiddis). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java index a2156ee3f3..b365d9e787 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java @@ -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. diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Psapi.java b/contrib/platform/src/com/sun/jna/platform/win32/Psapi.java index 8a0bd0139c..82394ab7e8 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Psapi.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Psapi.java @@ -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; @@ -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. @@ -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 MSDN + */ + boolean GetPerformanceInfo(PERFORMANCE_INFORMATION pPerformanceInformation, int cb); + class MODULEINFO extends Structure { public static final List FIELDS = createFieldsOrder("lpBaseOfDll", "SizeOfImage", "EntryPoint"); @@ -246,4 +266,30 @@ protected List getFieldOrder() { return FIELDS; } } + + class PERFORMANCE_INFORMATION extends Structure { + public static final List 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 getFieldOrder() { + return FIELDS; + } + } } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java index fca801832f..fcb5fd6fe7 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java @@ -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); diff --git a/contrib/platform/test/com/sun/jna/platform/win32/PsapiTest.java b/contrib/platform/test/com/sun/jna/platform/win32/PsapiTest.java index bd434194e7..635e1f4749 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/PsapiTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/PsapiTest.java @@ -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; @@ -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); + } } From af62b5487cc60be38d1c99ed8293d36a808e2d65 Mon Sep 17 00:00:00 2001 From: dbwiddis Date: Mon, 25 Apr 2016 01:38:24 -0700 Subject: [PATCH 2/2] Add SetErrorMode --- CHANGES.md | 2 +- .../src/com/sun/jna/platform/win32/Kernel32.java | 12 ++++++++++++ .../com/sun/jna/platform/win32/Kernel32Test.java | 7 +++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index aaadbbd580..227b4954fe 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -48,7 +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()` and `com.sun.jna.platform.win32.Kernel32.GetTickCount64()` - [@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 diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java index b365d9e787..5ae4acb2af 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java @@ -3335,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 MSDN + * + * @param umode + * The process error mode. + * @return The return value is the previous state of the error-mode bit + * flags. + */ + int SetErrorMode(int umode); } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java index fcb5fd6fe7..1f42ef3773 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java @@ -1151,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); + } }