/
Kernel32Test.java
2111 lines (1772 loc) · 92.4 KB
/
Kernel32Test.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) 2007 Timothy Wall, 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 static com.sun.jna.platform.win32.WinBase.WAIT_OBJECT_0;
import static com.sun.jna.platform.win32.WinNT.MEM_COMMIT;
import static com.sun.jna.platform.win32.WinNT.MEM_RESERVE;
import static com.sun.jna.platform.win32.WinNT.PAGE_EXECUTE_READWRITE;
import static com.sun.jna.platform.win32.WinioctlUtil.FSCTL_GET_COMPRESSION;
import static com.sun.jna.platform.win32.WinioctlUtil.FSCTL_GET_REPARSE_POINT;
import static com.sun.jna.platform.win32.WinioctlUtil.FSCTL_SET_COMPRESSION;
import static com.sun.jna.platform.win32.WinioctlUtil.FSCTL_SET_REPARSE_POINT;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.NativeMappedConverter;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTRByReference;
import com.sun.jna.platform.win32.COM.COMUtils;
import com.sun.jna.platform.win32.Ntifs.REPARSE_DATA_BUFFER;
import com.sun.jna.platform.win32.Ntifs.SymbolicLinkReparseBuffer;
import com.sun.jna.platform.win32.WinBase.FILETIME;
import com.sun.jna.platform.win32.WinBase.FILE_ATTRIBUTE_TAG_INFO;
import com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO;
import com.sun.jna.platform.win32.WinBase.FILE_COMPRESSION_INFO;
import com.sun.jna.platform.win32.WinBase.FILE_DISPOSITION_INFO;
import com.sun.jna.platform.win32.WinBase.FILE_ID_INFO;
import com.sun.jna.platform.win32.WinBase.FILE_STANDARD_INFO;
import com.sun.jna.platform.win32.WinBase.MEMORYSTATUSEX;
import com.sun.jna.platform.win32.WinBase.SYSTEMTIME;
import com.sun.jna.platform.win32.WinBase.SYSTEM_INFO;
import com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.DWORDByReference;
import com.sun.jna.platform.win32.WinDef.HMODULE;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.USHORT;
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.MEMORY_BASIC_INFORMATION;
import com.sun.jna.platform.win32.WinNT.OSVERSIONINFO;
import com.sun.jna.platform.win32.WinNT.OSVERSIONINFOEX;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.ShortByReference;
import junit.framework.TestCase;
import org.junit.Assume;
public class Kernel32Test extends TestCase {
public static void main(String[] args) {
OSVERSIONINFO lpVersionInfo = new OSVERSIONINFO();
assertTrue(Kernel32.INSTANCE.GetVersionEx(lpVersionInfo));
System.out.println("Operating system: "
+ lpVersionInfo.dwMajorVersion.longValue() + "." + lpVersionInfo.dwMinorVersion.longValue()
+ " (" + lpVersionInfo.dwBuildNumber + ")"
+ " [" + Native.toString(lpVersionInfo.szCSDVersion) + "]");
junit.textui.TestRunner.run(Kernel32Test.class);
}
// see https://github.com/java-native-access/jna/issues/604
public void testGetLastErrorNativeLibraryOverride() {
assertFalse("Unexpected success", Kernel32.INSTANCE.CloseHandle(null));
assertEquals("Mismatched error code", WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
}
// see https://github.com/twall/jna/issues/482
public void testNoDuplicateMethodsNames() {
Collection<String> dupSet = AbstractWin32TestSupport.detectDuplicateMethods(Kernel32.class);
if (!dupSet.isEmpty()) {
for (String name : new String[]{
// has 2 overloads by design since the API accepts both OSVERSIONINFO and OSVERSIONINFOEX
"GetVersionEx",
// one version is kind-of broken and retained for compatiblity (deprecated)
"CreateRemoteThread"
}) {
dupSet.remove(name);
}
}
assertTrue("Duplicate methods found: " + dupSet, dupSet.isEmpty());
}
public void testGetDriveType() {
if (!Platform.isWindows()) return;
Kernel32 kernel = Kernel32.INSTANCE;
assertEquals("Wrong drive type.", WinBase.DRIVE_FIXED, kernel.GetDriveType("c:"));
}
public void testStructureOutArgument() {
Kernel32 kernel = Kernel32.INSTANCE;
WinBase.SYSTEMTIME time = new WinBase.SYSTEMTIME();
kernel.GetSystemTime(time);
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
assertEquals("Hour not properly set",
cal.get(Calendar.HOUR_OF_DAY), time.wHour);
assertEquals("Day not properly set",
cal.get(Calendar.DAY_OF_WEEK)-1,
time.wDayOfWeek);
assertEquals("Year not properly set",
cal.get(Calendar.YEAR), time.wYear);
}
public void testSetSystemTime() {
Kernel32 kernel = Kernel32.INSTANCE;
WinBase.SYSTEMTIME time = new WinBase.SYSTEMTIME();
kernel.GetSystemTime(time);
try {
WinBase.SYSTEMTIME expected = new WinBase.SYSTEMTIME();
expected.wYear = time.wYear;
expected.wMonth = time.wMonth;
expected.wDay = time.wDay;
expected.wHour = time.wHour;
expected.wMinute = time.wMinute;
expected.wSecond = time.wSecond;
expected.wMilliseconds = time.wMilliseconds;
if (expected.wHour > 0) {
expected.wHour--;
} else {
expected.wHour++;
}
if (assertTimeSettingOperationSucceeded("SetSystemTime", kernel.SetSystemTime(expected))) {
WinBase.SYSTEMTIME actual = new WinBase.SYSTEMTIME();
kernel.GetSystemTime(actual);
assertEquals("Mismatched hour value", expected.wHour, actual.wHour);
}
} finally {
assertTimeSettingOperationSucceeded("Restore original system time", kernel.SetSystemTime(time));
}
}
public void testSetLocaltime() {
Kernel32 kernel = Kernel32.INSTANCE;
WinBase.SYSTEMTIME time = new WinBase.SYSTEMTIME();
kernel.GetLocalTime(time);
try {
WinBase.SYSTEMTIME expected = new WinBase.SYSTEMTIME();
expected.wYear = time.wYear;
expected.wMonth = time.wMonth;
expected.wDay = time.wDay;
expected.wHour = time.wHour;
expected.wMinute = time.wMinute;
expected.wSecond = time.wSecond;
expected.wMilliseconds = time.wMilliseconds;
if (expected.wHour > 0) {
expected.wHour--;
} else {
expected.wHour++;
}
if (assertTimeSettingOperationSucceeded("SetLocalTime", kernel.SetLocalTime(expected))) {
WinBase.SYSTEMTIME actual = new WinBase.SYSTEMTIME();
kernel.GetLocalTime(actual);
assertEquals("Mismatched hour value", expected.wHour, actual.wHour);
}
} finally {
assertTimeSettingOperationSucceeded("Restore local time", kernel.SetLocalTime(time));
}
}
private static boolean assertTimeSettingOperationSucceeded(String message, boolean result) {
if (result) {
return result;
}
int hr=Kernel32.INSTANCE.GetLastError();
/*
* Check special error in case the user running the test isn't allowed
* to change the time. This can happen for hosts that are managed
* by some central administrator using an automated time setting mechanism.
* In such cases, the user running the tests might not have admin
* privileges and it may be too much to ask to have them just for running
* this JNA API test...
*/
if (hr == WinError.ERROR_PRIVILEGE_NOT_HELD) {
return false; // don't fail the test, but signal the failure
}
if (hr != WinError.ERROR_SUCCESS) {
fail(message + " failed: hr=" + hr);
} else {
fail(message + " unknown failure reason code");
}
return false;
}
public void testGetLastError() {
Kernel32 kernel = Kernel32.INSTANCE;
int ERRCODE = 8;
kernel.SetLastError(ERRCODE);
int code = kernel.GetLastError();
assertEquals("Wrong error value after SetLastError", ERRCODE, code);
if (kernel.GetProcessVersion(-1) == 0) {
final int INVALID_PARAMETER = 87;
code = kernel.GetLastError();
assertEquals("Wrong error value after failed syscall", INVALID_PARAMETER, code);
}
else {
fail("GetProcessId(NULL) should fail");
}
}
public void testConvertHWND_BROADCAST() {
HWND hwnd = WinUser.HWND_BROADCAST;
NativeMappedConverter.getInstance(hwnd.getClass()).toNative(hwnd, null);
}
public void testGetComputerName() {
IntByReference lpnSize = new IntByReference(0);
assertFalse(Kernel32.INSTANCE.GetComputerName(null, lpnSize));
assertEquals(WinError.ERROR_BUFFER_OVERFLOW, Kernel32.INSTANCE.GetLastError());
char buffer[] = new char[WinBase.MAX_COMPUTERNAME_LENGTH + 1];
lpnSize.setValue(buffer.length);
assertTrue(Kernel32.INSTANCE.GetComputerName(buffer, lpnSize));
}
public void testGetComputerNameExSameAsGetComputerName() {
IntByReference lpnSize = new IntByReference(0);
char buffer[] = new char[WinBase.MAX_COMPUTERNAME_LENGTH + 1];
lpnSize.setValue(buffer.length);
assertTrue("Failed to retrieve expected computer name", Kernel32.INSTANCE.GetComputerName(buffer, lpnSize));
String expected = Native.toString(buffer);
// reset
lpnSize.setValue(buffer.length);
Arrays.fill(buffer, '\0');
assertTrue("Failed to retrieve extended computer name", Kernel32.INSTANCE.GetComputerNameEx(WinBase.COMPUTER_NAME_FORMAT.ComputerNameNetBIOS, buffer, lpnSize));
String actual = Native.toString(buffer);
assertEquals("Mismatched names", expected, actual);
}
public void testWaitForSingleObject() {
HANDLE handle = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
assertNotNull("Failed to create event: " + Kernel32.INSTANCE.GetLastError(), handle);
try {
// handle runs into timeout since it is not triggered
// WAIT_TIMEOUT = 0x00000102
assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
} finally {
Kernel32Util.closeHandle(handle);
}
}
public void testOpenEvent() {
HANDLE handle = null, handle2 = null;
try {
handle = Kernel32.INSTANCE.CreateEvent(null, false, false, "jna-kernel32test");
assertNotNull("Failed to create event: " + Kernel32.INSTANCE.GetLastError(), handle);
handle2 = Kernel32.INSTANCE.OpenEvent(WinNT.EVENT_MODIFY_STATE, false, "jna-kernel32test");
assertNotNull("Failed to open event: " + Kernel32.INSTANCE.GetLastError(), handle2);
Kernel32.INSTANCE.SetEvent(handle2);
assertEquals(WinBase.WAIT_OBJECT_0, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
} finally {
Kernel32Util.closeHandle(handle);
Kernel32Util.closeHandle(handle2);
}
}
public void testResetEvent() {
HANDLE handle = Kernel32.INSTANCE.CreateEvent(null, true, false, null);
assertNotNull("Failed to create event: " + Kernel32.INSTANCE.GetLastError(), handle);
try {
// set the event to the signaled state
Kernel32.INSTANCE.SetEvent(handle);
// This should return successfully
assertEquals(WinBase.WAIT_OBJECT_0, Kernel32.INSTANCE.WaitForSingleObject(
handle, 1000));
// now reset it to not signaled
Kernel32.INSTANCE.ResetEvent(handle);
// handle runs into timeout since it is not triggered
// WAIT_TIMEOUT = 0x00000102
assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
} finally {
Kernel32Util.closeHandle(handle);
}
}
public void testWaitForMultipleObjects(){
HANDLE[] handles = new HANDLE[2];
try {
for (int index = 0; index < handles.length; index++) {
HANDLE h = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
assertNotNull("Failed to create event #" + index + ": " + Kernel32.INSTANCE.GetLastError(), h);
handles[index] = h;
}
// handle runs into timeout since it is not triggered
// WAIT_TIMEOUT = 0x00000102
assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForMultipleObjects(
handles.length, handles, false, 1000));
} finally {
Kernel32Util.closeHandles(handles);
}
// invalid Handle
handles[0] = WinBase.INVALID_HANDLE_VALUE;
handles[1] = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
assertNotNull("Failed to create valid event: " + Kernel32.INSTANCE.GetLastError(), handles[1]);
try {
// returns WAIT_FAILED since handle is invalid
assertEquals(WinBase.WAIT_FAILED, Kernel32.INSTANCE.WaitForMultipleObjects(
handles.length, handles, false, 5000));
} finally {
Kernel32Util.closeHandle(handles[1]);
}
}
public void testGetCurrentThreadId() {
assertTrue(Kernel32.INSTANCE.GetCurrentThreadId() > 0);
}
public void testGetCurrentThread() {
HANDLE h = Kernel32.INSTANCE.GetCurrentThread();
assertNotNull("No current thread handle", h);
assertNotNull("Null current thread handle", h.getPointer());
}
public void testOpenThread() {
HANDLE h = Kernel32.INSTANCE.OpenThread(WinNT.THREAD_ALL_ACCESS, false,
Kernel32.INSTANCE.GetCurrentThreadId());
assertNotNull(h);
assertNotNull(h.getPointer());
Kernel32Util.closeHandle(h);
}
public void testGetCurrentProcessId() {
assertTrue(Kernel32.INSTANCE.GetCurrentProcessId() > 0);
}
public void testGetCurrentProcess() {
HANDLE h = Kernel32.INSTANCE.GetCurrentProcess();
assertNotNull("No current process handle", h);
assertNotNull("Null current process handle", h.getPointer());
}
public void testOpenProcess() {
HANDLE h = Kernel32.INSTANCE.OpenProcess(0, false,
Kernel32.INSTANCE.GetCurrentProcessId());
assertNull(h);
// opening your own process fails with access denied
assertEquals(WinError.ERROR_ACCESS_DENIED, Kernel32.INSTANCE.GetLastError());
}
public void testQueryFullProcessImageName() {
int pid = Kernel32.INSTANCE.GetCurrentProcessId();
HANDLE h = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION, false, pid);
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process ID=" + pid + " handle", h);
try {
char[] path = new char[WinDef.MAX_PATH];
IntByReference lpdwSize = new IntByReference(path.length);
boolean b = Kernel32.INSTANCE.QueryFullProcessImageName(h, 0, path, lpdwSize);
assertTrue("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to query process image name", b);
assertTrue("Failed to query process image name, empty path returned", lpdwSize.getValue() > 0);
} finally {
Kernel32Util.closeHandle(h);
}
}
public void testGetProcessTimes() {
int pid = Kernel32.INSTANCE.GetCurrentProcessId();
HANDLE h = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION, false, pid);
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process ID=" + pid + " handle", h);
try {
FILETIME lpCreationTime = new FILETIME();
FILETIME lpExitTime = new FILETIME();
FILETIME lpKernelTime = new FILETIME();
FILETIME lpUserTime = new FILETIME();
boolean b = Kernel32.INSTANCE.GetProcessTimes(h, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime);
assertTrue("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process times", b);
// Process must have started before now.
long upTimeMillis = System.currentTimeMillis() - lpCreationTime.toTime();
assertTrue(upTimeMillis >= 0);
// lpExitTime is undefined for a running process, do not test
// Kernel and User time must be < up time (in 100ns ticks)
assertTrue(lpKernelTime.toDWordLong().longValue()
+ lpUserTime.toDWordLong().longValue() < (upTimeMillis + 1L) * 10000L);
} finally {
Kernel32Util.closeHandle(h);
}
}
public void testGetProcessIoCounters() {
int pid = Kernel32.INSTANCE.GetCurrentProcessId();
HANDLE h = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION, false, pid);
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process ID=" + pid + " handle", h);
try {
WinNT.IO_COUNTERS lpIoCounters = new WinNT.IO_COUNTERS();
boolean b = Kernel32.INSTANCE.GetProcessIoCounters(h, lpIoCounters);
assertTrue("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process IO counters", b);
// IO must be nonzero
assertTrue(lpIoCounters.ReadOperationCount >= 0);
assertTrue(lpIoCounters.WriteOperationCount >= 0);
assertTrue(lpIoCounters.OtherOperationCount >= 0);
assertTrue(lpIoCounters.ReadTransferCount >= 0);
assertTrue(lpIoCounters.WriteTransferCount >= 0);
assertTrue(lpIoCounters.OtherTransferCount >= 0);
} finally {
Kernel32Util.closeHandle(h);
}
}
public void testGetAndSetProcessAffinityMask() {
// Pseudo handle, no need to close. Has PROCESS_ALL_ACCESS right.
HANDLE pHandle = Kernel32.INSTANCE.GetCurrentProcess();
assertNotNull(pHandle);
ULONG_PTRByReference pProcessAffinity = new ULONG_PTRByReference();
ULONG_PTRByReference pSystemAffinity = new ULONG_PTRByReference();
assertTrue("Failed to get affinity masks.",
Kernel32.INSTANCE.GetProcessAffinityMask(pHandle, pProcessAffinity, pSystemAffinity));
long processAffinity = pProcessAffinity.getValue().longValue();
long systemAffinity = pSystemAffinity.getValue().longValue();
if (systemAffinity == 0) {
// Rare case for process to be running in multiple processor groups, where both
// systemAffinity and processAffinity are 0 and we can't do anything else.
assertEquals(
"Both process and system affinity must be zero if this process is running in multiple processor groups",
processAffinity, systemAffinity);
} else {
// Test current affinity
assertEquals("Process affinity must be a subset of system affinity", processAffinity,
processAffinity & systemAffinity);
assertEquals("System affinity must be a superset of process affinity", systemAffinity,
processAffinity | systemAffinity);
// Set affinity to a single processor in the current system
long lowestOneBit = Long.lowestOneBit(systemAffinity);
ULONG_PTR dwProcessAffinityMask = new ULONG_PTR(lowestOneBit);
assertTrue("Failed to set affinity",
Kernel32.INSTANCE.SetProcessAffinityMask(pHandle, dwProcessAffinityMask));
assertTrue("Failed to get affinity masks.",
Kernel32.INSTANCE.GetProcessAffinityMask(pHandle, pProcessAffinity, pSystemAffinity));
assertEquals("Process affinity doesn't match what was just set", lowestOneBit,
pProcessAffinity.getValue().longValue());
// Now try to set affinity to an invalid processor
lowestOneBit = Long.lowestOneBit(~systemAffinity);
// In case we have exactly 64 processors we can't do this, otherwise...
if (lowestOneBit != 0) {
dwProcessAffinityMask = new ULONG_PTR(lowestOneBit);
assertFalse("Successfully set affinity when it should have failed",
Kernel32.INSTANCE.SetProcessAffinityMask(pHandle, dwProcessAffinityMask));
assertEquals("Last error should be ERROR_INVALID_PARAMETER", WinError.ERROR_INVALID_PARAMETER,
Kernel32.INSTANCE.GetLastError());
}
// Cleanup. Be nice and put affinity back where it started!
dwProcessAffinityMask = new ULONG_PTR(processAffinity);
assertTrue("Failed to restore affinity to original setting",
Kernel32.INSTANCE.SetProcessAffinityMask(pHandle, dwProcessAffinityMask));
}
}
public void testGetTempPath() {
char[] buffer = new char[WinDef.MAX_PATH];
assertTrue(Kernel32.INSTANCE.GetTempPath(new DWORD(WinDef.MAX_PATH), buffer).intValue() > 0);
}
public void testGetTickCount() throws InterruptedException {
// Tick count rolls over every 49.7 days, so to safeguard from
// roll-over, we will get two time spans. At least one should
// yield a positive.
int tick1 = Kernel32.INSTANCE.GetTickCount();
Thread.sleep(10);
int tick2 = Kernel32.INSTANCE.GetTickCount();
Thread.sleep(10);
int tick3 = Kernel32.INSTANCE.GetTickCount();
assertTrue(tick2 > tick1 || tick3 > tick2);
}
public void testGetTickCount64() throws InterruptedException {
long tick1 = Kernel32.INSTANCE.GetTickCount64();
Thread.sleep(100);
long tick2 = Kernel32.INSTANCE.GetTickCount64();
assertTrue(tick2 > tick1);
}
public void testGetVersion() {
DWORD version = Kernel32.INSTANCE.GetVersion();
assertTrue("Version high should be non-zero: 0x" + Integer.toHexString(version.getHigh().intValue()), version.getHigh().intValue() != 0);
assertTrue("Version low should be >= 0: 0x" + Integer.toHexString(version.getLow().intValue()), version.getLow().intValue() >= 0);
}
public void testGetVersionEx_OSVERSIONINFO() {
OSVERSIONINFO lpVersionInfo = new OSVERSIONINFO();
assertEquals(lpVersionInfo.size(), lpVersionInfo.dwOSVersionInfoSize.longValue());
assertTrue(Kernel32.INSTANCE.GetVersionEx(lpVersionInfo));
assertTrue(lpVersionInfo.dwMajorVersion.longValue() > 0);
assertTrue(lpVersionInfo.dwMinorVersion.longValue() >= 0);
assertEquals(lpVersionInfo.size(), lpVersionInfo.dwOSVersionInfoSize.longValue());
assertTrue(lpVersionInfo.dwPlatformId.longValue() > 0);
assertTrue(lpVersionInfo.dwBuildNumber.longValue() > 0);
assertTrue(Native.toString(lpVersionInfo.szCSDVersion).length() >= 0);
}
public void testGetVersionEx_OSVERSIONINFOEX() {
OSVERSIONINFOEX lpVersionInfo = new OSVERSIONINFOEX();
assertEquals(lpVersionInfo.size(), lpVersionInfo.dwOSVersionInfoSize.longValue());
assertTrue(Kernel32.INSTANCE.GetVersionEx(lpVersionInfo));
assertTrue(lpVersionInfo.getMajor() > 0);
assertTrue(lpVersionInfo.getMinor() >= 0);
assertEquals(lpVersionInfo.size(), lpVersionInfo.dwOSVersionInfoSize.longValue());
assertTrue(lpVersionInfo.getPlatformId() > 0);
assertTrue(lpVersionInfo.getBuildNumber() > 0);
assertTrue(lpVersionInfo.getServicePack().length() >= 0);
assertTrue(lpVersionInfo.getProductType() >= 0);
}
public void testGetSystemInfo() {
SYSTEM_INFO lpSystemInfo = new SYSTEM_INFO();
Kernel32.INSTANCE.GetSystemInfo(lpSystemInfo);
assertTrue(lpSystemInfo.dwNumberOfProcessors.intValue() > 0);
// the dwOemID member is obsolete, but gets a value.
// the pi member is a structure and isn't read by default
assertEquals(lpSystemInfo.processorArchitecture.dwOemID.getLow(),
lpSystemInfo.processorArchitecture.pi.wProcessorArchitecture);
}
public void testGetSystemTimes() {
Kernel32 kernel = Kernel32.INSTANCE;
FILETIME lpIdleTime = new FILETIME();
FILETIME lpKernelTime = new FILETIME();
FILETIME lpUserTime = new FILETIME();
boolean succ = kernel.GetSystemTimes(lpIdleTime, lpKernelTime, lpUserTime);
assertTrue(succ);
long idleTime = lpIdleTime.toDWordLong().longValue();
long kernelTime = lpKernelTime.toDWordLong().longValue();
long userTime = lpUserTime.toDWordLong().longValue();
// All should be >= 0. kernel includes idle.
assertTrue(idleTime >= 0);
assertTrue(kernelTime >= idleTime);
assertTrue(userTime >= 0);
}
public void testIsWow64Process() {
try {
IntByReference isWow64 = new IntByReference(42);
HANDLE hProcess = Kernel32.INSTANCE.GetCurrentProcess();
assertTrue(Kernel32.INSTANCE.IsWow64Process(hProcess, isWow64));
assertTrue(0 == isWow64.getValue() || 1 == isWow64.getValue());
} catch (UnsatisfiedLinkError e) {
// IsWow64Process is not available on this OS
}
}
public void testGetNativeSystemInfo() {
try {
SYSTEM_INFO lpSystemInfo = new SYSTEM_INFO();
Kernel32.INSTANCE.GetNativeSystemInfo(lpSystemInfo);
assertTrue(lpSystemInfo.dwNumberOfProcessors.intValue() > 0);
} catch (UnsatisfiedLinkError e) {
// only available under WOW64
}
}
public void testGlobalMemoryStatusEx() {
MEMORYSTATUSEX lpBuffer = new MEMORYSTATUSEX();
assertTrue(Kernel32.INSTANCE.GlobalMemoryStatusEx(lpBuffer));
assertTrue(lpBuffer.ullTotalPhys.longValue() > 0);
assertTrue(lpBuffer.dwMemoryLoad.intValue() >= 0 && lpBuffer.dwMemoryLoad.intValue() <= 100);
assertEquals(0, lpBuffer.ullAvailExtendedVirtual.intValue());
}
public void testDeleteFile() {
String filename = Kernel32Util.getTempPath() + "\\FileDoesNotExist.jna";
assertFalse(Kernel32.INSTANCE.DeleteFile(filename));
assertEquals(WinError.ERROR_FILE_NOT_FOUND, Kernel32.INSTANCE.GetLastError());
}
public void testReadFile() throws IOException {
String expected = "jna - testReadFile";
File tmp = File.createTempFile("testReadFile", "jna");
tmp.deleteOnExit();
try (FileWriter fw = new FileWriter(tmp)) {
fw.append(expected);
}
HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ, WinNT.FILE_SHARE_READ,
new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
assertFalse("Failed to create file handle: " + tmp, WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
byte[] readBuffer=new byte[expected.length() + Byte.MAX_VALUE];
IntByReference lpNumberOfBytesRead = new IntByReference(0);
assertTrue("Failed to read from file", Kernel32.INSTANCE.ReadFile(hFile, readBuffer, readBuffer.length, lpNumberOfBytesRead, null));
int read = lpNumberOfBytesRead.getValue();
assertEquals("Mismatched read size", expected.length(), read);
assertEquals("Mismatched read content", expected, new String(readBuffer, 0, read));
} finally {
Kernel32Util.closeHandle(hFile);
}
}
public void testSetHandleInformation() throws IOException {
File tmp = File.createTempFile("testSetHandleInformation", "jna");
tmp.deleteOnExit();
HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ, WinNT.FILE_SHARE_READ,
new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
assertTrue(Kernel32.INSTANCE.SetHandleInformation(hFile, WinBase.HANDLE_FLAG_PROTECT_FROM_CLOSE, 0));
} finally {
Kernel32Util.closeHandle(hFile);
}
}
public void testCreatePipe() {
HANDLEByReference hReadPipe = new HANDLEByReference();
HANDLEByReference hWritePipe = new HANDLEByReference();
try {
assertTrue(Kernel32.INSTANCE.CreatePipe(hReadPipe, hWritePipe, null, 0));
} finally {
Kernel32Util.closeHandleRefs(hReadPipe, hWritePipe);
}
}
public void testGetExitCodeProcess() {
IntByReference lpExitCode = new IntByReference(0);
assertTrue(Kernel32.INSTANCE.GetExitCodeProcess(Kernel32.INSTANCE.GetCurrentProcess(), lpExitCode));
assertEquals(WinBase.STILL_ACTIVE, lpExitCode.getValue());
}
public void testTerminateProcess() throws IOException {
File tmp = File.createTempFile("testTerminateProcess", "jna");
tmp.deleteOnExit();
HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ, WinNT.FILE_SHARE_READ,
new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
assertFalse(Kernel32.INSTANCE.TerminateProcess(hFile, 1));
assertEquals(WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
} finally {
Kernel32Util.closeHandle(hFile);
}
}
public void testGetFileAttributes() {
assertTrue(WinBase.INVALID_FILE_ATTRIBUTES != Kernel32.INSTANCE.GetFileAttributes("."));
}
public void testDeviceIoControlFsctlCompression() throws IOException {
File tmp = File.createTempFile("testDeviceIoControlFsctlCompression", "jna");
tmp.deleteOnExit();
HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_ALL, WinNT.FILE_SHARE_READ,
new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
ShortByReference lpBuffer = new ShortByReference();
IntByReference lpBytes = new IntByReference();
AbstractWin32TestSupport.assertCallSucceeded("DeviceIoControl",
Kernel32.INSTANCE.DeviceIoControl(hFile,
FSCTL_GET_COMPRESSION,
null,
0,
lpBuffer.getPointer(),
USHORT.SIZE,
lpBytes,
null));
assertEquals(WinNT.COMPRESSION_FORMAT_NONE, lpBuffer.getValue());
assertEquals(USHORT.SIZE, lpBytes.getValue());
lpBuffer = new ShortByReference((short)WinNT.COMPRESSION_FORMAT_LZNT1);
AbstractWin32TestSupport.assertCallSucceeded("DeviceIoControl",
Kernel32.INSTANCE.DeviceIoControl(hFile,
FSCTL_SET_COMPRESSION,
lpBuffer.getPointer(),
USHORT.SIZE,
null,
0,
lpBytes,
null));
AbstractWin32TestSupport.assertCallSucceeded("DeviceIoControl",
Kernel32.INSTANCE.DeviceIoControl(hFile,
FSCTL_GET_COMPRESSION,
null,
0,
lpBuffer.getPointer(),
USHORT.SIZE,
lpBytes,
null));
assertEquals(WinNT.COMPRESSION_FORMAT_LZNT1, lpBuffer.getValue());
assertEquals(USHORT.SIZE, lpBytes.getValue());
} finally {
Kernel32Util.closeHandle(hFile);
}
}
/**
* NOTE: Due to process elevation, this test must be run as administrator
* @throws IOException
*/
public void testDeviceIoControlFsctlReparse() throws IOException {
Path folder = Files.createTempDirectory("testDeviceIoControlFsctlReparse_FOLDER");
Path link = Files.createTempDirectory("testDeviceIoControlFsctlReparse_LINK");
File delFolder = folder.toFile();
delFolder.deleteOnExit();
File delLink = link.toFile();
delLink.deleteOnExit();
try (Advapi32Util.Privilege restore = new Advapi32Util.Privilege(WinNT.SE_RESTORE_NAME)) { // Required for FSCTL_SET_REPARSE_POINT
restore.enable();
HANDLE hFile = Kernel32.INSTANCE.CreateFile(link.toAbsolutePath().toString(),
WinNT.GENERIC_READ | WinNT.FILE_WRITE_ATTRIBUTES | WinNT.FILE_WRITE_EA,
WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE | WinNT.FILE_SHARE_DELETE,
new WinBase.SECURITY_ATTRIBUTES(),
WinNT.OPEN_EXISTING,
WinNT.FILE_ATTRIBUTE_DIRECTORY | WinNT.FILE_FLAG_BACKUP_SEMANTICS | WinNT.FILE_FLAG_OPEN_REPARSE_POINT,
null);
AbstractWin32TestSupport.assertCallSucceeded("CreateFile",
!WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
SymbolicLinkReparseBuffer symLinkReparseBuffer = new SymbolicLinkReparseBuffer(folder.getFileName().toString(),
folder.getFileName().toString(),
Ntifs.SYMLINK_FLAG_RELATIVE);
REPARSE_DATA_BUFFER lpBuffer = new REPARSE_DATA_BUFFER(WinNT.IO_REPARSE_TAG_SYMLINK, (short) 0, symLinkReparseBuffer);
AbstractWin32TestSupport.assertCallSucceeded("DeviceIoControl",
Kernel32.INSTANCE.DeviceIoControl(hFile,
FSCTL_SET_REPARSE_POINT,
lpBuffer.getPointer(),
lpBuffer.getSize(),
null,
0,
null,
null));
Memory p = new Memory(REPARSE_DATA_BUFFER.sizeOf());
IntByReference lpBytes = new IntByReference();
AbstractWin32TestSupport.assertCallSucceeded("DeviceIoControl",
Kernel32.INSTANCE.DeviceIoControl(hFile,
FSCTL_GET_REPARSE_POINT,
null,
0,
p,
(int) p.size(),
lpBytes,
null));
// Is a reparse point
lpBuffer = new REPARSE_DATA_BUFFER(p);
assertTrue(lpBytes.getValue() > 0);
assertEquals(WinNT.IO_REPARSE_TAG_SYMLINK, lpBuffer.ReparseTag);
assertEquals(folder.getFileName().toString(), lpBuffer.u.symLinkReparseBuffer.getPrintName());
assertEquals(folder.getFileName().toString(), lpBuffer.u.symLinkReparseBuffer.getSubstituteName());
} finally {
Kernel32Util.closeHandle(hFile);
}
}
}
public void testCopyFile() throws IOException {
File source = File.createTempFile("testCopyFile", "jna");
source.deleteOnExit();
File destination = new File(source.getParent(), source.getName() + "-destination");
destination.deleteOnExit();
Kernel32.INSTANCE.CopyFile(source.getCanonicalPath(), destination.getCanonicalPath(), true);
assertTrue(destination.exists());
}
public void testMoveFile() throws IOException {
File source = File.createTempFile("testMoveFile", "jna");
source.deleteOnExit();
File destination = new File(source.getParent(), source.getName() + "-destination");
destination.deleteOnExit();
Kernel32.INSTANCE.MoveFile(source.getCanonicalPath(), destination.getCanonicalPath());
assertTrue(destination.exists());
assertFalse(source.exists());
}
public void testMoveFileEx() throws IOException {
File source = File.createTempFile("testMoveFileEx", "jna");
source.deleteOnExit();
File destination = File.createTempFile("testCopyFile", "jna");
destination.deleteOnExit();
Kernel32.INSTANCE.MoveFileEx(source.getCanonicalPath(), destination.getCanonicalPath(), new DWORD(WinBase.MOVEFILE_REPLACE_EXISTING));
assertTrue(destination.exists());
assertFalse(source.exists());
}
public void testCreateProcess() {
WinBase.STARTUPINFO startupInfo = new WinBase.STARTUPINFO();
WinBase.PROCESS_INFORMATION.ByReference processInformation = new WinBase.PROCESS_INFORMATION.ByReference();
boolean status = Kernel32.INSTANCE.CreateProcess(
null,
"cmd.exe /c echo hi",
null,
null,
true,
new WinDef.DWORD(0),
Pointer.NULL,
System.getProperty("java.io.tmpdir"),
startupInfo,
processInformation);
assertTrue(status);
assertTrue(processInformation.dwProcessId.longValue() > 0);
}
public void testFindFirstFile() throws IOException {
Path tmpDir = Files.createTempDirectory("testFindFirstFile");
File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFile", ".jna").toString());
Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
HANDLE hFile = Kernel32.INSTANCE.FindFirstFile(tmpDir.toAbsolutePath().toString() + "\\*", p);
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
// Get data and confirm the 1st name is . for the directory itself.
WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
String actualFileName = fd.getFileName();
assertTrue(actualFileName.contentEquals("."));
// Get data and confirm the 2nd name is .. for the directory's parent
assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
fd = new WIN32_FIND_DATA(p);
actualFileName = fd.getFileName();
assertTrue(actualFileName.contentEquals(".."));
// Get data and confirm the 3rd name is the tmp file name
assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
fd = new WIN32_FIND_DATA(p);
actualFileName = fd.getFileName();
assertTrue(actualFileName.contentEquals(tmpFile.getName()));
// No more files in directory
assertFalse(Kernel32.INSTANCE.FindNextFile(hFile, p));
assertEquals(WinNT.ERROR_NO_MORE_FILES, Kernel32.INSTANCE.GetLastError());
}
finally {
Kernel32.INSTANCE.FindClose(hFile);
tmpFile.delete();
Files.delete(tmpDir);
}
}
public void testFindFirstFileExFindExInfoStandard() throws IOException {
Path tmpDir = Files.createTempDirectory("testFindFirstFileExFindExInfoStandard");
File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFileExFindExInfoStandard", ".jna").toString());
Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
HANDLE hFile = Kernel32.INSTANCE.FindFirstFileEx(tmpDir.toAbsolutePath().toString() + "\\*",
WinBase.FindExInfoStandard,
p,
WinBase.FindExSearchNameMatch,
null,
new DWORD(0));
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
// Get data and confirm the 1st name is . for the directory itself.
WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
String actualFileName = fd.getFileName();
assertTrue(actualFileName.contentEquals("."));
// Get data and confirm the 2nd name is .. for the directory's parent
assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
fd = new WIN32_FIND_DATA(p);
actualFileName = fd.getFileName();
assertTrue(actualFileName.contentEquals(".."));
// Get data and confirm the 3rd name is the tmp file name
assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
fd = new WIN32_FIND_DATA(p);
actualFileName = fd.getFileName();
assertTrue(actualFileName.contentEquals(tmpFile.getName()));
// No more files in directory
assertFalse(Kernel32.INSTANCE.FindNextFile(hFile, p));
assertEquals(WinNT.ERROR_NO_MORE_FILES, Kernel32.INSTANCE.GetLastError());
}
finally {
Kernel32.INSTANCE.FindClose(hFile);
tmpFile.delete();
Files.delete(tmpDir);
}
}
public void testFindFirstFileExFindExInfoBasic() throws IOException {
Path tmpDir = Files.createTempDirectory("testFindFirstFileExFindExInfoBasic");
File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFileExFindExInfoBasic", ".jna").toString());
Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
// Add the file name to the search to get just that one entry
HANDLE hFile = Kernel32.INSTANCE.FindFirstFileEx(tmpDir.toAbsolutePath().toString() + "\\" + tmpFile.getName(),
WinBase.FindExInfoBasic,
p,
WinBase.FindExSearchNameMatch,
null,
new DWORD(0));
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
// Get data and confirm the 1st name is for the file itself
WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
String actualFileName = fd.getFileName();
assertTrue(actualFileName.contentEquals(tmpFile.getName()));
// FindExInfoBasic does not return the short name, so confirm that its empty
String alternateFileName = fd.getAlternateFileName();
assertTrue(alternateFileName.isEmpty());
// No more files in directory
assertFalse(Kernel32.INSTANCE.FindNextFile(hFile, p));
assertEquals(WinNT.ERROR_NO_MORE_FILES, Kernel32.INSTANCE.GetLastError());
}
finally {