-
Notifications
You must be signed in to change notification settings - Fork 116
/
win32_api.hpp
1671 lines (1413 loc) · 60 KB
/
win32_api.hpp
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
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_INTERPROCESS_WIN32_API_HPP
#define BOOST_INTERPROCESS_WIN32_API_HPP
#ifndef BOOST_CONFIG_HPP
# include <boost/config.hpp>
#endif
#
#if defined(BOOST_HAS_PRAGMA_ONCE)
# pragma once
#endif
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <boost/assert.hpp>
#include <string>
#include <vector>
#ifdef BOOST_USE_WINDOWS_H
#include <windows.h>
#endif
#if defined(_MSC_VER)
# pragma once
# pragma comment( lib, "Advapi32.lib" )
# pragma comment( lib, "oleaut32.lib" )
# pragma comment( lib, "Ole32.lib" )
#endif
#if defined (BOOST_INTERPROCESS_WINDOWS)
# include <cstdarg>
# include <boost/detail/interlocked.hpp>
#else
# error "This file can only be included in Windows OS"
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Declaration of Windows structures or typedefs if BOOST_USE_WINDOWS_H is used
//
//////////////////////////////////////////////////////////////////////////////
#if defined(BOOST_GCC)
//Ignore -pedantic errors here (anonymous structs, etc.)
# if (BOOST_GCC >= 40600)
# pragma GCC diagnostic push
# if (BOOST_GCC >= 40800)
# pragma GCC diagnostic ignored "-Wpedantic"
# else
# pragma GCC diagnostic ignored "-pedantic"
# endif
# pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
# else
# pragma GCC system_header
# endif
//When loading DLLs we have no option but reinterpret casting function types
# if (BOOST_GCC >= 80000)
# pragma GCC diagnostic ignored "-Wcast-function-type"
# endif
#endif
//#define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
//#define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
#ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
# define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE 1
#else
# define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE 0
#endif
#ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
# define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE 1
#else
# define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE 0
#endif
#define BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM \
(BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE + \
BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE)
#if 1 < BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM
# error "Only one of \
BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED and \
BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED can be defined"
#endif
#if 0 == BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM
# define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
#endif
namespace boost {
namespace interprocess {
namespace winapi {
//Own defines
static const unsigned long MaxPath = 260;
//////////////////////////////////////////////////////////////////////////////
//
// Nt native structures
//
//////////////////////////////////////////////////////////////////////////////
struct interprocess_semaphore_basic_information
{
unsigned int count; // current semaphore count
unsigned int limit; // max semaphore count
};
struct interprocess_section_basic_information
{
void * base_address;
unsigned long section_attributes;
__int64 section_size;
};
struct file_rename_information_t {
int Replace;
void *RootDir;
unsigned long FileNameLength;
wchar_t FileName[1];
};
struct unicode_string_t {
unsigned short Length;
unsigned short MaximumLength;
wchar_t *Buffer;
};
struct object_attributes_t {
unsigned long Length;
void * RootDirectory;
unicode_string_t *ObjectName;
unsigned long Attributes;
void *SecurityDescriptor;
void *SecurityQualityOfService;
};
struct io_status_block_t {
union {
long Status;
void *Pointer;
};
unsigned long *Information;
};
union system_timeofday_information
{
struct data_t
{
__int64 liKeBootTime;
__int64 liKeSystemTime;
__int64 liExpTimeZoneBias;
unsigned long uCurrentTimeZoneId;
unsigned long dwReserved;
::boost::ulong_long_type ullBootTimeBias;
::boost::ulong_long_type ullSleepTimeBias;
} data;
unsigned char Reserved1[sizeof(data_t)];
};
static const long BootstampLength = sizeof(__int64);
static const long BootAndSystemstampLength = sizeof(__int64)*2;
static const long SystemTimeOfDayInfoLength = sizeof(system_timeofday_information::data_t);
struct object_name_information_t
{
unicode_string_t Name;
wchar_t NameBuffer[1];
};
enum file_information_class_t {
file_directory_information = 1,
file_full_directory_information,
file_both_directory_information,
file_basic_information,
file_standard_information,
file_internal_information,
file_ea_information,
file_access_information,
file_name_information,
file_rename_information,
file_link_information,
file_names_information,
file_disposition_information,
file_position_information,
file_full_ea_information,
file_mode_information,
file_alignment_information,
file_all_information,
file_allocation_information,
file_end_of_file_information,
file_alternate_name_information,
file_stream_information,
file_pipe_information,
file_pipe_local_information,
file_pipe_remote_information,
file_mailslot_query_information,
file_mailslot_set_information,
file_compression_information,
file_copy_on_write_information,
file_completion_information,
file_move_cluster_information,
file_quota_information,
file_reparse_point_information,
file_network_open_information,
file_object_id_information,
file_tracking_information,
file_ole_directory_information,
file_content_index_information,
file_inherit_content_index_information,
file_ole_information,
file_maximum_information
};
enum semaphore_information_class {
semaphore_basic_information = 0
};
enum system_information_class {
system_basic_information = 0,
system_performance_information = 2,
system_time_of_day_information = 3,
system_process_information = 5,
system_processor_performance_information = 8,
system_interrupt_information = 23,
system_exception_information = 33,
system_registry_quota_information = 37,
system_lookaside_information = 45
};
enum object_information_class
{
object_basic_information,
object_name_information,
object_type_information,
object_all_information,
object_data_information
};
enum section_information_class
{
section_basic_information,
section_image_information
};
} //namespace winapi {
} //namespace interprocess {
} //namespace boost {
//////////////////////////////////////////////////////////////////////////////
//
// Forward declaration of winapi
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/winapi/get_current_process_id.hpp>
#include <boost/winapi/get_current_thread_id.hpp>
#include <boost/winapi/get_current_process.hpp>
#include <boost/winapi/get_process_times.hpp>
#include <boost/winapi/error_codes.hpp>
#include <boost/winapi/thread.hpp>
#include <boost/winapi/system.hpp>
#include <boost/winapi/time.hpp>
#include <boost/winapi/timers.hpp>
#include <boost/winapi/get_last_error.hpp>
#include <boost/winapi/handles.hpp>
#include <boost/winapi/file_management.hpp>
#include <boost/winapi/mutex.hpp>
#include <boost/winapi/wait.hpp>
#include <boost/winapi/file_mapping.hpp>
#include <boost/winapi/semaphore.hpp>
#include <boost/winapi/system.hpp>
#include <boost/winapi/error_handling.hpp>
#include <boost/winapi/local_memory.hpp>
#include <boost/winapi/directory_management.hpp>
#include <boost/winapi/security.hpp>
#include <boost/winapi/dll.hpp>
#include <boost/winapi/basic_types.hpp>
//This should go in winapi's basic_types.hpp
namespace boost {
namespace ipwinapiext {
typedef boost::winapi::LONG_ LSTATUS;
//#ifndef BOOST_USE_WINDOWS_H
//typedef boost::winapi::LARGE_INTEGER_ LARGE_INTEGER_EXT;
//#else
//typedef LARGE_INTEGER LARGE_INTEGER_EXT;
//#endif
}} //namespace boost::ipwinapiext
#ifndef BOOST_USE_WINDOWS_H
extern "C" {
//Error handling
BOOST_SYMBOL_IMPORT BOOST_WINAPI_DETAIL_VOID BOOST_WINAPI_WINAPI_CC SetLastError(boost::winapi::DWORD_ dwErrCode);
//File management
BOOST_SYMBOL_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC GetFileType(boost::winapi::HANDLE_ hTemplateFile);
BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC FlushFileBuffers(boost::winapi::HANDLE_ hFile);
//Virtual Memory
BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualLock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize);
BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualUnlock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize);
BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualProtect( boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize
, boost::winapi::DWORD_ flNewProtect, boost::winapi::PDWORD_ lpflOldProtect);
//registry.hpp
BOOST_WINAPI_DETAIL_DECLARE_HANDLE(HKEY);
BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegOpenKeyExA
(::HKEY hKey, const char *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, ::HKEY *phkResult);
BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegQueryValueExA
(::HKEY hKey, const char *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData);
BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegCloseKey(::HKEY hKey);
//Event Log
BOOST_SYMBOL_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC OpenEventLogA(const char* lpUNCServerName, const char* lpSourceName);
BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC CloseEventLog(boost::winapi::HANDLE_ hEventLog);
BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC ReadEventLogA
( boost::winapi::HANDLE_ hEventLog, boost::winapi::DWORD_ dwReadFlags, boost::winapi::DWORD_ dwRecordOffset, void* lpBuffer
, boost::winapi::DWORD_ nNumberOfBytesToRead, boost::winapi::DWORD_ *pnBytesRead, boost::winapi::DWORD_ *pnMinNumberOfBytesNeeded);
} //extern "C" {
#endif //#ifndef BOOST_USE_WINDOWS_H
namespace boost {
namespace ipwinapiext {
typedef ::HKEY HKEY_;
#if BOOST_WINAPI_PARTITION_APP_SYSTEM
//Error handling
BOOST_FORCEINLINE BOOST_WINAPI_DETAIL_VOID SetLastError(boost::winapi::DWORD_ dwErrCode)
{ ::SetLastError(dwErrCode); }
//File management
BOOST_FORCEINLINE boost::winapi::DWORD_ GetFileType(boost::winapi::HANDLE_ hTemplateFile)
{ return ::GetFileType(hTemplateFile); }
BOOST_FORCEINLINE boost::winapi::BOOL_ FlushFileBuffers(boost::winapi::HANDLE_ hFile)
{ return ::FlushFileBuffers(hFile); }
//Virtual Memory
BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualLock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize)
{ return ::VirtualLock(lpAddress, dwSize); }
BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualUnlock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize)
{ return ::VirtualUnlock(lpAddress, dwSize); }
BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualProtect( boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize
, boost::winapi::DWORD_ flNewProtect, boost::winapi::PDWORD_ lpflOldProtect)
{ return ::VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect); }
//registry.hpp
BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegOpenKeyExA
(boost::ipwinapiext::HKEY_ hKey, const char *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, boost::ipwinapiext::HKEY_ *phkResult)
{
return ::RegOpenKeyExA(reinterpret_cast< ::HKEY >(hKey), lpSubKey, ulOptions, samDesired, reinterpret_cast< ::HKEY* >(phkResult));
}
BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegQueryValueExA
(boost::ipwinapiext::HKEY_ hKey, const char *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData)
{
return ::RegQueryValueExA(reinterpret_cast< ::HKEY >(hKey), lpValueName, lpReserved, lpType, lpData, lpcbData);
}
BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegCloseKey(boost::ipwinapiext::HKEY_ hKey)
{
return ::RegCloseKey(reinterpret_cast< ::HKEY >(hKey));
}
BOOST_FORCEINLINE void GetSystemInfo(boost::winapi::LPSYSTEM_INFO_ lpSystemInfo)
{ return ::GetSystemInfo(reinterpret_cast< ::_SYSTEM_INFO* >(lpSystemInfo)); }
#endif //BOOST_WINAPI_PARTITION_APP_SYSTEM
} //namespace ipwinapiext {
} //namespace boost {
namespace boost {
namespace interprocess {
namespace winapi {
typedef boost::winapi::SYSTEM_INFO_ interprocess_system_info;
typedef boost::winapi::OVERLAPPED_ interprocess_overlapped;
typedef boost::winapi::FILETIME_ interprocess_filetime;
typedef boost::winapi::WIN32_FIND_DATAA_ win32_find_data;
typedef boost::winapi::SECURITY_ATTRIBUTES_ interprocess_security_attributes;
typedef boost::winapi::SECURITY_DESCRIPTOR_ interprocess_security_descriptor;
typedef boost::winapi::BY_HANDLE_FILE_INFORMATION_ interprocess_by_handle_file_information;
typedef boost::winapi::HMODULE_ hmodule;
typedef boost::ipwinapiext::HKEY_ hkey;
typedef boost::winapi::FARPROC_ farproc_t;
//ntdll.dll
typedef long (__stdcall *NtDeleteFile_t)(object_attributes_t *ObjectAttributes);
typedef long (__stdcall *NtSetInformationFile_t)(void *FileHandle, io_status_block_t *IoStatusBlock, void *FileInformation, unsigned long Length, int FileInformationClass );
typedef long (__stdcall *NtOpenFile)(void **FileHandle, unsigned long DesiredAccess, object_attributes_t *ObjectAttributes
, io_status_block_t *IoStatusBlock, unsigned long ShareAccess, unsigned long Length, unsigned long OpenOptions);
typedef long (__stdcall *NtQuerySystemInformation_t)(int, void*, unsigned long, unsigned long *);
typedef long (__stdcall *NtQueryObject_t)(void*, object_information_class, void *, unsigned long, unsigned long *);
typedef long (__stdcall *NtQuerySemaphore_t)(void*, unsigned int info_class, interprocess_semaphore_basic_information *pinfo, unsigned int info_size, unsigned int *ret_len);
typedef long (__stdcall *NtQuerySection_t)(void*, section_information_class, interprocess_section_basic_information *pinfo, unsigned long info_size, unsigned long *ret_len);
typedef long (__stdcall *NtQueryInformationFile_t)(void *,io_status_block_t *,void *, long, int);
typedef long (__stdcall *NtOpenFile_t)(void*,unsigned long ,object_attributes_t*,io_status_block_t*,unsigned long,unsigned long);
typedef long (__stdcall *NtClose_t) (void*);
typedef long (__stdcall *NtQueryTimerResolution_t) (unsigned long* LowestResolution, unsigned long* HighestResolution, unsigned long* CurrentResolution);
typedef long (__stdcall *NtSetTimerResolution_t) (unsigned long RequestedResolution, int Set, unsigned long* ActualResolution);
} //namespace winapi {
} //namespace interprocess {
} //namespace boost {
//////////////////////////////////////////////////////////////////////////////
//
// Forward declaration of constants
//
//////////////////////////////////////////////////////////////////////////////
namespace boost {
namespace interprocess {
namespace winapi {
//Some used constants
static const unsigned long infinite_time = 0xFFFFFFFF;
static const unsigned long error_already_exists = 183L;
static const unsigned long error_invalid_handle = 6L;
static const unsigned long error_sharing_violation = 32L;
static const unsigned long error_file_not_found = 2u;
static const unsigned long error_no_more_files = 18u;
static const unsigned long error_not_locked = 158L;
//Retries in CreateFile, see http://support.microsoft.com/kb/316609
static const unsigned long error_sharing_violation_tries = 3L;
static const unsigned long error_sharing_violation_sleep_ms = 250L;
static const unsigned long error_file_too_large = 223L;
static const unsigned long error_insufficient_buffer = 122L;
static const unsigned long error_handle_eof = 38L;
static const unsigned long semaphore_all_access = (0x000F0000L)|(0x00100000L)|0x3;
static const unsigned long mutex_all_access = (0x000F0000L)|(0x00100000L)|0x0001;
static const unsigned long page_readonly = 0x02;
static const unsigned long page_readwrite = 0x04;
static const unsigned long page_writecopy = 0x08;
static const unsigned long page_noaccess = 0x01;
static const unsigned long standard_rights_required = 0x000F0000L;
static const unsigned long section_query = 0x0001;
static const unsigned long section_map_write = 0x0002;
static const unsigned long section_map_read = 0x0004;
static const unsigned long section_map_execute = 0x0008;
static const unsigned long section_extend_size = 0x0010;
static const unsigned long section_all_access = standard_rights_required |
section_query |
section_map_write |
section_map_read |
section_map_execute |
section_extend_size;
static const unsigned long file_map_copy = section_query;
static const unsigned long file_map_write = section_map_write;
static const unsigned long file_map_read = section_map_read;
static const unsigned long file_map_all_access = section_all_access;
static const unsigned long delete_access = 0x00010000L;
static const unsigned long file_flag_backup_semantics = 0x02000000;
static const long file_flag_delete_on_close = 0x04000000;
//Native API constants
static const unsigned long file_open_for_backup_intent = 0x00004000;
static const int file_share_valid_flags = 0x00000007;
static const long file_delete_on_close = 0x00001000L;
static const long obj_case_insensitive = 0x00000040L;
static const long delete_flag = 0x00010000L;
static const unsigned long movefile_copy_allowed = 0x02;
static const unsigned long movefile_delay_until_reboot = 0x04;
static const unsigned long movefile_replace_existing = 0x01;
static const unsigned long movefile_write_through = 0x08;
static const unsigned long movefile_create_hardlink = 0x10;
static const unsigned long movefile_fail_if_not_trackable = 0x20;
static const unsigned long file_share_read = 0x00000001;
static const unsigned long file_share_write = 0x00000002;
static const unsigned long file_share_delete = 0x00000004;
static const unsigned long file_attribute_readonly = 0x00000001;
static const unsigned long file_attribute_hidden = 0x00000002;
static const unsigned long file_attribute_system = 0x00000004;
static const unsigned long file_attribute_directory = 0x00000010;
static const unsigned long file_attribute_archive = 0x00000020;
static const unsigned long file_attribute_device = 0x00000040;
static const unsigned long file_attribute_normal = 0x00000080;
static const unsigned long file_attribute_temporary = 0x00000100;
static const unsigned long generic_read = 0x80000000L;
static const unsigned long generic_write = 0x40000000L;
static const unsigned long wait_object_0 = 0;
static const unsigned long wait_abandoned = 0x00000080L;
static const unsigned long wait_timeout = 258L;
static const unsigned long wait_failed = (unsigned long)0xFFFFFFFF;
static const unsigned long duplicate_close_source = (unsigned long)0x00000001;
static const unsigned long duplicate_same_access = (unsigned long)0x00000002;
static const unsigned long format_message_allocate_buffer
= (unsigned long)0x00000100;
static const unsigned long format_message_ignore_inserts
= (unsigned long)0x00000200;
static const unsigned long format_message_from_string
= (unsigned long)0x00000400;
static const unsigned long format_message_from_hmodule
= (unsigned long)0x00000800;
static const unsigned long format_message_from_system
= (unsigned long)0x00001000;
static const unsigned long format_message_argument_array
= (unsigned long)0x00002000;
static const unsigned long format_message_max_width_mask
= (unsigned long)0x000000FF;
static const unsigned long lang_neutral = (unsigned long)0x00;
static const unsigned long sublang_default = (unsigned long)0x01;
static const unsigned long invalid_file_size = (unsigned long)0xFFFFFFFF;
static const unsigned long invalid_file_attributes = ((unsigned long)-1);
static void * const invalid_handle_value = ((void*)(long)(-1));
static const unsigned long file_type_char = 0x0002L;
static const unsigned long file_type_disk = 0x0001L;
static const unsigned long file_type_pipe = 0x0003L;
static const unsigned long file_type_remote = 0x8000L;
static const unsigned long file_type_unknown = 0x0000L;
static const unsigned long create_new = 1;
static const unsigned long create_always = 2;
static const unsigned long open_existing = 3;
static const unsigned long open_always = 4;
static const unsigned long truncate_existing = 5;
static const unsigned long file_begin = 0;
static const unsigned long file_current = 1;
static const unsigned long file_end = 2;
static const unsigned long lockfile_fail_immediately = 1;
static const unsigned long lockfile_exclusive_lock = 2;
static const unsigned long error_lock_violation = 33;
static const unsigned long security_descriptor_revision = 1;
const unsigned long max_record_buffer_size = 0x10000L; // 64K
const unsigned long max_path = 260;
//Keys
static const hkey hkey_local_machine = (hkey)(unsigned long*)(long)(0x80000002);
static unsigned long key_query_value = 0x0001;
// Registry types
#define reg_none ( 0 ) // No value type
#define reg_sz ( 1 ) // Unicode nul terminated string
#define reg_expand_sz ( 2 ) // Unicode nul terminated string
// (with environment variable references)
#define reg_binary ( 3 ) // Free form binary
#define reg_dword ( 4 ) // 32-bit number
#define reg_dword_little_endian ( 4 ) // 32-bit number (same as REG_DWORD)
#define reg_dword_big_endian ( 5 ) // 32-bit number
#define reg_link ( 6 ) // Symbolic Link (unicode)
#define reg_multi_sz ( 7 ) // Multiple Unicode strings
#define reg_resource_list ( 8 ) // Resource list in the resource map
#define reg_full_resource_descriptor ( 9 ) // Resource list in the hardware description
#define reg_resource_requirements_list ( 10 )
#define reg_qword ( 11 ) // 64-bit number
#define reg_qword_little_endian ( 11 ) // 64-bit number (same as reg_qword)
} //namespace winapi {
} //namespace interprocess {
} //namespace boost {
namespace boost {
namespace interprocess {
namespace winapi {
inline unsigned long get_last_error()
{ return GetLastError(); }
inline void set_last_error(unsigned long err)
{ return SetLastError(err); }
inline unsigned long format_message
(unsigned long dwFlags, const void *lpSource,
unsigned long dwMessageId, unsigned long dwLanguageId,
char *lpBuffer, unsigned long nSize, std::va_list *Arguments)
{
return FormatMessageA
(dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments);
}
//And now, wrapper functions
inline void * local_free(void *hmem)
{ return LocalFree(hmem); }
inline unsigned long make_lang_id(unsigned long p, unsigned long s)
{ return ((((unsigned short)(s)) << 10) | (unsigned short)(p)); }
inline void sched_yield()
{
if(!SwitchToThread()){
Sleep(0);
}
}
inline void sleep_tick()
{ Sleep(1); }
inline void sleep(unsigned long ms)
{ Sleep(ms); }
inline unsigned long get_current_thread_id()
{ return GetCurrentThreadId(); }
inline bool get_process_times
( void *hProcess, interprocess_filetime* lpCreationTime
, interprocess_filetime *lpExitTime, interprocess_filetime *lpKernelTime
, interprocess_filetime *lpUserTime )
{ return 0 != GetProcessTimes(hProcess, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime); }
inline unsigned long get_current_process_id()
{ return GetCurrentProcessId(); }
inline unsigned int close_handle(void* handle)
{ return CloseHandle(handle); }
inline void * find_first_file(const char *lpFileName, win32_find_data *lpFindFileData)
{ return FindFirstFileA(lpFileName, lpFindFileData); }
inline bool find_next_file(void *hFindFile, win32_find_data *lpFindFileData)
{ return FindNextFileA(hFindFile, lpFindFileData) != 0; }
inline bool find_close(void *handle)
{ return FindClose(handle) != 0; }
inline bool duplicate_current_process_handle
(void *hSourceHandle, void **lpTargetHandle)
{
return 0 != DuplicateHandle
( GetCurrentProcess(), hSourceHandle, GetCurrentProcess()
, lpTargetHandle, 0, 0
, duplicate_same_access);
}
inline unsigned long get_file_type(void *hFile)
{
return GetFileType(hFile);
}
/*
inline void get_system_time_as_file_time(interprocess_filetime *filetime)
{ GetSystemTimeAsFileTime(filetime); }
inline bool file_time_to_local_file_time
(const interprocess_filetime *in, const interprocess_filetime *out)
{ return 0 != FileTimeToLocalFileTime(in, out); }
*/
inline void *open_or_create_mutex(const char *name, bool initial_owner, interprocess_security_attributes *attr)
{ return CreateMutexA(attr, (int)initial_owner, name); }
inline unsigned long wait_for_single_object(void *handle, unsigned long time)
{ return WaitForSingleObject(handle, time); }
inline int release_mutex(void *handle)
{ return ReleaseMutex(handle); }
inline int unmap_view_of_file(void *address)
{ return UnmapViewOfFile(address); }
inline void *open_or_create_semaphore(const char *name, long initial_count, long maximum_count, interprocess_security_attributes *attr)
{ return CreateSemaphoreA(attr, initial_count, maximum_count, name); }
inline void *open_semaphore(const char *name)
{ return OpenSemaphoreA(semaphore_all_access, 0, name); }
inline int release_semaphore(void *handle, long release_count, long *prev_count)
{ return ReleaseSemaphore(handle, release_count, prev_count); }
class interprocess_all_access_security
{
interprocess_security_attributes sa;
interprocess_security_descriptor sd;
bool initialized;
public:
interprocess_all_access_security()
: initialized(false)
{
if(!boost::winapi::InitializeSecurityDescriptor(&sd, security_descriptor_revision))
return;
if(!boost::winapi::SetSecurityDescriptorDacl(&sd, true, 0, false))
return;
sa.lpSecurityDescriptor = &sd;
sa.nLength = sizeof(interprocess_security_attributes);
sa.bInheritHandle = false;
initialized = true;
}
interprocess_security_attributes *get_attributes()
{ return &sa; }
};
inline void * create_file_mapping (void * handle, unsigned long access, ::boost::ulong_long_type file_offset, const char * name, interprocess_security_attributes *psec)
{
const boost::winapi::DWORD_ high_size(file_offset >> 32), low_size((boost::winapi::DWORD_)file_offset);
return CreateFileMappingA (handle, psec, access, high_size, low_size, name);
}
inline void * open_file_mapping (unsigned long access, const char *name)
{ return OpenFileMappingA (access, 0, name); }
inline void *map_view_of_file_ex(void *handle, unsigned long file_access, ::boost::ulong_long_type offset, std::size_t numbytes, void *base_addr)
{
const unsigned long offset_low = (unsigned long)(offset & ((::boost::ulong_long_type)0xFFFFFFFF));
const unsigned long offset_high = offset >> 32;
return MapViewOfFileEx(handle, file_access, offset_high, offset_low, numbytes, base_addr);
}
inline void *create_file(const char *name, unsigned long access, unsigned long creation_flags, unsigned long attributes, interprocess_security_attributes *psec)
{
for (unsigned int attempt(0); attempt < error_sharing_violation_tries; ++attempt){
void * const handle = CreateFileA(name, access,
file_share_read | file_share_write | file_share_delete,
psec, creation_flags, attributes, 0);
bool const invalid(invalid_handle_value == handle);
if (!invalid){
return handle;
}
if (error_sharing_violation != get_last_error()){
return handle;
}
sleep(error_sharing_violation_sleep_ms);
}
return invalid_handle_value;
}
inline void get_system_info(interprocess_system_info *info)
{ boost::ipwinapiext::GetSystemInfo(info); }
inline bool flush_view_of_file(void *base_addr, std::size_t numbytes)
{ return 0 != boost::winapi::FlushViewOfFile(base_addr, numbytes); }
inline bool virtual_unlock(void *base_addr, std::size_t numbytes)
{ return 0 != boost::ipwinapiext::VirtualUnlock(base_addr, numbytes); }
inline bool virtual_protect(void *base_addr, std::size_t numbytes, unsigned long flNewProtect, unsigned long &lpflOldProtect)
{ return 0 != boost::ipwinapiext::VirtualProtect(base_addr, numbytes, flNewProtect, &lpflOldProtect); }
inline bool flush_file_buffers(void *handle)
{ return 0 != boost::ipwinapiext::FlushFileBuffers(handle); }
inline bool get_file_size(void *handle, __int64 &size)
{ return 0 != boost::winapi::GetFileSizeEx(handle, (boost::winapi::LARGE_INTEGER_*)&size); }
inline bool create_directory(const char *name)
{
interprocess_all_access_security sec;
return 0 != boost::winapi::CreateDirectoryA(name, sec.get_attributes());
}
inline bool remove_directory(const char *lpPathName)
{ return 0 != boost::winapi::RemoveDirectoryA(lpPathName); }
inline unsigned long get_temp_path(unsigned long length, char *buffer)
{ return boost::winapi::GetTempPathA(length, buffer); }
inline int set_end_of_file(void *handle)
{ return 0 != boost::winapi::SetEndOfFile(handle); }
inline bool set_file_pointer(void *handle, __int64 distance, __int64 *new_file_pointer, unsigned long move_method)
{
long highPart = distance >> 32u;
boost::winapi::DWORD_ r = boost::winapi::SetFilePointer(handle, (unsigned long)distance, &highPart, move_method);
bool br = r != boost::winapi::INVALID_SET_FILE_POINTER_ || boost::winapi::GetLastError() != 0;
if (br && new_file_pointer){
*new_file_pointer = (unsigned __int64)r + ((__int64)highPart << 32);
}
return br;
}
inline bool lock_file_ex(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
{ return 0 != boost::winapi::LockFileEx(hnd, flags, reserved, size_low, size_high, overlapped); }
inline bool unlock_file_ex(void *hnd, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
{ return 0 != boost::winapi::UnlockFileEx(hnd, reserved, size_low, size_high, overlapped); }
inline bool write_file(void *hnd, const void *buffer, unsigned long bytes_to_write, unsigned long *bytes_written, interprocess_overlapped* overlapped)
{ return 0 != boost::winapi::WriteFile(hnd, buffer, bytes_to_write, bytes_written, overlapped); }
inline bool read_file(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped)
{ return 0 != boost::winapi::ReadFile(hnd, buffer, bytes_to_read, bytes_read, overlapped); }
inline bool get_file_information_by_handle(void *hnd, interprocess_by_handle_file_information *info)
{ return 0 != boost::winapi::GetFileInformationByHandle(hnd, info); }
inline long interlocked_increment(long volatile *addr)
{ return BOOST_INTERLOCKED_INCREMENT(const_cast<long*>(addr)); }
inline long interlocked_decrement(long volatile *addr)
{ return BOOST_INTERLOCKED_DECREMENT(const_cast<long*>(addr)); }
inline long interlocked_compare_exchange(long volatile *addr, long val1, long val2)
{ return BOOST_INTERLOCKED_COMPARE_EXCHANGE(const_cast<long*>(addr), val1, val2); }
inline long interlocked_exchange_add(long volatile* addend, long value)
{ return BOOST_INTERLOCKED_EXCHANGE_ADD(const_cast<long*>(addend), value); }
inline long interlocked_exchange(long volatile* addend, long value)
{ return BOOST_INTERLOCKED_EXCHANGE(const_cast<long*>(addend), value); }
//Forward functions
inline hmodule load_library(const char *name)
{ return boost::winapi::LoadLibraryA(name); }
inline bool free_library(hmodule module)
{ return 0 != boost::winapi::FreeLibrary(module); }
inline farproc_t get_proc_address(hmodule module, const char *name)
{ return boost::winapi::GetProcAddress(module, name); }
inline void *get_current_process()
{ return boost::winapi::GetCurrentProcess(); }
inline hmodule get_module_handle(const char *name)
{ return boost::winapi::GetModuleHandleA(name); }
inline long reg_open_key_ex(hkey hKey, const char *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
{ return boost::ipwinapiext::RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
inline long reg_query_value_ex(hkey hKey, const char *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
{ return boost::ipwinapiext::RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
inline long reg_close_key(hkey hKey)
{ return boost::ipwinapiext::RegCloseKey(hKey); }
inline void initialize_object_attributes
( object_attributes_t *pobject_attr, unicode_string_t *name
, unsigned long attr, void *rootdir, void *security_descr)
{
pobject_attr->Length = sizeof(object_attributes_t);
pobject_attr->RootDirectory = rootdir;
pobject_attr->Attributes = attr;
pobject_attr->ObjectName = name;
pobject_attr->SecurityDescriptor = security_descr;
pobject_attr->SecurityQualityOfService = 0;
}
inline void rtl_init_empty_unicode_string(unicode_string_t *ucStr, wchar_t *buf, unsigned short bufSize)
{
ucStr->Buffer = buf;
ucStr->Length = 0;
ucStr->MaximumLength = bufSize;
}
//A class that locates and caches loaded DLL function addresses.
template<int Dummy>
struct function_address_holder
{
enum { NtSetInformationFile
, NtQuerySystemInformation
, NtQueryObject
, NtQuerySemaphore
, NtQuerySection
, NtOpenFile
, NtClose
, NtQueryTimerResolution
, NumFunction
};
enum { NtDll_dll, Kernel32_dll, NumModule };
private:
static const char *FunctionNames[NumFunction];
static const char *ModuleNames[NumModule];
static farproc_t FunctionAddresses[NumFunction];
static unsigned int FunctionModules[NumFunction];
static volatile long FunctionStates[NumFunction];
static hmodule ModuleAddresses[NumModule];
static volatile long ModuleStates[NumModule];
static hmodule get_module_from_id(unsigned int id)
{
BOOST_ASSERT(id < (unsigned int)NumModule);
hmodule addr = get_module_handle(ModuleNames[id]);
BOOST_ASSERT(addr);
return addr;
}
static hmodule get_module(const unsigned int id)
{
BOOST_ASSERT(id < (unsigned int)NumModule);
for(unsigned i = 0; ModuleStates[id] < 2; ++i){
if(interlocked_compare_exchange(&ModuleStates[id], 1, 0) == 0){
ModuleAddresses[id] = get_module_from_id(id);
interlocked_increment(&ModuleStates[id]);
break;
}
else if(i & 1){
sched_yield();
}
else{
sleep_tick();
}
}
return ModuleAddresses[id];
}
static farproc_t get_address_from_dll(const unsigned int id)
{
BOOST_ASSERT(id < (unsigned int)NumFunction);
farproc_t addr = get_proc_address(get_module(FunctionModules[id]), FunctionNames[id]);
BOOST_ASSERT(addr);
return addr;
}
public:
static farproc_t get(const unsigned int id)
{
BOOST_ASSERT(id < (unsigned int)NumFunction);
for(unsigned i = 0; FunctionStates[id] < 2; ++i){
if(interlocked_compare_exchange(&FunctionStates[id], 1, 0) == 0){
FunctionAddresses[id] = get_address_from_dll(id);
interlocked_increment(&FunctionStates[id]);
break;
}
else if(i & 1){
sched_yield();
}
else{
sleep_tick();
}
}
return FunctionAddresses[id];
}
};
template<int Dummy>
const char *function_address_holder<Dummy>::FunctionNames[function_address_holder<Dummy>::NumFunction] =
{
"NtSetInformationFile",
"NtQuerySystemInformation",
"NtQueryObject",
"NtQuerySemaphore",
"NtQuerySection",
"NtOpenFile",
"NtClose",
"NtQueryTimerResolution",
};
template<int Dummy>
unsigned int function_address_holder<Dummy>::FunctionModules[function_address_holder<Dummy>::NumFunction] =
{
NtDll_dll,
NtDll_dll,
NtDll_dll,
NtDll_dll,
NtDll_dll,
NtDll_dll,
NtDll_dll,
NtDll_dll,
};
template<int Dummy>
const char *function_address_holder<Dummy>::ModuleNames[function_address_holder<Dummy>::NumModule] =
{
"ntdll.dll"//, "kernel32.dll"
};