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

Added VirtualQueryEx, MEMORY_BASIC_INFORMATION, and related constants #368

Merged
merged 1 commit into from
Sep 20, 2014
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 @@ -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
---------
Expand Down
22 changes: 22 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 @@ -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);
}
91 changes: 91 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WinNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}