0
# Windows Platform APIs
1
2
Comprehensive Windows API bindings providing access to core system functions, user interface operations, graphics, networking, services, and Component Object Model (COM) automation. These bindings provide direct access to Win32 APIs through JNA interfaces.
3
4
## Capabilities
5
6
### Core System Functions (Kernel32)
7
8
Essential Windows system functions for process management, memory operations, file I/O, and system information.
9
10
```java { .api }
11
/**
12
* Core Windows system functions from kernel32.dll
13
*/
14
public interface Kernel32 extends StdCallLibrary, WinNT, Wincon {
15
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.DEFAULT_OPTIONS);
16
17
/**
18
* Create a new process
19
* @param lpApplicationName Path to executable (can be null)
20
* @param lpCommandLine Command line string
21
* @param lpProcessAttributes Process security attributes
22
* @param lpThreadAttributes Thread security attributes
23
* @param bInheritHandles Whether child inherits handles
24
* @param dwCreationFlags Process creation flags
25
* @param lpEnvironment Environment block (can be null)
26
* @param lpCurrentDirectory Current directory (can be null)
27
* @param lpStartupInfo Startup information
28
* @param lpProcessInformation Process information output
29
* @return true if successful
30
*/
31
boolean CreateProcess(String lpApplicationName, String lpCommandLine,
32
WinBase.SECURITY_ATTRIBUTES lpProcessAttributes,
33
WinBase.SECURITY_ATTRIBUTES lpThreadAttributes,
34
boolean bInheritHandles, DWORD dwCreationFlags,
35
Pointer lpEnvironment, String lpCurrentDirectory,
36
WinBase.STARTUPINFO lpStartupInfo,
37
WinBase.PROCESS_INFORMATION lpProcessInformation);
38
39
/**
40
* Open existing process for access
41
* @param dwDesiredAccess Access rights requested
42
* @param bInheritHandle Whether handle can be inherited
43
* @param dwProcessId Process ID to open
44
* @return Process handle, or null if failed
45
*/
46
HANDLE OpenProcess(DWORD dwDesiredAccess, boolean bInheritHandle, DWORD dwProcessId);
47
48
/**
49
* Get current process ID
50
* @return Current process identifier
51
*/
52
DWORD GetCurrentProcessId();
53
54
/**
55
* Get current thread ID
56
* @return Current thread identifier
57
*/
58
DWORD GetCurrentThreadId();
59
60
/**
61
* Get last error code
62
* @return Error code from last failed API call
63
*/
64
DWORD GetLastError();
65
66
/**
67
* Allocate virtual memory
68
* @param lpAddress Starting address (can be null)
69
* @param dwSize Size to allocate
70
* @param flAllocationType Allocation type flags
71
* @param flProtect Memory protection flags
72
* @return Allocated memory address, or null if failed
73
*/
74
Pointer VirtualAlloc(Pointer lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
75
76
/**
77
* Free virtual memory
78
* @param lpAddress Address to free
79
* @param dwSize Size to free (0 for entire allocation)
80
* @param dwFreeType Free operation type
81
* @return true if successful
82
*/
83
boolean VirtualFree(Pointer lpAddress, SIZE_T dwSize, DWORD dwFreeType);
84
}
85
```
86
87
**Usage Examples:**
88
89
```java
90
import com.sun.jna.platform.win32.Kernel32;
91
import com.sun.jna.platform.win32.WinDef.DWORD;
92
import com.sun.jna.platform.win32.WinBase;
93
94
// Get current process information
95
DWORD processId = Kernel32.INSTANCE.GetCurrentProcessId();
96
DWORD threadId = Kernel32.INSTANCE.GetCurrentThreadId();
97
System.out.println("PID: " + processId + ", TID: " + threadId);
98
99
// Create a new process
100
WinBase.STARTUPINFO startupInfo = new WinBase.STARTUPINFO();
101
WinBase.PROCESS_INFORMATION processInfo = new WinBase.PROCESS_INFORMATION();
102
103
boolean success = Kernel32.INSTANCE.CreateProcess(
104
null, // Application name
105
"notepad.exe", // Command line
106
null, // Process attributes
107
null, // Thread attributes
108
false, // Inherit handles
109
new DWORD(0), // Creation flags
110
null, // Environment
111
null, // Current directory
112
startupInfo, // Startup info
113
processInfo // Process info
114
);
115
116
if (success) {
117
System.out.println("Process created with PID: " + processInfo.dwProcessId);
118
} else {
119
System.err.println("Failed to create process. Error: " + Kernel32.INSTANCE.GetLastError());
120
}
121
```
122
123
### User Interface Operations (User32)
124
125
Windows user interface APIs for window management, message handling, and input processing.
126
127
```java { .api }
128
/**
129
* Windows user interface functions from user32.dll
130
*/
131
public interface User32 extends StdCallLibrary, WinUser, WinNT {
132
User32 INSTANCE = Native.load("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
133
134
// Window visibility constants
135
int SW_HIDE = 0;
136
int SW_MAXIMIZE = 3;
137
int SW_MINIMIZE = 6;
138
int SW_RESTORE = 9;
139
int SW_SHOW = 5;
140
141
/**
142
* Find window by class name and/or window title
143
* @param lpClassName Window class name (can be null)
144
* @param lpWindowName Window title (can be null)
145
* @return Window handle, or null if not found
146
*/
147
HWND FindWindow(String lpClassName, String lpWindowName);
148
149
/**
150
* Get window title text
151
* @param hWnd Window handle
152
* @param lpString Buffer to receive text
153
* @param nMaxCount Maximum characters to copy
154
* @return Number of characters copied
155
*/
156
int GetWindowText(HWND hWnd, char[] lpString, int nMaxCount);
157
158
/**
159
* Send message to window
160
* @param hWnd Target window handle
161
* @param Msg Message identifier
162
* @param wParam First message parameter
163
* @param lParam Second message parameter
164
* @return Message result
165
*/
166
LRESULT SendMessage(HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam);
167
168
/**
169
* Show or hide window
170
* @param hWnd Window handle
171
* @param nCmdShow Show command (SW_* constants)
172
* @return true if window was previously visible
173
*/
174
boolean ShowWindow(HWND hWnd, int nCmdShow);
175
176
/**
177
* Set window position and size
178
* @param hWnd Window handle
179
* @param hWndInsertAfter Z-order position
180
* @param X Left position
181
* @param Y Top position
182
* @param cx Width
183
* @param cy Height
184
* @param uFlags Position flags
185
* @return true if successful
186
*/
187
boolean SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
188
189
/**
190
* Get window rectangle
191
* @param hWnd Window handle
192
* @param rect Rectangle structure to fill
193
* @return true if successful
194
*/
195
boolean GetWindowRect(HWND hWnd, WinDef.RECT rect);
196
}
197
```
198
199
### Graphics Operations (GDI32)
200
201
Graphics Device Interface functions for drawing operations, device contexts, and graphics object management.
202
203
```java { .api }
204
/**
205
* Graphics Device Interface functions from gdi32.dll
206
*/
207
public interface GDI32 extends StdCallLibrary {
208
GDI32 INSTANCE = Native.load("gdi32", GDI32.class, W32APIOptions.DEFAULT_OPTIONS);
209
210
/**
211
* Create compatible device context
212
* @param hdc Device context to make compatible with (can be null)
213
* @return Device context handle, or null if failed
214
*/
215
HDC CreateCompatibleDC(HDC hdc);
216
217
/**
218
* Delete device context
219
* @param hdc Device context to delete
220
* @return true if successful
221
*/
222
boolean DeleteDC(HDC hdc);
223
224
/**
225
* Perform bit block transfer
226
* @param hdcDest Destination device context
227
* @param nXDest Destination X coordinate
228
* @param nYDest Destination Y coordinate
229
* @param nWidth Width of area to copy
230
* @param nHeight Height of area to copy
231
* @param hdcSrc Source device context
232
* @param nXSrc Source X coordinate
233
* @param nYSrc Source Y coordinate
234
* @param dwRop Raster operation code
235
* @return true if successful
236
*/
237
boolean BitBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight,
238
HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop);
239
240
/**
241
* Select graphics object into device context
242
* @param hdc Device context
243
* @param hgdiobj Graphics object to select
244
* @return Previously selected object, or null if failed
245
*/
246
HGDIOBJ SelectObject(HDC hdc, HGDIOBJ hgdiobj);
247
248
/**
249
* Delete graphics object
250
* @param hObject Graphics object to delete
251
* @return true if successful
252
*/
253
boolean DeleteObject(HGDIOBJ hObject);
254
}
255
```
256
257
### Advanced Services (Advapi32)
258
259
Advanced Windows services including registry access, security, services management, and event logging.
260
261
```java { .api }
262
/**
263
* Advanced Windows services from advapi32.dll
264
*/
265
public interface Advapi32 extends StdCallLibrary {
266
Advapi32 INSTANCE = Native.load("Advapi32", Advapi32.class, W32APIOptions.DEFAULT_OPTIONS);
267
268
/**
269
* Open registry key
270
* @param hKey Parent key handle
271
* @param lpSubKey Subkey name
272
* @param ulOptions Reserved (should be 0)
273
* @param samDesired Security access mask
274
* @param phkResult Opened key handle output
275
* @return Error code (ERROR_SUCCESS if successful)
276
*/
277
int RegOpenKeyEx(HKEY hKey, String lpSubKey, int ulOptions, int samDesired, HKEYByReference phkResult);
278
279
/**
280
* Query registry value
281
* @param hKey Registry key handle
282
* @param lpValueName Value name (can be null for default)
283
* @param lpReserved Reserved (should be null)
284
* @param lpType Value type output
285
* @param lpData Value data buffer
286
* @param lpcbData Data buffer size
287
* @return Error code (ERROR_SUCCESS if successful)
288
*/
289
int RegQueryValueEx(HKEY hKey, String lpValueName, int lpReserved, IntByReference lpType,
290
byte[] lpData, IntByReference lpcbData);
291
292
/**
293
* Close registry key
294
* @param hKey Key handle to close
295
* @return Error code (ERROR_SUCCESS if successful)
296
*/
297
int RegCloseKey(HKEY hKey);
298
299
/**
300
* Open process token
301
* @param ProcessHandle Process handle
302
* @param DesiredAccess Access rights requested
303
* @param TokenHandle Token handle output
304
* @return true if successful
305
*/
306
boolean OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, HANDLEByReference TokenHandle);
307
308
/**
309
* Get token information
310
* @param TokenHandle Token handle
311
* @param TokenInformationClass Information class to retrieve
312
* @param TokenInformation Information buffer
313
* @param TokenInformationLength Buffer length
314
* @param ReturnLength Required length output
315
* @return true if successful
316
*/
317
boolean GetTokenInformation(HANDLE TokenHandle, int TokenInformationClass,
318
Structure TokenInformation, int TokenInformationLength,
319
IntByReference ReturnLength);
320
}
321
```
322
323
### Windows Data Types
324
325
Essential Windows data structures and type definitions used throughout the APIs.
326
327
```java { .api }
328
/**
329
* Basic Windows type definitions
330
*/
331
public class WinDef {
332
/**
333
* Window handle
334
*/
335
public static class HWND extends HANDLE {
336
public HWND() { }
337
public HWND(Pointer p) { super(p); }
338
}
339
340
/**
341
* 32-bit unsigned integer
342
*/
343
public static class DWORD extends IntegerType {
344
public DWORD() { this(0); }
345
public DWORD(long value) { super(4, value, true); }
346
}
347
348
/**
349
* 16-bit unsigned integer
350
*/
351
public static class WORD extends IntegerType {
352
public WORD() { this(0); }
353
public WORD(long value) { super(2, value, true); }
354
}
355
356
/**
357
* 8-bit unsigned integer
358
*/
359
public static class BYTE extends IntegerType {
360
public BYTE() { this(0); }
361
public BYTE(long value) { super(1, value, true); }
362
}
363
364
/**
365
* Pointer-sized integer for memory sizes
366
*/
367
public static class SIZE_T extends IntegerType {
368
public SIZE_T() { this(0); }
369
public SIZE_T(long value) { super(Native.POINTER_SIZE, value, true); }
370
}
371
372
/**
373
* Registry key handle
374
*/
375
public static class HKEY extends HANDLE {
376
public HKEY() { }
377
public HKEY(Pointer p) { super(p); }
378
}
379
380
/**
381
* Device context handle
382
*/
383
public static class HDC extends HANDLE {
384
public HDC() { }
385
public HDC(Pointer p) { super(p); }
386
}
387
388
/**
389
* GDI object handle
390
*/
391
public static class HGDIOBJ extends HANDLE {
392
public HGDIOBJ() { }
393
public HGDIOBJ(Pointer p) { super(p); }
394
}
395
396
/**
397
* Rectangle structure
398
*/
399
public static class RECT extends Structure {
400
public int left, top, right, bottom;
401
402
public RECT() { }
403
404
public RECT(int left, int top, int right, int bottom) {
405
this.left = left; this.top = top;
406
this.right = right; this.bottom = bottom;
407
}
408
409
@Override
410
protected List<String> getFieldOrder() {
411
return Arrays.asList(new String[] {"left", "top", "right", "bottom"});
412
}
413
}
414
415
/**
416
* Point structure
417
*/
418
public static class POINT extends Structure {
419
public int x, y;
420
421
public POINT() { }
422
423
public POINT(int x, int y) {
424
this.x = x; this.y = y;
425
}
426
427
@Override
428
protected List<String> getFieldOrder() {
429
return Arrays.asList(new String[] {"x", "y"});
430
}
431
}
432
}
433
434
/**
435
* Windows NT definitions
436
*/
437
public class WinNT {
438
/**
439
* Generic handle type
440
*/
441
public static class HANDLE extends PointerType {
442
public HANDLE() { }
443
public HANDLE(Pointer p) { setPointer(p); }
444
}
445
446
/**
447
* Access mask for security operations
448
*/
449
public static class ACCESS_MASK extends DWORD {
450
public ACCESS_MASK() { }
451
public ACCESS_MASK(long value) { super(value); }
452
}
453
}
454
```
455
456
### Utility Classes
457
458
High-level wrapper classes that provide easier-to-use APIs and better error handling.
459
460
```java { .api }
461
/**
462
* High-level Kernel32 utilities
463
*/
464
public class Kernel32Util {
465
/**
466
* Get computer name
467
* @return Computer name string
468
* @throws Win32Exception if operation fails
469
*/
470
public static String getComputerName() throws Win32Exception;
471
472
/**
473
* Get user name
474
* @return Current user name
475
* @throws Win32Exception if operation fails
476
*/
477
public static String getUserName() throws Win32Exception;
478
479
/**
480
* Format system error message
481
* @param code Error code to format
482
* @return Error message string
483
*/
484
public static String formatMessage(int code);
485
}
486
487
/**
488
* High-level Advapi32 utilities
489
*/
490
public class Advapi32Util {
491
/**
492
* Get user account information
493
* @param systemName System name (can be null for local)
494
* @param accountName Account name to lookup
495
* @return Account information
496
* @throws Win32Exception if lookup fails
497
*/
498
public static Account getAccountByName(String systemName, String accountName) throws Win32Exception;
499
500
/**
501
* Get registry value as string
502
* @param root Root key
503
* @param keyPath Key path
504
* @param valueName Value name
505
* @return String value
506
* @throws Win32Exception if registry operation fails
507
*/
508
public static String registryGetStringValue(HKEY root, String keyPath, String valueName) throws Win32Exception;
509
}
510
511
/**
512
* Windows-specific exception with error code
513
*/
514
public class Win32Exception extends RuntimeException {
515
private final int errorCode;
516
517
public Win32Exception(int errorCode) {
518
super(Kernel32Util.formatMessage(errorCode));
519
this.errorCode = errorCode;
520
}
521
522
public int getErrorCode() {
523
return errorCode;
524
}
525
}
526
```
527
528
## Complete Windows API Coverage
529
530
The JNA Platform library provides comprehensive coverage of Windows APIs including:
531
532
- **System Services**: Winsvc (Service Control Manager), Psapi (Process Status), NtDll (Native APIs)
533
- **Networking**: WinINet (Internet), Netapi32 (Network Management), IPHlpAPI (IP Helper)
534
- **Hardware/Devices**: SetupApi (Device Installation), Cfgmgr32 (Configuration Manager), Dxva2 (DirectX)
535
- **File System**: Shell32 (Shell), Shlwapi (Shell Utilities), Winioctl (I/O Control)
536
- **Security**: Secur32 (Security Support Provider), Wtsapi32 (Terminal Services)
537
- **Performance**: Pdh (Performance Data Helper), WMI integration
538
- **Graphics**: OpenGL32 bindings, GDI32Util high-level graphics utilities
539
540
Each API follows the same pattern of providing both direct native bindings and high-level utility classes for common operations.