/
Kernel32Util.java
1238 lines (1132 loc) · 49.4 KB
/
Kernel32Util.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2010, 2013 Daniel Doubrovkine, Markus Karg, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna.platform.win32;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.sun.jna.LastErrorException;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
import com.sun.jna.platform.win32.WinNT.HRESULT;
import com.sun.jna.platform.win32.WinNT.LOGICAL_PROCESSOR_RELATIONSHIP;
import com.sun.jna.platform.win32.WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.W32APITypeMapper;
/**
* Kernel32 utility API.
*
* @author dblock[at]dblock.org
* @author markus[at]headcrashing[dot]eu
* @author Andreas "PAX" Lück, onkelpax-git[at]yahoo.de
*/
public abstract class Kernel32Util implements WinDef {
/**
* Get current computer NetBIOS name.
*
* @return Netbios name.
*/
public static String getComputerName() {
char buffer[] = new char[WinBase.MAX_COMPUTERNAME_LENGTH + 1];
IntByReference lpnSize = new IntByReference(buffer.length);
if (!Kernel32.INSTANCE.GetComputerName(buffer, lpnSize)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return Native.toString(buffer);
}
/**
* Invokes {@link Kernel32#LocalFree(Pointer)} and checks if it succeeded.
*
* @param ptr The {@link Pointer} to the memory to be released - ignored if NULL
* @throws Win32Exception if non-{@code ERROR_SUCCESS} code reported
*/
public static void freeLocalMemory(Pointer ptr) {
Pointer res = Kernel32.INSTANCE.LocalFree(ptr);
if (res != null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
/**
* Invokes {@link Kernel32#GlobalFree(Pointer)} and checks if it succeeded.
*
* @param ptr The {@link Pointer} to the memory to be released - ignored if NULL
* @throws Win32Exception if non-{@code ERROR_SUCCESS} code reported
*/
public static void freeGlobalMemory(Pointer ptr) {
Pointer res = Kernel32.INSTANCE.GlobalFree(ptr);
if (res != null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
/**
* Closes all referenced handles. If an exception is thrown for
* a specific handle, then it is accumulated until all
* handles have been closed. If more than one exception occurs,
* then it is added as a suppressed exception to the first one.
* Once closed all handles, the accumulated exception (if any) is thrown
*
* @param refs The references to close
* @see #closeHandleRef(WinNT.HANDLEByReference)
*/
public static void closeHandleRefs(HANDLEByReference... refs) {
Win32Exception err = null;
for (HANDLEByReference r : refs) {
try {
closeHandleRef(r);
} catch(Win32Exception e) {
if (err == null) {
err = e;
} else {
err.addSuppressedReflected(e);
}
}
}
if (err != null) {
throw err;
}
}
/**
* Closes the handle in the reference
*
* @param ref The handle reference - ignored if {@code null}
* @see #closeHandle(WinNT.HANDLE)
*/
public static void closeHandleRef(HANDLEByReference ref) {
closeHandle((ref == null) ? null : ref.getValue());
}
/**
* Invokes {@link #closeHandle(WinNT.HANDLE)} on each handle. If an exception
* is thrown for a specific handle, then it is accumulated until all
* handles have been closed. If more than one exception occurs, then it
* is added as a suppressed exception to the first one. Once closed all
* handles, the accumulated exception (if any) is thrown
*
* @param handles The handles to be closed
* @see Throwable#getSuppressed()
*/
public static void closeHandles(HANDLE... handles) {
Win32Exception err = null;
for (HANDLE h : handles) {
try {
closeHandle(h);
} catch(Win32Exception e) {
if (err == null) {
err = e;
} else {
err.addSuppressedReflected(e);
}
}
}
if (err != null) {
throw err;
}
}
/**
* Invokes {@link Kernel32#CloseHandle(WinNT.HANDLE)} and checks the success code.
* If not successful, then throws a {@link Win32Exception} with the
* {@code GetLastError} value
*
* @param h The handle to be closed - ignored if {@code null}
*/
public static void closeHandle(HANDLE h) {
if (h == null) {
return;
}
if (!Kernel32.INSTANCE.CloseHandle(h)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
/**
* Format a message from the value obtained from
* {@link Kernel32#GetLastError()} or {@link Native#getLastError()}.
*
* @param code The error code
* @return Formatted message.
*/
public static String formatMessage(int code) {
PointerByReference buffer = new PointerByReference();
int nLen = Kernel32.INSTANCE.FormatMessage(
WinBase.FORMAT_MESSAGE_ALLOCATE_BUFFER
| WinBase.FORMAT_MESSAGE_FROM_SYSTEM
| WinBase.FORMAT_MESSAGE_IGNORE_INSERTS,
null,
code,
0, // TODO: // MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
buffer, 0, null);
if (nLen == 0) {
throw new LastErrorException(Native.getLastError());
}
Pointer ptr = buffer.getValue();
try {
String s = ptr.getWideString(0);
return s.trim();
} finally {
freeLocalMemory(ptr);
}
}
/**
* Format a message from an HRESULT.
*
* @param code
* HRESULT
* @return Formatted message.
*/
public static String formatMessage(HRESULT code) {
return formatMessage(code.intValue());
}
/**
* Format a system message from an error code.
*
* @param code
* Error code, typically a result of GetLastError.
* @return Formatted message.
*/
public static String formatMessageFromLastErrorCode(int code) {
return formatMessage(W32Errors.HRESULT_FROM_WIN32(code));
}
/**
* @return Obtains the human-readable error message text from the last error
* that occurred by invocating {@code Kernel32.GetLastError()}.
*/
public static String getLastErrorMessage() {
return Kernel32Util.formatMessageFromLastErrorCode(Kernel32.INSTANCE
.GetLastError());
}
/**
* Return the path designated for temporary files.
*
* @return Path.
*/
public static String getTempPath() {
DWORD nBufferLength = new DWORD(WinDef.MAX_PATH);
char[] buffer = new char[nBufferLength.intValue()];
if (Kernel32.INSTANCE.GetTempPath(nBufferLength, buffer).intValue() == 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return Native.toString(buffer);
}
public static void deleteFile(String filename) {
if (!Kernel32.INSTANCE.DeleteFile(filename)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
/**
* Returns valid drives in the system.
*
* @return A {@link List} of valid drives.
*/
public static List<String> getLogicalDriveStrings() {
DWORD dwSize = Kernel32.INSTANCE.GetLogicalDriveStrings(new DWORD(0), null);
if (dwSize.intValue() <= 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
char buf[] = new char[dwSize.intValue()];
dwSize = Kernel32.INSTANCE.GetLogicalDriveStrings(dwSize, buf);
int bufSize = dwSize.intValue();
if (bufSize <= 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return Native.toStringList(buf, 0, bufSize);
}
/**
* Retrieves file system attributes for a specified file or directory.
*
* @param fileName
* The name of the file or directory.
* @return The attributes of the specified file or directory.
*/
public static int getFileAttributes(String fileName) {
int fileAttributes = Kernel32.INSTANCE.GetFileAttributes(fileName);
if (fileAttributes == WinBase.INVALID_FILE_ATTRIBUTES) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return fileAttributes;
}
/**
* Retrieves the result of GetFileType, provided the file exists.
* @param fileName file name
* @return file type
* @throws FileNotFoundException if file not found
*/
public static int getFileType(String fileName) throws FileNotFoundException {
File f = new File(fileName);
if (!f.exists()) {
throw new FileNotFoundException(fileName);
}
HANDLE hFile = null;
Win32Exception err = null;
try {
hFile = Kernel32.INSTANCE.CreateFile(fileName, WinNT.GENERIC_READ,
WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(),
WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL,
new HANDLEByReference().getValue());
if (WinBase.INVALID_HANDLE_VALUE.equals(hFile)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
int type = Kernel32.INSTANCE.GetFileType(hFile);
switch (type) {
case WinNT.FILE_TYPE_UNKNOWN:
int rc = Kernel32.INSTANCE.GetLastError();
switch (rc) {
case WinError.NO_ERROR:
break;
default:
throw new Win32Exception(rc);
}
default:
return type;
}
} catch(Win32Exception e) {
err = e;
throw err; // re-throw so finally block executed
} finally {
try {
closeHandle(hFile);
} catch(Win32Exception e) {
if (err == null) {
err = e;
} else {
err.addSuppressedReflected(e);
}
}
if (err != null) {
throw err;
}
}
}
/**
* @param rootName name of root drive
* @return One of the WinBase.DRIVE_* constants.
*/
public static int getDriveType(String rootName) {
return Kernel32.INSTANCE.GetDriveType(rootName);
}
/**
* Get the value of an environment variable.
*
* @param name
* Name of the environment variable.
* @return Value of an environment variable.
*/
public static String getEnvironmentVariable(String name) {
// obtain the buffer size
int size = Kernel32.INSTANCE.GetEnvironmentVariable(name, null, 0);
if (size == 0) {
return null;
} else if (size < 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
// obtain the value
char[] buffer = new char[size];
size = Kernel32.INSTANCE.GetEnvironmentVariable(name, buffer,
buffer.length);
if (size <= 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return Native.toString(buffer);
}
/**
* Uses the {@link Kernel32#GetEnvironmentStrings()} to retrieve and
* parse the current process environment
* @return The current process environment as a {@link Map}.
* @throws LastErrorException if failed to get or free the environment
* data block
* @see #getEnvironmentVariables(Pointer, long)
*/
public static Map<String,String> getEnvironmentVariables() {
Pointer lpszEnvironmentBlock=Kernel32.INSTANCE.GetEnvironmentStrings();
if (lpszEnvironmentBlock == null) {
throw new LastErrorException(Kernel32.INSTANCE.GetLastError());
}
try {
return getEnvironmentVariables(lpszEnvironmentBlock, 0L);
} finally {
if (!Kernel32.INSTANCE.FreeEnvironmentStrings(lpszEnvironmentBlock)) {
throw new LastErrorException(Kernel32.INSTANCE.GetLastError());
}
}
}
/**
* @param lpszEnvironmentBlock The environment block as received from the
* <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx">GetEnvironmentStrings</A>
* function
* @param offset Offset within the block to parse the data
* @return A {@link Map} of the parsed <code>name=value</code> pairs.
* <B>Note:</B> if the environment block is {@code null} then {@code null}
* is returned instead of an empty map since we want to distinguish
* between the case that the data block is {@code null} and when there are
* no environment variables (as unlikely as it may be)
*/
public static Map<String,String> getEnvironmentVariables(Pointer lpszEnvironmentBlock, long offset) {
if (lpszEnvironmentBlock == null) {
return null;
}
Map<String,String> vars=new TreeMap<String,String>();
boolean asWideChars=isWideCharEnvironmentStringBlock(lpszEnvironmentBlock, offset);
long stepFactor=asWideChars ? 2L : 1L;
for (long curOffset=offset; ; ) {
String nvp=readEnvironmentStringBlockEntry(lpszEnvironmentBlock, curOffset, asWideChars);
int len=nvp.length();
if (len == 0) { // found the ending '\0'
break;
}
int pos=nvp.indexOf('=');
if (pos < 0) {
throw new IllegalArgumentException("Missing variable value separator in " + nvp);
}
String name=nvp.substring(0, pos), value=nvp.substring(pos + 1);
vars.put(name, value);
curOffset += (len + 1 /* skip the ending '\0' */) * stepFactor;
}
return vars;
}
/**
* @param lpszEnvironmentBlock The environment block as received from the
* <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx">GetEnvironmentStrings</A>
* function
* @param offset Offset within the block to look for the entry
* @param asWideChars If {@code true} then the block contains {@code wchar_t}
* instead of "plain old" {@code char}s
* @return A {@link String} containing the <code>name=value</code> pair or
* empty if reached end of block
* @see #isWideCharEnvironmentStringBlock
* @see #findEnvironmentStringBlockEntryEnd
*/
public static String readEnvironmentStringBlockEntry(Pointer lpszEnvironmentBlock, long offset, boolean asWideChars) {
long endOffset=findEnvironmentStringBlockEntryEnd(lpszEnvironmentBlock, offset, asWideChars);
int dataLen=(int) (endOffset - offset);
if (dataLen == 0) {
return "";
}
int charsLen=asWideChars ? (dataLen / 2) : dataLen;
char[] chars=new char[charsLen];
long curOffset=offset, stepSize=asWideChars ? 2L : 1L;
ByteOrder byteOrder=ByteOrder.nativeOrder();
for (int index=0; index < chars.length; index++, curOffset += stepSize) {
byte b=lpszEnvironmentBlock.getByte(curOffset);
if (asWideChars) {
byte x=lpszEnvironmentBlock.getByte(curOffset + 1L);
if (ByteOrder.LITTLE_ENDIAN.equals(byteOrder)) {
chars[index] = (char) (((x << Byte.SIZE) & 0xFF00) | (b & 0x00FF));
} else { // unlikely, but handle it
chars[index] = (char) (((b << Byte.SIZE) & 0xFF00) | (x & 0x00FF));
}
} else {
chars[index] = (char) (b & 0x00FF);
}
}
return new String(chars);
}
/**
* @param lpszEnvironmentBlock The environment block as received from the
* <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx">GetEnvironmentStrings</A>
* function
* @param offset Offset within the block to look for the entry
* @param asWideChars If {@code true} then the block contains {@code wchar_t}
* instead of "plain old" {@code char}s
* @return The offset of the <U>first</U> {@code '\0'} in the data block
* starting at the specified offset - can be the start offset itself if empty
* string.
* @see #isWideCharEnvironmentStringBlock
*/
public static long findEnvironmentStringBlockEntryEnd(Pointer lpszEnvironmentBlock, long offset, boolean asWideChars) {
for (long curOffset=offset, stepSize=asWideChars ? 2L : 1L; ; curOffset += stepSize) {
byte b=lpszEnvironmentBlock.getByte(curOffset);
if (b == 0) {
return curOffset;
}
}
}
/**
* <P>Attempts to determine whether the data block uses {@code wchar_t}
* instead of "plain old" {@code char}s. It does that by reading
* 2 bytes from the specified offset - the character value and its charset
* indicator - and examining them as follows:</P>
* <UL>
* <LI>
* If the charset indicator is non-zero then it is assumed to be
* a "plain old" {@code char}s data block. <B>Note:</B>
* the assumption is that the environment variable <U>name</U> (at
* least) is ASCII.
* </LI>
*
* <LI>
* Otherwise (i.e., zero charset indicator), it is assumed to be
* a {@code wchar_t}
* </LI>
* </UL>
* <B>Note:</B> the code takes into account the {@link ByteOrder} even though
* only {@link ByteOrder#LITTLE_ENDIAN} is the likely one
* @param lpszEnvironmentBlock The environment block as received from the
* <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx">GetEnvironmentStrings</A>
* function
* @param offset offset
* @return {@code true} if the block contains {@code wchar_t} instead of
* "plain old" {@code char}s
*/
public static boolean isWideCharEnvironmentStringBlock(Pointer lpszEnvironmentBlock, long offset) {
byte b0=lpszEnvironmentBlock.getByte(offset);
byte b1=lpszEnvironmentBlock.getByte(offset + 1L);
ByteOrder byteOrder=ByteOrder.nativeOrder();
if (ByteOrder.LITTLE_ENDIAN.equals(byteOrder)) {
return isWideCharEnvironmentStringBlock(b1);
} else {
return isWideCharEnvironmentStringBlock(b0);
}
}
private static boolean isWideCharEnvironmentStringBlock(byte charsetIndicator) {
// assume wchar_t for environment variables represents ASCII letters
if (charsetIndicator != 0) {
return false;
} else {
return true;
}
}
/**
* Retrieves an integer associated with a key in the specified section of an
* initialization file.
*
* @param appName
* The name of the section in the initialization file.
* @param keyName
* The name of the key whose value is to be retrieved. This value
* is in the form of a string; the
* {@link Kernel32#GetPrivateProfileInt} function converts the
* string into an integer and returns the integer.
* @param defaultValue
* The default value to return if the key name cannot be found in
* the initialization file.
* @param fileName
* The name of the initialization file. If this parameter does
* not contain a full path to the file, the system searches for
* the file in the Windows directory.
* @return The retrieved integer, or the default if not found.
*/
public static final int getPrivateProfileInt(final String appName,
final String keyName, final int defaultValue, final String fileName) {
return Kernel32.INSTANCE.GetPrivateProfileInt(appName, keyName,
defaultValue, fileName);
}
/**
* Retrieves a string from the specified section in an initialization file.
*
* @param lpAppName
* The name of the section containing the key name. If this
* parameter is {@code null}, the
* {@link Kernel32#GetPrivateProfileString} function copies all
* section names in the file to the supplied buffer.
* @param lpKeyName
* The name of the key whose associated string is to be
* retrieved. If this parameter is {@code null}, all key names in
* the section specified by the {@code lpAppName} parameter are
* returned.
* @param lpDefault
* A default string. If the {@code lpKeyName} key cannot be found
* in the initialization file,
* {@link Kernel32#GetPrivateProfileString} returns the default.
* If this parameter is {@code null}, the default is an empty
* string, {@code ""}.
* <p>
* Avoid specifying a default string with trailing blank
* characters. The function inserts a {@code null} character in
* the {@code lpReturnedString} buffer to strip any trailing
* blanks.
* </p>
* @param lpFileName
* The name of the initialization file. If this parameter does
* not contain a full path to the file, the system searches for
* the file in the Windows directory.
* @return <p>
* If neither {@code lpAppName} nor {@code lpKeyName} is
* {@code null} and the destination buffer is too small to hold the
* requested string, the string is truncated.
* </p>
* <p>
* If either {@code lpAppName} or {@code lpKeyName} is {@code null}
* and the destination buffer is too small to hold all the strings,
* the last string is truncated and followed by two {@code null}
* characters.
* </p>
* <p>
* In the event the initialization file specified by
* {@code lpFileName} is not found, or contains invalid values, this
* function will set errorno with a value of '0x2' (File Not Found).
* To retrieve extended error information, call
* {@link Kernel32#GetLastError}.
* </p>
*/
public static final String getPrivateProfileString(final String lpAppName,
final String lpKeyName, final String lpDefault,
final String lpFileName) {
final char buffer[] = new char[1024];
Kernel32.INSTANCE.GetPrivateProfileString(lpAppName, lpKeyName,
lpDefault, buffer, new DWORD(buffer.length), lpFileName);
return Native.toString(buffer);
}
public static final void writePrivateProfileString(final String appName,
final String keyName, final String string, final String fileName) {
if (!Kernel32.INSTANCE.WritePrivateProfileString(appName, keyName,
string, fileName))
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
/**
* Convenience method to get the processor information. Takes care of
* auto-growing the array.
*
* @return the array of processor information.
*/
public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] getLogicalProcessorInformation() {
int sizePerStruct = new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION()
.size();
WinDef.DWORDByReference bufferSize = new WinDef.DWORDByReference(
new WinDef.DWORD(sizePerStruct));
Memory memory;
while (true) {
memory = new Memory(bufferSize.getValue().intValue());
if (!Kernel32.INSTANCE.GetLogicalProcessorInformation(memory,
bufferSize)) {
int err = Kernel32.INSTANCE.GetLastError();
if (err != WinError.ERROR_INSUFFICIENT_BUFFER)
throw new Win32Exception(err);
} else {
break;
}
}
WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION firstInformation = new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION(
memory);
int returnedStructCount = bufferSize.getValue().intValue()
/ sizePerStruct;
return (WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]) firstInformation
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[returnedStructCount]);
}
/**
* Convenience method to get the processor information. Takes care of
* auto-growing the array and populating variable-length arrays in
* structures.
*
* @param relationshipType
* The type of relationship to retrieve. This parameter can be
* one of the following values:
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationCache},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationGroup},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationNumaNode},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorCore},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorPackage},
* or {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationAll}
* @return the array of processor information.
*/
public static final SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[] getLogicalProcessorInformationEx(
int relationshipType) {
WinDef.DWORDByReference bufferSize = new WinDef.DWORDByReference(new WinDef.DWORD(1));
Memory memory;
while (true) {
memory = new Memory(bufferSize.getValue().intValue());
if (!Kernel32.INSTANCE.GetLogicalProcessorInformationEx(relationshipType, memory, bufferSize)) {
int err = Kernel32.INSTANCE.GetLastError();
if (err != WinError.ERROR_INSUFFICIENT_BUFFER)
throw new Win32Exception(err);
} else {
break;
}
}
// Array elements have variable size; iterate to populate array
List<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX> procInfoList = new ArrayList<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>();
int offset = 0;
while (offset < bufferSize.getValue().intValue()) {
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX information = SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
.fromPointer(memory.share(offset));
procInfoList.add(information);
offset += information.size;
}
return procInfoList.toArray(new SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[0]);
}
// Prevents useless heap pollution
private static final String[] EMPTY_STRING_ARRAY = new String[0];
/**
* Retrieves all the keys and values for the specified section of an initialization file.
*
* <p>
* Each string has the following format: {@code key=string}.
* </p>
* <p>
* This operation is atomic; no updates to the specified initialization file are allowed while this method is executed.
* </p>
*
* @param appName
* The name of the section in the initialization file.
* @param fileName
* The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the
* Windows directory.
* @return The key name and value pairs associated with the named section.
*/
public static final String[] getPrivateProfileSection(final String appName, final String fileName) {
final char buffer[] = new char[32768]; // Maximum section size according to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724348(v=vs.85).aspx)
if (Kernel32.INSTANCE.GetPrivateProfileSection(appName, buffer, new DWORD(buffer.length), fileName).intValue() == 0) {
final int lastError = Kernel32.INSTANCE.GetLastError();
if (lastError == Kernel32.ERROR_SUCCESS) {
return EMPTY_STRING_ARRAY;
} else {
throw new Win32Exception(lastError);
}
}
return new String(buffer).split("\0");
}
/**
* Retrieves the names of all sections in an initialization file.
* <p>
* This operation is atomic; no updates to the initialization file are allowed while this method is executed.
* </p>
*
* @param fileName
* The name of the initialization file. If this parameter is {@code NULL}, the function searches the Win.ini file. If this parameter does not
* contain a full path to the file, the system searches for the file in the Windows directory.
* @return the section names associated with the named file.
*/
public static final String[] getPrivateProfileSectionNames(final String fileName) {
final char buffer[] = new char[65536]; // Maximum INI file size according to MSDN (http://support.microsoft.com/kb/78346)
if (Kernel32.INSTANCE.GetPrivateProfileSectionNames(buffer, new DWORD(buffer.length), fileName).intValue() == 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return new String(buffer).split("\0");
}
/**
* @param appName
* The name of the section in which data is written. This section name is typically the name of the calling application.
* @param strings
* The new key names and associated values that are to be written to the named section. Each entry must be of the form {@code key=value}.
* @param fileName
* The name of the initialization file. If this parameter does not contain a full path for the file, the function searches the Windows directory
* for the file. If the file does not exist and lpFileName does not contain a full path, the function creates the file in the Windows directory.
*/
public static final void writePrivateProfileSection(final String appName, final String[] strings, final String fileName) {
final StringBuilder buffer = new StringBuilder();
for (final String string : strings)
buffer.append(string).append('\0');
buffer.append('\0');
if (! Kernel32.INSTANCE.WritePrivateProfileSection(appName, buffer.toString(), fileName)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
/**
* Invokes the {@link Kernel32#QueryDosDevice(String, char[], int)} method
* and parses the result
* @param lpszDeviceName The device name
* @param maxTargetSize The work buffer size to use for the query
* @return The parsed result
*/
public static final List<String> queryDosDevice(String lpszDeviceName, int maxTargetSize) {
char[] lpTargetPath = new char[maxTargetSize];
int dwSize = Kernel32.INSTANCE.QueryDosDevice(lpszDeviceName, lpTargetPath, lpTargetPath.length);
if (dwSize == 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return Native.toStringList(lpTargetPath, 0, dwSize);
}
/**
* Invokes and parses the result of {@link Kernel32#GetVolumePathNamesForVolumeName(String, char[], int, IntByReference)}
* @param lpszVolumeName The volume name
* @return The parsed result
* @throws Win32Exception If failed to retrieve the required information
*/
public static final List<String> getVolumePathNamesForVolumeName(String lpszVolumeName) {
char[] lpszVolumePathNames = new char[WinDef.MAX_PATH + 1];
IntByReference lpcchReturnLength = new IntByReference();
if (!Kernel32.INSTANCE.GetVolumePathNamesForVolumeName(lpszVolumeName, lpszVolumePathNames, lpszVolumePathNames.length, lpcchReturnLength)) {
int hr = Kernel32.INSTANCE.GetLastError();
if (hr != WinError.ERROR_MORE_DATA) {
throw new Win32Exception(hr);
}
int required = lpcchReturnLength.getValue();
lpszVolumePathNames = new char[required];
// this time we MUST succeed
if (!Kernel32.INSTANCE.GetVolumePathNamesForVolumeName(lpszVolumeName, lpszVolumePathNames, lpszVolumePathNames.length, lpcchReturnLength)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
int bufSize = lpcchReturnLength.getValue();
return Native.toStringList(lpszVolumePathNames, 0, bufSize);
}
// prefix and suffix of a volume GUID path
public static final String VOLUME_GUID_PATH_PREFIX = "\\\\?\\Volume{";
public static final String VOLUME_GUID_PATH_SUFFIX = "}\\";
/**
* Parses and returns the pure GUID value of a volume name obtained
* from {@link Kernel32#FindFirstVolume(char[], int)} or
* {@link Kernel32#FindNextVolume} calls
*
* @param volumeGUIDPath
* The volume GUID path as returned by one of the above mentioned calls
* @return The pure GUID value after stripping the "\\?\" prefix and
* removing the trailing backslash.
* @throws IllegalArgumentException if bad format encountered
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365248(v=vs.85).aspx">Naming a Volume</A>
*/
public static final String extractVolumeGUID(String volumeGUIDPath) {
if ((volumeGUIDPath == null)
|| (volumeGUIDPath.length() <= (VOLUME_GUID_PATH_PREFIX.length() + VOLUME_GUID_PATH_SUFFIX.length()))
|| (!volumeGUIDPath.startsWith(VOLUME_GUID_PATH_PREFIX))
|| (!volumeGUIDPath.endsWith(VOLUME_GUID_PATH_SUFFIX))) {
throw new IllegalArgumentException("Bad volume GUID path format: " + volumeGUIDPath);
}
return volumeGUIDPath.substring(VOLUME_GUID_PATH_PREFIX.length(), volumeGUIDPath.length() - VOLUME_GUID_PATH_SUFFIX.length());
}
/**
* This function retrieves the full path of the executable file of a given process identifier.
*
* @param pid
* Identifier for the running process
* @param dwFlags
* 0 - The name should use the Win32 path format.
* 1(WinNT.PROCESS_NAME_NATIVE) - The name should use the native system path format.
*
* @return the full path of the process's executable file of null if failed. To get extended error information,
* call GetLastError.
*/
public static final String QueryFullProcessImageName(int pid, int dwFlags) {
HANDLE hProcess = null;
Win32Exception we = null;
try {
hProcess = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION | WinNT.PROCESS_VM_READ, false, pid);
if (hProcess == null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return QueryFullProcessImageName(hProcess, dwFlags);
} catch (Win32Exception e) {
we = e;
throw we; // re-throw to invoke finally block
} finally {
try {
closeHandle(hProcess);
} catch (Win32Exception e) {
if (we == null) {
we = e;
} else {
we.addSuppressed(e);
}
}
if (we != null) {
throw we;
}
}
}
/**
*
* This function retrieves the full path of the executable file of a given process.
*
* @param hProcess
* Handle for the running process
* @param dwFlags
* 0 - The name should use the Win32 path format.
* 1(WinNT.PROCESS_NAME_NATIVE) - The name should use the native system path format.
*
* @return the full path of the process's executable file of null if failed. To get extended error information,
* call GetLastError.
*/
public static final String QueryFullProcessImageName(HANDLE hProcess, int dwFlags) {
int size = WinDef.MAX_PATH; // Start with MAX_PATH, then increment with 1024 each iteration
IntByReference lpdwSize = new IntByReference();
do {
char[] lpExeName = new char[size];
lpdwSize.setValue(size);
if (Kernel32.INSTANCE.QueryFullProcessImageName(hProcess, dwFlags, lpExeName, lpdwSize)) {
return new String(lpExeName, 0, lpdwSize.getValue());
}
size += 1024;
} while (Kernel32.INSTANCE.GetLastError() == Kernel32.ERROR_INSUFFICIENT_BUFFER);
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
/**
* Gets the specified resource out of the specified executable file
*
* @param path
* The path to the executable file
* @param type
* The type of the resource (either a type name or type ID is
* allowed)
* @param name
* The name or ID of the resource
* @return The resource bytes, or null if no such resource exists.
* @throws IllegalStateException if the call to LockResource fails
*/
public static byte[] getResource(String path, String type, String name) {
HMODULE target = Kernel32.INSTANCE.LoadLibraryEx(path, null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
if (target == null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
Win32Exception err = null;
Pointer start = null;
int length = 0;
byte[] results = null;
try {
Pointer t = null;
try {
t = new Pointer(Long.parseLong(type));
} catch (NumberFormatException e) {
t = new Memory(Native.WCHAR_SIZE * (type.length() + 1));
t.setWideString(0, type);
}
Pointer n = null;
try {
n = new Pointer(Long.parseLong(name));
} catch (NumberFormatException e) {
n = new Memory(Native.WCHAR_SIZE * (name.length() + 1));
n.setWideString(0, name);
}
HRSRC hrsrc = Kernel32.INSTANCE.FindResource(target, n, t);
if (hrsrc == null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
// according to MSDN, on 32 bit Windows or newer, calling FreeResource() is not necessary - and in fact does nothing but return false.
HANDLE loaded = Kernel32.INSTANCE.LoadResource(target, hrsrc);
if (loaded == null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
length = Kernel32.INSTANCE.SizeofResource(target, hrsrc);
if (length == 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
// MSDN: It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.
// MSDN does not say that LockResource sets GetLastError
start = Kernel32.INSTANCE.LockResource(loaded);
if (start == null) {
throw new IllegalStateException("LockResource returned null.");
}
// have to capture it into a byte array before you free the library, otherwise bad things happen.
results = start.getByteArray(0, length);
} catch (Win32Exception we) {
err = we;
} finally {