|
26 | 26 | import com.sun.jna.Native;
|
27 | 27 | import com.sun.jna.Pointer;
|
28 | 28 | import com.sun.jna.Structure;
|
| 29 | +import com.sun.jna.Structure.ByReference; |
29 | 30 | import com.sun.jna.Structure.FieldOrder;
|
30 | 31 | import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
|
| 32 | +import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR; |
31 | 33 | import com.sun.jna.platform.win32.WinDef.DWORD;
|
32 | 34 | import com.sun.jna.platform.win32.WinDef.HMODULE;
|
33 | 35 | import com.sun.jna.platform.win32.WinNT.HANDLE;
|
@@ -292,6 +294,18 @@ public interface Psapi extends StdCallLibrary {
|
292 | 294 | */
|
293 | 295 | boolean EnumProcesses(int[] lpidProcess, int cb, IntByReference lpcbNeeded);
|
294 | 296 |
|
| 297 | + /** |
| 298 | + * Retrieves extended information about the pages at specific |
| 299 | + * virtual addresses in the address space of the specified process. |
| 300 | + * |
| 301 | + * @param hProcess A Handle to the Process |
| 302 | + * @param pv A pointer to an array of PSAPI_WORKING_SET_EX_INFORMATION structures |
| 303 | + * @param cb The size of the pv buffer, in bytes. |
| 304 | + * @return If the function succeeds, the return value is nonzero. |
| 305 | + * @see <a href="https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-queryworkingsetex">MSDN</a> |
| 306 | + */ |
| 307 | + boolean QueryWorkingSetEx(HANDLE hProcess, Pointer pv, int cb); |
| 308 | + |
295 | 309 | @FieldOrder({"lpBaseOfDll", "SizeOfImage", "EntryPoint"})
|
296 | 310 | class MODULEINFO extends Structure {
|
297 | 311 | public Pointer EntryPoint;
|
@@ -320,4 +334,106 @@ class PERFORMANCE_INFORMATION extends Structure {
|
320 | 334 | public DWORD ProcessCount;
|
321 | 335 | public DWORD ThreadCount;
|
322 | 336 | }
|
| 337 | + |
| 338 | + @FieldOrder({"Flags", "Data"}) |
| 339 | + class PSAPI_WORKING_SET_EX_BLOCK extends Structure implements ByReference { |
| 340 | + public ULONG_PTR Flags; |
| 341 | + public ULONG_PTR[] Data = new ULONG_PTR[Native.POINTER_SIZE == 8 ? 1 : 2]; |
| 342 | + private long innerValue; |
| 343 | + |
| 344 | + @Override |
| 345 | + public void read() { |
| 346 | + super.read(); |
| 347 | + innerValue = this.Data[0].longValue(); |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * If this bit is 1, the subsequent members are valid; otherwise they should be |
| 352 | + * ignored. |
| 353 | + */ |
| 354 | + public boolean isValid() { |
| 355 | + return getBitFieldValue(1, 0) == 1; |
| 356 | + } |
| 357 | + |
| 358 | + /** |
| 359 | + * The number of processes that share this page. The maximum value of this |
| 360 | + * member is 7. |
| 361 | + */ |
| 362 | + public int getShareCount() { |
| 363 | + return getBitFieldValue(3, 1); |
| 364 | + } |
| 365 | + |
| 366 | + /** |
| 367 | + * The memory protection attributes of the page. For a list of values see below. |
| 368 | + * |
| 369 | + * @see <a href= |
| 370 | + * "https://docs.microsoft.com/en-us/windows/desktop/Memory/memory-protection-constants">Memory |
| 371 | + * Protection Constants</a>. |
| 372 | + */ |
| 373 | + public int getWin32Protection() { |
| 374 | + return getBitFieldValue(11, 3 + 1); |
| 375 | + } |
| 376 | + |
| 377 | + /** |
| 378 | + * If this bit is 1, the page can be shared. |
| 379 | + */ |
| 380 | + public boolean isShared() { |
| 381 | + return getBitFieldValue(1, 11 + 3 + 1) == 1; |
| 382 | + } |
| 383 | + |
| 384 | + /** |
| 385 | + * The NUMA node. The maximum value of this member is 63. |
| 386 | + */ |
| 387 | + public int getNode() { |
| 388 | + return getBitFieldValue(6, 1 + 11 + 3 + 1); |
| 389 | + } |
| 390 | + |
| 391 | + /** |
| 392 | + * If this bit is 1, the virtual page is locked in physical memory. |
| 393 | + */ |
| 394 | + public boolean isLocked() { |
| 395 | + return getBitFieldValue(1, 6 + 1 + 11 + 3 + 1) == 1; |
| 396 | + } |
| 397 | + |
| 398 | + /** |
| 399 | + * If this bit is 1, the page is a large page. |
| 400 | + */ |
| 401 | + public boolean isLargePage() { |
| 402 | + return getBitFieldValue(1, 1 + 6 + 1 + 11 + 3 + 1) == 1; |
| 403 | + } |
| 404 | + |
| 405 | + /** |
| 406 | + * If this bit is 1, the page is has been reported as bad. |
| 407 | + */ |
| 408 | + public boolean isBad() { |
| 409 | + return getBitFieldValue(1, 1 + 1 + 1 + 6 + 1 + 11 + 3 + 1) == 1; |
| 410 | + } |
| 411 | + |
| 412 | + /** |
| 413 | + * Returns innerValue after shifting the value rightShiftAmount, and applying a |
| 414 | + * Bit Mask of size maskLength. Example, <br/> |
| 415 | + * innerValue = 0011<br/> |
| 416 | + * getBitFieldValue(2, 1) = 0011 >> 1 & 11 = 01 |
| 417 | + * |
| 418 | + * @param maskLength |
| 419 | + * Size of the Bit Mask |
| 420 | + * @param rightShiftAmount |
| 421 | + * Amount to Shift innerValue to the right by |
| 422 | + * @return innerValue with the mask and shift applied. |
| 423 | + */ |
| 424 | + private int getBitFieldValue(final int maskLength, final int rightShiftAmount) { |
| 425 | + long bitMask = 0; |
| 426 | + |
| 427 | + for (int bit = 0; bit < maskLength; bit++) { |
| 428 | + bitMask |= 1 << bit; |
| 429 | + } |
| 430 | + return (int) ((innerValue >>> rightShiftAmount) & bitMask); |
| 431 | + } |
| 432 | + } |
| 433 | + |
| 434 | + @FieldOrder({"VirtualAddress", "VirtualAttributes"}) |
| 435 | + class PSAPI_WORKING_SET_EX_INFORMATION extends Structure { |
| 436 | + public Pointer VirtualAddress; |
| 437 | + public PSAPI_WORKING_SET_EX_BLOCK VirtualAttributes; |
| 438 | + } |
323 | 439 | }
|
0 commit comments