From f8f1b11e88804f79e44f766f365231fc807a848a Mon Sep 17 00:00:00 2001 From: Andrii Polishchuk Date: Sat, 20 Sep 2014 06:48:05 +0300 Subject: [PATCH] Added VirtualQueryEx, MEMORY_BASIC_INFORMATION, and related constants --- CHANGES.md | 1 + .../com/sun/jna/platform/win32/Kernel32.java | 22 +++++ .../src/com/sun/jna/platform/win32/WinNT.java | 91 +++++++++++++++++++ .../sun/jna/platform/win32/Kernel32Test.java | 9 ++ 4 files changed, 123 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 52b8c0125c..5a66fa280c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,6 +21,7 @@ Features * [#352](https://github.com/twall/jna/pull/352): Performance improvements due to reduced locking in `com.sun.jna.Library$Handler` and fewer vararg checks in `com.sun.jna.Function` - [@Boereck](https://github.com/Boereck). * [#357](https://github.com/twall/jna/pull/357): Added `com.sun.jna.platform.win32.Kernel32.SetSystemTime` - [@lgoldstein](https://github.com/lgoldstein), [@thomasjoulin](https://github.com/thomasjoulin). * [#365](https://github.com/twall/jna/pull/365): Added `com.sun.jna.platform.win32.Kernel32.GetComputerNameEx` support - [@lgoldstein](https://github.com/lgoldstein). +* [#368](https://github.com/twall/jna/pull/368): Added `com.sun.jna.platform.win32.Kernel32.VirtualQueryEx`, `com.sun.jna.platform.win32.WinNT.MEMORY_BASIC_INFORMATION` and `MEM_COMMIT`, `MEM_FREE`, `MEM_RESERVE`, `MEM_IMAGE`, `MEM_MAPPED`, `MEM_PRIVATE` constants to `com.sun.jna.platform.win32.WinNT` - [@apsk](https://github.com/apsk). 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 7f45d51c9d..46bb1486ed 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java @@ -2262,4 +2262,26 @@ boolean SystemTimeToTzSpecificLocalTime(TIME_ZONE_INFORMATION lpTimeZone, */ boolean ReadProcessMemory(HANDLE hProcess, Pointer lpBaseAddress, Pointer lpBuffer, int nSize, IntByReference lpNumberOfBytesRead); + /** + * Retrieves information about a range of pages within the virtual address space of a specified process. + * + * @param hProcess + * A handle to the process whose memory information is queried. The handle must have been + * opened with the PROCESS_QUERY_INFORMATION access right, which enables using the handle + * to read information from the process object. + * @param lpAddress + * A pointer to the base address of the region of pages to be queried. + * This value is rounded down to the next page boundary. To determine the size of a page on the host computer, + * use the GetSystemInfo function. If lpAddress specifies an address above the highest memory address + * accessible to the process, the function fails with ERROR_INVALID_PARAMETER. + * @param lpBuffer + * A pointer to a MEMORY_BASIC_INFORMATION structure in which information about the specified page range is returned. + * @param dwLength + * The size of the buffer pointed to by the lpBuffer parameter, in bytes. + * + * @return The return value is the actual number of bytes returned in the information buffer. + * If the function fails, the return value is zero. To get extended error information, + * call GetLastError. Possible error values include ERROR_INVALID_PARAMETER. + */ + SIZE_T VirtualQueryEx(HANDLE hProcess, Pointer lpAddress, MEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength); } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java index df3a01f2ed..211f7ab148 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/WinNT.java @@ -2448,4 +2448,95 @@ public static abstract class PROCESSOR_CACHE_TYPE { */ public static int CacheTrace = 3; } + + /** + * Indicates committed pages for which physical storage has been allocated, either in memory or in the paging file on disk. + */ + int MEM_COMMIT = 0x1000; + + /** + * Indicates free pages not accessible to the calling process and available to be allocated. + * For free pages, the information in the AllocationBase, AllocationProtect, Protect, and Type members is undefined. + */ + int MEM_FREE = 0x10000; + + /** + * Indicates reserved pages where a range of the process's virtual address space is reserved without any physical storage being allocated. + * For reserved pages, the information in the Protect member is undefined. + */ + int MEM_RESERVE = 0x2000; + + /** + * Indicates that the memory pages within the region are mapped into the view of an image section. + */ + int MEM_IMAGE = 0x1000000; + + /** + * Indicates that the memory pages within the region are mapped into the view of a section. + */ + int MEM_MAPPED = 0x40000; + + /** + * Indicates that the memory pages within the region are private (that is, not shared by other processes). + */ + int MEM_PRIVATE = 0x20000; + + public static class MEMORY_BASIC_INFORMATION extends Structure { + + /** + * A pointer to the base address of the region of pages. + */ + public Pointer baseAddress; + + /** + * A pointer to the base address of a range of pages allocated by the VirtualAlloc function. + * The page pointed to by the BaseAddress member is contained within this allocation range. + */ + public Pointer allocationBase; + + /** + * The memory protection option when the region was initially allocated. + * This member can be one of the memory protection constants or 0 if the caller does not have access. + */ + public DWORD allocationProtect; + + /** + * The size of the region beginning at the base address in which all pages have identical attributes, in bytes. + */ + public SIZE_T regionSize; + + /** + * The state of the pages in the region. + * This member can be one of the following values: + * + * MEM_COMMIT, + * MEM_FREE, + * MEM_RESERVE. + */ + public DWORD state; + + /** + * The access protection of the pages in the region. + * This member is one of the values listed for the AllocationProtect member. + */ + public DWORD protect; + + /** + * The type of pages in the region. + * The following types are defined: + * + * MEM_IMAGE + * MEM_MAPPED + * MEM_PRIVATE + */ + public DWORD type; + + @Override + protected List getFieldOrder() { + return Arrays.asList(new String[]{ + "baseAddress", "allocationBase", "allocationProtect", + "regionSize", "state", "protect", "type" + }); + } + } } 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 6f084c2558..88dddb0215 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java @@ -39,11 +39,13 @@ import com.sun.jna.platform.win32.WinBase.SYSTEM_INFO; import com.sun.jna.platform.win32.WinDef.DWORD; import com.sun.jna.platform.win32.WinDef.HWND; +import com.sun.jna.platform.win32.BaseTSD.SIZE_T; import com.sun.jna.platform.win32.WinNT.HANDLE; import com.sun.jna.platform.win32.WinNT.HANDLEByReference; import com.sun.jna.platform.win32.WinNT.LARGE_INTEGER; import com.sun.jna.platform.win32.WinNT.OSVERSIONINFO; import com.sun.jna.platform.win32.WinNT.OSVERSIONINFOEX; +import com.sun.jna.platform.win32.WinNT.MEMORY_BASIC_INFORMATION; import com.sun.jna.ptr.IntByReference; public class Kernel32Test extends TestCase { @@ -717,4 +719,11 @@ public void testReadProcessMemory() { assertEquals(bufDest.get(2),15); assertEquals(bufDest.get(3),3); } + + public void testVirtualQueryEx() { + HANDLE selfHandle = Kernel32.INSTANCE.GetCurrentProcess(); + MEMORY_BASIC_INFORMATION mbi = new MEMORY_BASIC_INFORMATION(); + SIZE_T bytesRead = Kernel32.INSTANCE.VirtualQueryEx(selfHandle, Pointer.NULL, mbi, new SIZE_T(mbi.size())); + assertTrue(bytesRead.intValue() > 0); + } }