View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.jna.platform.windows;
6   
7   import com.sun.jna.Native;
8   import com.sun.jna.Pointer;
9   import com.sun.jna.Structure;
10  import com.sun.jna.Structure.FieldOrder;
11  import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
12  import com.sun.jna.platform.win32.WinNT.HANDLE;
13  import com.sun.jna.ptr.IntByReference;
14  import com.sun.jna.win32.W32APIOptions;
15  
16  public interface NtDll extends com.sun.jna.platform.win32.NtDll {
17  
18      NtDll INSTANCE = Native.load("NtDll", NtDll.class, W32APIOptions.DEFAULT_OPTIONS);
19  
20      int PROCESS_BASIC_INFORMATION = 0;
21  
22      @FieldOrder({ "Reserved1", "PebBaseAddress", "Reserved2" })
23      class PROCESS_BASIC_INFORMATION extends Structure {
24          public Pointer Reserved1;
25          public Pointer PebBaseAddress;
26          public Pointer[] Reserved2 = new Pointer[4];
27      }
28  
29      @FieldOrder({ "pad", "pad2", "ProcessParameters" })
30      class PEB extends Structure {
31          public byte[] pad = new byte[4];
32          public Pointer[] pad2 = new Pointer[3];
33          public Pointer ProcessParameters; // RTL_USER_PROCESS_PARAMETERS
34      }
35  
36      @FieldOrder({ "MaximumLength", "Length", "Flags", "DebugFlags", "ConsoleHandle", "ConsoleFlags", "StandardInput",
37              "StandardOutput", "StandardError", "CurrentDirectory", "DllPath", "ImagePathName", "CommandLine",
38              "Environment", "StartingX", "StartingY", "CountX", "CountY", "CountCharsX", "CountCharsY", "FillAttribute",
39              "WindowFlags", "ShowWindowFlags", "WindowTitle", "DesktopInfo", "ShellInfo", "RuntimeData",
40              "CurrentDirectories", "EnvironmentSize", "EnvironmentVersion", "PackageDependencyData", "ProcessGroupId",
41              "LoaderThreads", "RedirectionDllName", "HeapPartitionName", "DefaultThreadpoolCpuSetMasks",
42              "DefaultThreadpoolCpuSetMaskCount" })
43      class RTL_USER_PROCESS_PARAMETERS extends Structure {
44          public int MaximumLength;
45          public int Length;
46          public int Flags;
47          public int DebugFlags;
48          public HANDLE ConsoleHandle;
49          public int ConsoleFlags;
50          public HANDLE StandardInput;
51          public HANDLE StandardOutput;
52          public HANDLE StandardError;
53          public CURDIR CurrentDirectory;
54          public UNICODE_STRING DllPath;
55          public UNICODE_STRING ImagePathName;
56          public UNICODE_STRING CommandLine;
57          public Pointer Environment;
58          public int StartingX;
59          public int StartingY;
60          public int CountX;
61          public int CountY;
62          public int CountCharsX;
63          public int CountCharsY;
64          public int FillAttribute;
65          public int WindowFlags;
66          public int ShowWindowFlags;
67          public UNICODE_STRING WindowTitle;
68          public UNICODE_STRING DesktopInfo;
69          public UNICODE_STRING ShellInfo;
70          public UNICODE_STRING RuntimeData;
71          public RTL_DRIVE_LETTER_CURDIR[] CurrentDirectories = new RTL_DRIVE_LETTER_CURDIR[32];
72          public ULONG_PTR EnvironmentSize;
73          public ULONG_PTR EnvironmentVersion;
74          public Pointer PackageDependencyData;
75          public int ProcessGroupId;
76          public int LoaderThreads;
77          public UNICODE_STRING RedirectionDllName;
78          public UNICODE_STRING HeapPartitionName;
79          public ULONG_PTR DefaultThreadpoolCpuSetMasks;
80          public int DefaultThreadpoolCpuSetMaskCount;
81      }
82  
83      @FieldOrder({ "Length", "MaximumLength", "Buffer" })
84      class UNICODE_STRING extends Structure {
85          public short Length;
86          public short MaximumLength;
87          public Pointer Buffer;
88      }
89  
90      @FieldOrder({ "Length", "MaximumLength", "Buffer" })
91      class STRING extends Structure {
92          public short Length;
93          public short MaximumLength;
94          public Pointer Buffer;
95      }
96  
97      @FieldOrder({ "DosPath", "Handle" })
98      class CURDIR extends Structure {
99          public UNICODE_STRING DosPath;
100         public Pointer Handle;
101     }
102 
103     @FieldOrder({ "Flags", "Length", "TimeStamp", "DosPath" })
104     class RTL_DRIVE_LETTER_CURDIR extends Structure {
105         public short Flags;
106         public short Length;
107         public int TimeStamp;
108         public STRING DosPath;
109     }
110 
111     /*
112      * Windows API docs say NtQueryInformationProcess may be altered or unavailable in future versions of Windows.
113      * Applications should use the alternate functions listed in this topic. However, there is no other way to get this
114      * information, it's been officially non-API for over a decade, and many many programs including windows sysinternal
115      * tools rely on this behavior, so the odds of it going away are small.
116      */
117     int NtQueryInformationProcess(HANDLE ProcessHandle, int ProcessInformationClass, Pointer ProcessInformation,
118             int ProcessInformationLength, IntByReference ReturnLength);
119 }