1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_WIN32_API_HPP
12 #define BOOST_INTERPROCESS_WIN32_API_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/date_time/filetime_functions.hpp>
25 #include <cstddef>
26 #include <cstring>
27 #include <cstdlib>
28 #include <cstdio>
29
30 #include <boost/assert.hpp>
31 #include <string>
32 #include <vector>
33
34 #ifdef BOOST_USE_WINDOWS_H
35 #include <windows.h>
36 #include <Wbemidl.h>
37 #include <Objbase.h>
38 #include <Shlobj.h>
39 #endif
40
41 #if defined(_MSC_VER)
42 # pragma once
43 # pragma comment( lib, "Advapi32.lib" )
44 # pragma comment( lib, "oleaut32.lib" )
45 # pragma comment( lib, "Ole32.lib" )
46 # pragma comment( lib, "Psapi.lib" )
47 # pragma comment( lib, "Shell32.lib" ) //SHGetSpecialFolderPathA
48 #endif
49
50 #if defined (BOOST_INTERPROCESS_WINDOWS)
51 # include <cstdarg>
52 # include <boost/detail/interlocked.hpp>
53 #else
54 # error "This file can only be included in Windows OS"
55 #endif
56
57 //////////////////////////////////////////////////////////////////////////////
58 //
59 // Declaration of Windows structures or typedefs if BOOST_USE_WINDOWS_H is used
60 //
61 //////////////////////////////////////////////////////////////////////////////
62
63 //Ignore -pedantic errors here (anonymous structs, etc.)
64 #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
65 # pragma GCC diagnostic push
66 # pragma GCC diagnostic ignored "-pedantic"
67 #endif
68
69 namespace boost {
70 namespace interprocess {
71 namespace winapi {
72
73 //Own defines
74 static const unsigned long MaxPath = 260;
75
76 #ifndef BOOST_USE_WINDOWS_H
77
78 struct GUID_BIPC
79 {
80 unsigned long Data1;
81 unsigned short Data2;
82 unsigned short Data3;
83 unsigned char Data4[8];
84 };
85
86 #if defined(_MSC_VER)
87 #pragma warning (push)
88 #pragma warning (disable : 4201) // nonstandard extension used
89 #endif
90
91 struct decimal
92 {
93 unsigned short wReserved;
94 union {
95 struct {
96 unsigned char scale;
97 unsigned char sign;
98 };
99 unsigned short signscale;
100 };
101 unsigned long Hi32;
102 union {
103 struct {
104 unsigned long Lo32;
105 unsigned long Mid32;
106 };
107 ::boost::ulong_long_type Lo64;
108 };
109 };
110
111 typedef unsigned short *bstr;
112
113
114 struct wchar_variant
115 {
116 union
117 {
118 struct
119 {
120 unsigned short vt;
121 unsigned short wReserved1;
122 unsigned short wReserved2;
123 unsigned short wReserved3;
124 union
125 {
126 bstr bstrVal;
127 struct
128 {
129 void* pvRecord;
130 void* pRecInfo;
131 };
132 };
133 };
134 decimal decVal;
135 };
136 };
137
138 #if defined(_MSC_VER)
139 #pragma warning (pop)
140 #endif
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 struct IUnknown_BIPC
159 {
160 public:
161 virtual long __stdcall QueryInterface(
162 const GUID_BIPC &riid, // [in]
163 void **ppvObject) = 0; // [iid_is][out]
164
165 virtual unsigned long __stdcall AddRef (void) = 0;
166 virtual unsigned long __stdcall Release(void) = 0;
167 };
168
169 struct IWbemClassObject_BIPC : public IUnknown_BIPC
170 {
171 public:
172 virtual long __stdcall GetQualifierSet(
173 /* [out] */ void **ppQualSet) = 0;
174
175 virtual long __stdcall Get(
176 /* [string][in] */ const bstr wszName,
177 /* [in] */ long lFlags,
178 /* [unique][in][out] */ wchar_variant *pVal,
179 /* [unique][in][out] */ long *pType,
180 /* [unique][in][out] */ long *plFlavor) = 0;
181
182 virtual long __stdcall Put(
183 /* [string][in] */ const bstr wszName,
184 /* [in] */ long lFlags,
185 /* [in] */ wchar_variant *pVal,
186 /* [in] */ long Type) = 0;
187
188 virtual long __stdcall Delete(
189 /* [string][in] */ const bstr wszName) = 0;
190
191 virtual long __stdcall GetNames(
192 /* [string][in] */ const bstr wszQualifierName,
193 /* [in] */ long lFlags,
194 /* [in] */ wchar_variant *pQualifierVal,
195 /* [out] */ void * *pNames) = 0;
196
197 virtual long __stdcall BeginEnumeration(
198 /* [in] */ long lEnumFlags) = 0;
199
200 virtual long __stdcall Next(
201 /* [in] */ long lFlags,
202 /* [unique][in][out] */ bstr *strName,
203 /* [unique][in][out] */ wchar_variant *pVal,
204 /* [unique][in][out] */ long *pType,
205 /* [unique][in][out] */ long *plFlavor) = 0;
206
207 virtual long __stdcall EndEnumeration( void) = 0;
208
209 virtual long __stdcall GetPropertyQualifierSet(
210 /* [string][in] */ const bstr wszProperty,
211 /* [out] */ void **ppQualSet) = 0;
212
213 virtual long __stdcall Clone(
214 /* [out] */ IWbemClassObject_BIPC **ppCopy) = 0;
215
216 virtual long __stdcall GetObjectText(
217 /* [in] */ long lFlags,
218 /* [out] */ bstr *pstrObjectText) = 0;
219
220 virtual long __stdcall SpawnDerivedClass(
221 /* [in] */ long lFlags,
222 /* [out] */ IWbemClassObject_BIPC **ppNewClass) = 0;
223
224 virtual long __stdcall SpawnInstance(
225 /* [in] */ long lFlags,
226 /* [out] */ IWbemClassObject_BIPC **ppNewInstance) = 0;
227
228 virtual long __stdcall CompareTo(
229 /* [in] */ long lFlags,
230 /* [in] */ IWbemClassObject_BIPC *pCompareTo) = 0;
231
232 virtual long __stdcall GetPropertyOrigin(
233 /* [string][in] */ const bstr wszName,
234 /* [out] */ bstr *pstrClassName) = 0;
235
236 virtual long __stdcall InheritsFrom(
237 /* [in] */ const bstr strAncestor) = 0;
238
239 virtual long __stdcall GetMethod(
240 /* [string][in] */ const bstr wszName,
241 /* [in] */ long lFlags,
242 /* [out] */ IWbemClassObject_BIPC **ppInSignature,
243 /* [out] */ IWbemClassObject_BIPC **ppOutSignature) = 0;
244
245 virtual long __stdcall PutMethod(
246 /* [string][in] */ const bstr wszName,
247 /* [in] */ long lFlags,
248 /* [in] */ IWbemClassObject_BIPC *pInSignature,
249 /* [in] */ IWbemClassObject_BIPC *pOutSignature) = 0;
250
251 virtual long __stdcall DeleteMethod(
252 /* [string][in] */ const bstr wszName) = 0;
253
254 virtual long __stdcall BeginMethodEnumeration(
255 /* [in] */ long lEnumFlags) = 0;
256
257 virtual long __stdcall NextMethod(
258 /* [in] */ long lFlags,
259 /* [unique][in][out] */ bstr *pstrName,
260 /* [unique][in][out] */ IWbemClassObject_BIPC **ppInSignature,
261 /* [unique][in][out] */ IWbemClassObject_BIPC **ppOutSignature) = 0;
262
263 virtual long __stdcall EndMethodEnumeration( void) = 0;
264
265 virtual long __stdcall GetMethodQualifierSet(
266 /* [string][in] */ const bstr wszMethod,
267 /* [out] */ void **ppQualSet) = 0;
268
269 virtual long __stdcall GetMethodOrigin(
270 /* [string][in] */ const bstr wszMethodName,
271 /* [out] */ bstr *pstrClassName) = 0;
272
273 };
274
275 struct IWbemContext_BIPC : public IUnknown_BIPC
276 {
277 public:
278 virtual long __stdcall Clone(
279 /* [out] */ IWbemContext_BIPC **ppNewCopy) = 0;
280
281 virtual long __stdcall GetNames(
282 /* [in] */ long lFlags,
283 /* [out] */ void * *pNames) = 0;
284
285 virtual long __stdcall BeginEnumeration(
286 /* [in] */ long lFlags) = 0;
287
288 virtual long __stdcall Next(
289 /* [in] */ long lFlags,
290 /* [out] */ bstr *pstrName,
291 /* [out] */ wchar_variant *pValue) = 0;
292
293 virtual long __stdcall EndEnumeration( void) = 0;
294
295 virtual long __stdcall SetValue(
296 /* [string][in] */ const bstr wszName,
297 /* [in] */ long lFlags,
298 /* [in] */ wchar_variant *pValue) = 0;
299
300 virtual long __stdcall GetValue(
301 /* [string][in] */ const bstr wszName,
302 /* [in] */ long lFlags,
303 /* [out] */ wchar_variant *pValue) = 0;
304
305 virtual long __stdcall DeleteValue(
306 /* [string][in] */ const bstr wszName,
307 /* [in] */ long lFlags) = 0;
308
309 virtual long __stdcall DeleteAll( void) = 0;
310
311 };
312
313
314 struct IEnumWbemClassObject_BIPC : public IUnknown_BIPC
315 {
316 public:
317 virtual long __stdcall Reset( void) = 0;
318
319 virtual long __stdcall Next(
320 /* [in] */ long lTimeout,
321 /* [in] */ unsigned long uCount,
322 /* [length_is][size_is][out] */ IWbemClassObject_BIPC **apObjects,
323 /* [out] */ unsigned long *puReturned) = 0;
324
325 virtual long __stdcall NextAsync(
326 /* [in] */ unsigned long uCount,
327 /* [in] */ void *pSink) = 0;
328
329 virtual long __stdcall Clone(
330 /* [out] */ void **ppEnum) = 0;
331
332 virtual long __stdcall Skip(
333 /* [in] */ long lTimeout,
334 /* [in] */ unsigned long nCount) = 0;
335
336 };
337
338 struct IWbemServices_BIPC : public IUnknown_BIPC
339 {
340 public:
341 virtual long __stdcall OpenNamespace(
342 /* [in] */ const bstr strNamespace,
343 /* [in] */ long lFlags,
344 /* [in] */ void *pCtx,
345 /* [unique][in][out] */ void **ppWorkingNamespace,
346 /* [unique][in][out] */ void **ppResult) = 0;
347
348 virtual long __stdcall CancelAsyncCall(
349 /* [in] */ void *pSink) = 0;
350
351 virtual long __stdcall QueryObjectSink(
352 /* [in] */ long lFlags,
353 /* [out] */ void **ppResponseHandler) = 0;
354
355 virtual long __stdcall GetObject(
356 /* [in] */ const bstr strObjectPath,
357 /* [in] */ long lFlags,
358 /* [in] */ void *pCtx,
359 /* [unique][in][out] */ void **ppObject,
360 /* [unique][in][out] */ void **ppCallResult) = 0;
361
362 virtual long __stdcall GetObjectAsync(
363 /* [in] */ const bstr strObjectPath,
364 /* [in] */ long lFlags,
365 /* [in] */ void *pCtx,
366 /* [in] */ void *pResponseHandler) = 0;
367
368 virtual long __stdcall PutClass(
369 /* [in] */ IWbemClassObject_BIPC *pObject,
370 /* [in] */ long lFlags,
371 /* [in] */ void *pCtx,
372 /* [unique][in][out] */ void **ppCallResult) = 0;
373
374 virtual long __stdcall PutClassAsync(
375 /* [in] */ IWbemClassObject_BIPC *pObject,
376 /* [in] */ long lFlags,
377 /* [in] */ void *pCtx,
378 /* [in] */ void *pResponseHandler) = 0;
379
380 virtual long __stdcall DeleteClass(
381 /* [in] */ const bstr strClass,
382 /* [in] */ long lFlags,
383 /* [in] */ void *pCtx,
384 /* [unique][in][out] */ void **ppCallResult) = 0;
385
386 virtual long __stdcall DeleteClassAsync(
387 /* [in] */ const bstr strClass,
388 /* [in] */ long lFlags,
389 /* [in] */ void *pCtx,
390 /* [in] */ void *pResponseHandler) = 0;
391
392 virtual long __stdcall CreateClassEnum(
393 /* [in] */ const bstr strSuperclass,
394 /* [in] */ long lFlags,
395 /* [in] */ void *pCtx,
396 /* [out] */ void **ppEnum) = 0;
397
398 virtual long __stdcall CreateClassEnumAsync(
399 /* [in] */ const bstr strSuperclass,
400 /* [in] */ long lFlags,
401 /* [in] */ void *pCtx,
402 /* [in] */ void *pResponseHandler) = 0;
403
404 virtual long __stdcall PutInstance(
405 /* [in] */ void *pInst,
406 /* [in] */ long lFlags,
407 /* [in] */ void *pCtx,
408 /* [unique][in][out] */ void **ppCallResult) = 0;
409
410 virtual long __stdcall PutInstanceAsync(
411 /* [in] */ void *pInst,
412 /* [in] */ long lFlags,
413 /* [in] */ void *pCtx,
414 /* [in] */ void *pResponseHandler) = 0;
415
416 virtual long __stdcall DeleteInstance(
417 /* [in] */ const bstr strObjectPath,
418 /* [in] */ long lFlags,
419 /* [in] */ void *pCtx,
420 /* [unique][in][out] */ void **ppCallResult) = 0;
421
422 virtual long __stdcall DeleteInstanceAsync(
423 /* [in] */ const bstr strObjectPath,
424 /* [in] */ long lFlags,
425 /* [in] */ void *pCtx,
426 /* [in] */ void *pResponseHandler) = 0;
427
428 virtual long __stdcall CreateInstanceEnum(
429 /* [in] */ const bstr strFilter,
430 /* [in] */ long lFlags,
431 /* [in] */ void *pCtx,
432 /* [out] */ void **ppEnum) = 0;
433
434 virtual long __stdcall CreateInstanceEnumAsync(
435 /* [in] */ const bstr strFilter,
436 /* [in] */ long lFlags,
437 /* [in] */ void *pCtx,
438 /* [in] */ void *pResponseHandler) = 0;
439
440 virtual long __stdcall ExecQuery(
441 /* [in] */ const bstr strQueryLanguage,
442 /* [in] */ const bstr strQuery,
443 /* [in] */ long lFlags,
444 /* [in] */ IWbemContext_BIPC *pCtx,
445 /* [out] */ IEnumWbemClassObject_BIPC **ppEnum) = 0;
446
447 virtual long __stdcall ExecQueryAsync(
448 /* [in] */ const bstr strQueryLanguage,
449 /* [in] */ const bstr strQuery,
450 /* [in] */ long lFlags,
451 /* [in] */ IWbemContext_BIPC *pCtx,
452 /* [in] */ void *pResponseHandler) = 0;
453
454 virtual long __stdcall ExecNotificationQuery(
455 /* [in] */ const bstr strQueryLanguage,
456 /* [in] */ const bstr strQuery,
457 /* [in] */ long lFlags,
458 /* [in] */ IWbemContext_BIPC *pCtx,
459 /* [out] */ void **ppEnum) = 0;
460
461 virtual long __stdcall ExecNotificationQueryAsync(
462 /* [in] */ const bstr strQueryLanguage,
463 /* [in] */ const bstr strQuery,
464 /* [in] */ long lFlags,
465 /* [in] */ IWbemContext_BIPC *pCtx,
466 /* [in] */ void *pResponseHandler) = 0;
467
468 virtual long __stdcall ExecMethod(
469 /* [in] */ const bstr strObjectPath,
470 /* [in] */ const bstr strMethodName,
471 /* [in] */ long lFlags,
472 /* [in] */ IWbemContext_BIPC *pCtx,
473 /* [in] */ IWbemClassObject_BIPC *pInParams,
474 /* [unique][in][out] */ IWbemClassObject_BIPC **ppOutParams,
475 /* [unique][in][out] */ void **ppCallResult) = 0;
476
477 virtual long __stdcall ExecMethodAsync(
478 /* [in] */ const bstr strObjectPath,
479 /* [in] */ const bstr strMethodName,
480 /* [in] */ long lFlags,
481 /* [in] */ IWbemContext_BIPC *pCtx,
482 /* [in] */ IWbemClassObject_BIPC *pInParams,
483 /* [in] */ void *pResponseHandler) = 0;
484
485 };
486
487 struct IWbemLocator_BIPC : public IUnknown_BIPC
488 {
489 public:
490 virtual long __stdcall ConnectServer(
491 /* [in] */ const bstr strNetworkResource,
492 /* [in] */ const bstr strUser,
493 /* [in] */ const bstr strPassword,
494 /* [in] */ const bstr strLocale,
495 /* [in] */ long lSecurityFlags,
496 /* [in] */ const bstr strAuthority,
497 /* [in] */ void *pCtx,
498 /* [out] */ IWbemServices_BIPC **ppNamespace) = 0;
499
500 };
501
502 struct interprocess_overlapped
503 {
504 unsigned long *internal;
505 unsigned long *internal_high;
506 union {
507 struct {
508 unsigned long offset;
509 unsigned long offset_high;
510 }dummy;
511 void *pointer;
512 };
513
514 void *h_event;
515 };
516
517
518 struct interprocess_filetime
519 {
520 unsigned long dwLowDateTime;
521 unsigned long dwHighDateTime;
522 };
523
524 struct win32_find_data
525 {
526 unsigned long dwFileAttributes;
527 interprocess_filetime ftCreationTime;
528 interprocess_filetime ftLastAccessTime;
529 interprocess_filetime ftLastWriteTime;
530 unsigned long nFileSizeHigh;
531 unsigned long nFileSizeLow;
532 unsigned long dwReserved0;
533 unsigned long dwReserved1;
534 char cFileName[MaxPath];
535 char cAlternateFileName[14];
536 };
537
538 struct interprocess_security_attributes
539 {
540 unsigned long nLength;
541 void *lpSecurityDescriptor;
542 int bInheritHandle;
543 };
544
545 struct system_info {
546 union {
547 unsigned long dwOemId; // Obsolete field...do not use
548 struct {
549 unsigned short wProcessorArchitecture;
550 unsigned short wReserved;
551 } dummy;
552 };
553 unsigned long dwPageSize;
554 void * lpMinimumApplicationAddress;
555 void * lpMaximumApplicationAddress;
556 unsigned long * dwActiveProcessorMask;
557 unsigned long dwNumberOfProcessors;
558 unsigned long dwProcessorType;
559 unsigned long dwAllocationGranularity;
560 unsigned short wProcessorLevel;
561 unsigned short wProcessorRevision;
562 };
563
564 struct interprocess_acl
565 {
566 unsigned char AclRevision;
567 unsigned char Sbz1;
568 unsigned short AclSize;
569 unsigned short AceCount;
570 unsigned short Sbz2;
571 };
572
573 struct interprocess_security_descriptor
574 {
575 unsigned char Revision;
576 unsigned char Sbz1;
577 unsigned short Control;
578 void *Owner;
579 void *Group;
580 interprocess_acl *Sacl;
581 interprocess_acl *Dacl;
582 };
583
584 struct interprocess_by_handle_file_information
585 {
586 unsigned long dwFileAttributes;
587 interprocess_filetime ftCreationTime;
588 interprocess_filetime ftLastAccessTime;
589 interprocess_filetime ftLastWriteTime;
590 unsigned long dwVolumeSerialNumber;
591 unsigned long nFileSizeHigh;
592 unsigned long nFileSizeLow;
593 unsigned long nNumberOfLinks;
594 unsigned long nFileIndexHigh;
595 unsigned long nFileIndexLow;
596 };
597
598 struct interprocess_eventlogrecord
599 {
600 unsigned long Length; // Length of full record
601 unsigned long Reserved; // Used by the service
602 unsigned long RecordNumber; // Absolute record number
603 unsigned long TimeGenerated; // Seconds since 1-1-1970
604 unsigned long TimeWritten; // Seconds since 1-1-1970
605 unsigned long EventID;
606 unsigned short EventType;
607 unsigned short NumStrings;
608 unsigned short EventCategory;
609 unsigned short ReservedFlags; // For use with paired events (auditing)
610 unsigned long ClosingRecordNumber; // For use with paired events (auditing)
611 unsigned long StringOffset; // Offset from beginning of record
612 unsigned long UserSidLength;
613 unsigned long UserSidOffset;
614 unsigned long DataLength;
615 unsigned long DataOffset; // Offset from beginning of record
616 //
617 // Then follow:
618 //
619 // wchar_t SourceName[]
620 // wchar_t Computername[]
621 // SID UserSid
622 // wchar_t Strings[]
623 // BYTE Data[]
624 // CHAR Pad[]
625 // unsigned long Length;
626 //
627 };
628
629 union large_integer
630 {
631 __int64 QuadPart;
632 };
633
634 struct hinstance_struct { int unused; };
635 typedef hinstance_struct *hmodule;
636
637 struct hkey_struct;
638 typedef hkey_struct *hkey;
639
640 #ifdef _WIN64
641 typedef __int64 (__stdcall *farproc_t)();
642 #else
643 typedef int (__stdcall *farproc_t)();
644 #endif // _WIN64
645
646 #else //#ifndef BOOST_USE_WINDOWS_H
647
648 typedef GUID GUID_BIPC;
649 typedef VARIANT wchar_variant;
650
651 typedef IUnknown IUnknown_BIPC;
652
653 typedef IWbemClassObject IWbemClassObject_BIPC;
654
655 typedef IWbemContext IWbemContext_BIPC;
656
657 typedef IEnumWbemClassObject IEnumWbemClassObject_BIPC;
658
659 typedef IWbemServices IWbemServices_BIPC;
660
661 typedef IWbemLocator IWbemLocator_BIPC;
662
663 typedef OVERLAPPED interprocess_overlapped;
664
665 typedef FILETIME interprocess_filetime;
666
667 typedef WIN32_FIND_DATAA win32_find_data;
668
669 typedef SECURITY_ATTRIBUTES interprocess_security_attributes;
670
671 typedef SYSTEM_INFO system_info;
672
673 typedef ACL interprocess_acl;
674
675 typedef SECURITY_DESCRIPTOR interprocess_security_descriptor;
676
677 typedef BY_HANDLE_FILE_INFORMATION interprocess_by_handle_file_information;
678
679 typedef EVENTLOGRECORD interprocess_eventlogrecord;
680
681 typedef LARGE_INTEGER large_integer;
682
683 typedef HMODULE hmodule;
684
685 typedef HKEY hkey;
686
687 typedef BSTR bstr;
688
689 typedef FARPROC farproc_t;
690
691 #endif //#ifndef BOOST_USE_WINDOWS_H
692
693 //////////////////////////////////////////////////////////////////////////////
694 //
695 // Nt native structures
696 //
697 //////////////////////////////////////////////////////////////////////////////
698
699 struct interprocess_semaphore_basic_information
700 {
701 unsigned int count; // current semaphore count
702 unsigned int limit; // max semaphore count
703 };
704
705 struct interprocess_section_basic_information
706 {
707 void * base_address;
708 unsigned long section_attributes;
709 __int64 section_size;
710 };
711
712 struct file_rename_information_t {
713 int Replace;
714 void *RootDir;
715 unsigned long FileNameLength;
716 wchar_t FileName[1];
717 };
718
719 struct unicode_string_t {
720 unsigned short Length;
721 unsigned short MaximumLength;
722 wchar_t *Buffer;
723 };
724
725 struct object_attributes_t {
726 unsigned long Length;
727 void * RootDirectory;
728 unicode_string_t *ObjectName;
729 unsigned long Attributes;
730 void *SecurityDescriptor;
731 void *SecurityQualityOfService;
732 };
733
734 struct io_status_block_t {
735 union {
736 long Status;
737 void *Pointer;
738 };
739
740 unsigned long *Information;
741 };
742
743 union system_timeofday_information
744 {
745 struct data_t
746 {
747 __int64 liKeBootTime;
748 __int64 liKeSystemTime;
749 __int64 liExpTimeZoneBias;
750 unsigned long uCurrentTimeZoneId;
751 unsigned long dwReserved;
752 ::boost::ulong_long_type ullBootTimeBias;
753 ::boost::ulong_long_type ullSleepTimeBias;
754 } data;
755 unsigned char Reserved1[sizeof(data_t)];
756 };
757
758 static const long BootstampLength = sizeof(__int64);
759 static const long BootAndSystemstampLength = sizeof(__int64)*2;
760 static const long SystemTimeOfDayInfoLength = sizeof(system_timeofday_information::data_t);
761
762 struct object_name_information_t
763 {
764 unicode_string_t Name;
765 wchar_t NameBuffer[1];
766 };
767
768 enum file_information_class_t {
769 file_directory_information = 1,
770 file_full_directory_information,
771 file_both_directory_information,
772 file_basic_information,
773 file_standard_information,
774 file_internal_information,
775 file_ea_information,
776 file_access_information,
777 file_name_information,
778 file_rename_information,
779 file_link_information,
780 file_names_information,
781 file_disposition_information,
782 file_position_information,
783 file_full_ea_information,
784 file_mode_information,
785 file_alignment_information,
786 file_all_information,
787 file_allocation_information,
788 file_end_of_file_information,
789 file_alternate_name_information,
790 file_stream_information,
791 file_pipe_information,
792 file_pipe_local_information,
793 file_pipe_remote_information,
794 file_mailslot_query_information,
795 file_mailslot_set_information,
796 file_compression_information,
797 file_copy_on_write_information,
798 file_completion_information,
799 file_move_cluster_information,
800 file_quota_information,
801 file_reparse_point_information,
802 file_network_open_information,
803 file_object_id_information,
804 file_tracking_information,
805 file_ole_directory_information,
806 file_content_index_information,
807 file_inherit_content_index_information,
808 file_ole_information,
809 file_maximum_information
810 };
811
812 enum semaphore_information_class {
813 semaphore_basic_information = 0
814 };
815
816
817 enum system_information_class {
818 system_basic_information = 0,
819 system_performance_information = 2,
820 system_time_of_day_information = 3,
821 system_process_information = 5,
822 system_processor_performance_information = 8,
823 system_interrupt_information = 23,
824 system_exception_information = 33,
825 system_registry_quota_information = 37,
826 system_lookaside_information = 45
827 };
828
829 enum object_information_class
830 {
831 object_basic_information,
832 object_name_information,
833 object_type_information,
834 object_all_information,
835 object_data_information
836 };
837
838 enum section_information_class
839 {
840 section_basic_information,
841 section_image_information
842 };
843
844 //////////////////////////////////////////////////////////////////////////////
845 //
846 // Forward declaration of winapi
847 //
848 //////////////////////////////////////////////////////////////////////////////
849
850 #ifndef BOOST_USE_WINDOWS_H
851
852 //Kernel32.dll
853
854 //Some windows API declarations
855 extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentProcessId();
856 extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId();
857 extern "C" __declspec(dllimport) int __stdcall GetProcessTimes
858 ( void *hProcess, interprocess_filetime* lpCreationTime
859 , interprocess_filetime *lpExitTime,interprocess_filetime *lpKernelTime
860 , interprocess_filetime *lpUserTime );
861 extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long);
862 extern "C" __declspec(dllimport) unsigned long __stdcall GetTickCount(void);
863 extern "C" __declspec(dllimport) int __stdcall SwitchToThread();
864 extern "C" __declspec(dllimport) unsigned long __stdcall GetLastError();
865 extern "C" __declspec(dllimport) void __stdcall SetLastError(unsigned long);
866 extern "C" __declspec(dllimport) void * __stdcall GetCurrentProcess();
867 extern "C" __declspec(dllimport) int __stdcall CloseHandle(void*);
868 extern "C" __declspec(dllimport) int __stdcall DuplicateHandle
869 ( void *hSourceProcessHandle, void *hSourceHandle
870 , void *hTargetProcessHandle, void **lpTargetHandle
871 , unsigned long dwDesiredAccess, int bInheritHandle
872 , unsigned long dwOptions);
873 extern "C" __declspec(dllimport) long __stdcall GetFileType(void *hFile);
874 extern "C" __declspec(dllimport) void *__stdcall FindFirstFileA(const char *lpFileName, win32_find_data *lpFindFileData);
875 extern "C" __declspec(dllimport) int __stdcall FindNextFileA(void *hFindFile, win32_find_data *lpFindFileData);
876 extern "C" __declspec(dllimport) int __stdcall FindClose(void *hFindFile);
877 //extern "C" __declspec(dllimport) void __stdcall GetSystemTimeAsFileTime(interprocess_filetime*);
878 //extern "C" __declspec(dllimport) int __stdcall FileTimeToLocalFileTime(const interprocess_filetime *in, const interprocess_filetime *out);
879 extern "C" __declspec(dllimport) void * __stdcall CreateMutexA(interprocess_security_attributes*, int, const char *);
880 extern "C" __declspec(dllimport) void * __stdcall OpenMutexA(unsigned long, int, const char *);
881 extern "C" __declspec(dllimport) unsigned long __stdcall WaitForSingleObject(void *, unsigned long);
882 extern "C" __declspec(dllimport) int __stdcall ReleaseMutex(void *);
883 extern "C" __declspec(dllimport) int __stdcall UnmapViewOfFile(void *);
884 extern "C" __declspec(dllimport) void * __stdcall CreateSemaphoreA(interprocess_security_attributes*, long, long, const char *);
885 extern "C" __declspec(dllimport) int __stdcall ReleaseSemaphore(void *, long, long *);
886 extern "C" __declspec(dllimport) void * __stdcall OpenSemaphoreA(unsigned long, int, const char *);
887 extern "C" __declspec(dllimport) void * __stdcall CreateFileMappingA (void *, interprocess_security_attributes*, unsigned long, unsigned long, unsigned long, const char *);
888 extern "C" __declspec(dllimport) void * __stdcall MapViewOfFileEx (void *, unsigned long, unsigned long, unsigned long, std::size_t, void*);
889 extern "C" __declspec(dllimport) void * __stdcall OpenFileMappingA (unsigned long, int, const char *);
890 extern "C" __declspec(dllimport) void * __stdcall CreateFileA (const char *, unsigned long, unsigned long, struct interprocess_security_attributes*, unsigned long, unsigned long, void *);
891 extern "C" __declspec(dllimport) void __stdcall GetSystemInfo (struct system_info *);
892 extern "C" __declspec(dllimport) int __stdcall FlushViewOfFile (void *, std::size_t);
893 extern "C" __declspec(dllimport) int __stdcall VirtualUnlock (void *, std::size_t);
894 extern "C" __declspec(dllimport) int __stdcall VirtualProtect (void *, std::size_t, unsigned long, unsigned long *);
895 extern "C" __declspec(dllimport) int __stdcall FlushFileBuffers (void *);
896 extern "C" __declspec(dllimport) int __stdcall GetFileSizeEx (void *, large_integer *size);
897 extern "C" __declspec(dllimport) unsigned long __stdcall FormatMessageA
898 (unsigned long dwFlags, const void *lpSource, unsigned long dwMessageId,
899 unsigned long dwLanguageId, char *lpBuffer, unsigned long nSize,
900 std::va_list *Arguments);
901 extern "C" __declspec(dllimport) void *__stdcall LocalFree (void *);
902 extern "C" __declspec(dllimport) unsigned long __stdcall GetFileAttributesA(const char *);
903 extern "C" __declspec(dllimport) int __stdcall CreateDirectoryA(const char *, interprocess_security_attributes*);
904 extern "C" __declspec(dllimport) int __stdcall RemoveDirectoryA(const char *lpPathName);
905 extern "C" __declspec(dllimport) int __stdcall GetTempPathA(unsigned long length, char *buffer);
906 extern "C" __declspec(dllimport) int __stdcall CreateDirectory(const char *, interprocess_security_attributes*);
907 extern "C" __declspec(dllimport) int __stdcall SetFileValidData(void *, __int64 size);
908 extern "C" __declspec(dllimport) int __stdcall SetEndOfFile(void *);
909 extern "C" __declspec(dllimport) int __stdcall SetFilePointerEx(void *, large_integer distance, large_integer *new_file_pointer, unsigned long move_method);
910 extern "C" __declspec(dllimport) int __stdcall LockFile (void *hnd, unsigned long offset_low, unsigned long offset_high, unsigned long size_low, unsigned long size_high);
911 extern "C" __declspec(dllimport) int __stdcall UnlockFile(void *hnd, unsigned long offset_low, unsigned long offset_high, unsigned long size_low, unsigned long size_high);
912 extern "C" __declspec(dllimport) int __stdcall LockFileEx(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped* overlapped);
913 extern "C" __declspec(dllimport) int __stdcall UnlockFileEx(void *hnd, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped* overlapped);
914 extern "C" __declspec(dllimport) int __stdcall WriteFile(void *hnd, const void *buffer, unsigned long bytes_to_write, unsigned long *bytes_written, interprocess_overlapped* overlapped);
915 extern "C" __declspec(dllimport) int __stdcall ReadFile(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped);
916 extern "C" __declspec(dllimport) int __stdcall InitializeSecurityDescriptor(interprocess_security_descriptor *pSecurityDescriptor, unsigned long dwRevision);
917 extern "C" __declspec(dllimport) int __stdcall SetSecurityDescriptorDacl(interprocess_security_descriptor *pSecurityDescriptor, int bDaclPresent, interprocess_acl *pDacl, int bDaclDefaulted);
918 extern "C" __declspec(dllimport) hmodule __stdcall LoadLibraryA(const char *);
919 extern "C" __declspec(dllimport) int __stdcall FreeLibrary(hmodule);
920 extern "C" __declspec(dllimport) farproc_t __stdcall GetProcAddress(void *, const char*);
921 extern "C" __declspec(dllimport) hmodule __stdcall GetModuleHandleA(const char*);
922 extern "C" __declspec(dllimport) void *__stdcall GetFileInformationByHandle(void *, interprocess_by_handle_file_information*);
923
924 //Advapi32.dll
925 extern "C" __declspec(dllimport) long __stdcall RegOpenKeyExA(hkey, const char *, unsigned long, unsigned long, hkey*);
926 extern "C" __declspec(dllimport) long __stdcall RegQueryValueExA(hkey, const char *, unsigned long*, unsigned long*, unsigned char *, unsigned long*);
927 extern "C" __declspec(dllimport) long __stdcall RegCloseKey(hkey);
928
929 //Ole32.dll
930 extern "C" __declspec(dllimport) long __stdcall CoInitializeEx(void *pvReserved, unsigned long dwCoInit);
931 extern "C" __declspec(dllimport) long __stdcall CoInitializeSecurity(
932 void* pSecDesc,
933 long cAuthSvc,
934 void * asAuthSvc,
935 void *pReserved1,
936 unsigned long dwAuthnLevel,
937 unsigned long dwImpLevel,
938 void *pAuthList,
939 unsigned long dwCapabilities,
940 void *pReserved3 );
941
942 extern "C" __declspec(dllimport) long __stdcall CoSetProxyBlanket(
943 IUnknown_BIPC *pProxy,
944 unsigned long dwAuthnSvc,
945 unsigned long dwAuthzSvc,
946 wchar_t *pServerPrincName,
947 unsigned long dwAuthnLevel,
948 unsigned long dwImpLevel,
949 void *pAuthInfo,
950 unsigned long dwCapabilities);
951 extern "C" __declspec(dllimport) long __stdcall CoCreateInstance(const GUID_BIPC & rclsid, IUnknown_BIPC *pUnkOuter,
952 unsigned long dwClsContext, const GUID_BIPC & riid, void** ppv);
953 extern "C" __declspec(dllimport) void __stdcall CoUninitialize(void);
954
955 //OleAut32.dll
956 extern "C" __declspec(dllimport) long __stdcall VariantClear(wchar_variant * pvarg);
957
958 //Shell32.dll
959 extern "C" __declspec(dllimport) int __stdcall SHGetSpecialFolderPathA
960 (void* hwnd, const char *pszPath, int csidl, int fCreate);
961
962 extern "C" __declspec(dllimport) int __stdcall SHGetFolderPathA(void *hwnd, int csidl, void *hToken, unsigned long dwFlags, const char *pszPath);
963
964 //EventLog access functions
965
966 extern "C" __declspec(dllimport) void* __stdcall OpenEventLogA
967 (const char* lpUNCServerName, const char* lpSourceName);
968
969 extern "C" __declspec(dllimport) int __stdcall CloseEventLog(void *hEventLog);
970
971 extern "C" __declspec(dllimport) int __stdcall ReadEventLogA
972 (void *hEventLog,
973 unsigned long dwReadFlags,
974 unsigned long dwRecordOffset,
975 void *lpBuffer,
976 unsigned long nNumberOfBytesToRead,
977 unsigned long *pnBytesRead,
978 unsigned long *pnMinNumberOfBytesNeeded
979 );
980
981 #endif //#ifndef BOOST_USE_WINDOWS_H
982
983 //kernel32.dll
984 typedef int (__stdcall *QueryPerformanceCounter_t) (__int64 *lpPerformanceCount);
985 typedef int (__stdcall *QueryPerformanceFrequency_t)(__int64 *lpFrequency);
986
987 //ntdll.dll
988 typedef long (__stdcall *NtDeleteFile_t)(object_attributes_t *ObjectAttributes);
989 typedef long (__stdcall *NtSetInformationFile_t)(void *FileHandle, io_status_block_t *IoStatusBlock, void *FileInformation, unsigned long Length, int FileInformationClass );
990 typedef long (__stdcall *NtOpenFile)(void **FileHandle, unsigned long DesiredAccess, object_attributes_t *ObjectAttributes
991 , io_status_block_t *IoStatusBlock, unsigned long ShareAccess, unsigned long Length, unsigned long OpenOptions);
992 typedef long (__stdcall *NtQuerySystemInformation_t)(int, void*, unsigned long, unsigned long *);
993 typedef long (__stdcall *NtQueryObject_t)(void*, object_information_class, void *, unsigned long, unsigned long *);
994 typedef long (__stdcall *NtQuerySemaphore_t)(void*, unsigned int info_class, interprocess_semaphore_basic_information *pinfo, unsigned int info_size, unsigned int *ret_len);
995 typedef long (__stdcall *NtQuerySection_t)(void*, section_information_class, interprocess_section_basic_information *pinfo, unsigned long info_size, unsigned long *ret_len);
996 typedef long (__stdcall *NtQueryInformationFile_t)(void *,io_status_block_t *,void *, long, int);
997 typedef long (__stdcall *NtOpenFile_t)(void*,unsigned long ,object_attributes_t*,io_status_block_t*,unsigned long,unsigned long);
998 typedef long (__stdcall *NtClose_t) (void*);
999 typedef long (__stdcall *NtQueryTimerResolution_t) (unsigned long* LowestResolution, unsigned long* HighestResolution, unsigned long* CurrentResolution);
1000 typedef long (__stdcall *NtSetTimerResolution_t) (unsigned long RequestedResolution, int Set, unsigned long* ActualResolution);
1001
1002 } //namespace winapi {
1003 } //namespace interprocess {
1004 } //namespace boost {
1005
1006 //////////////////////////////////////////////////////////////////////////////
1007 //
1008 // Forward declaration of constants
1009 //
1010 //////////////////////////////////////////////////////////////////////////////
1011
1012 namespace boost {
1013 namespace interprocess {
1014 namespace winapi {
1015
1016 //Some used constants
1017 static const unsigned long infinite_time = 0xFFFFFFFF;
1018 static const unsigned long error_already_exists = 183L;
1019 static const unsigned long error_invalid_handle = 6L;
1020 static const unsigned long error_sharing_violation = 32L;
1021 static const unsigned long error_file_not_found = 2u;
1022 static const unsigned long error_no_more_files = 18u;
1023 static const unsigned long error_not_locked = 158L;
1024 //Retries in CreateFile, see http://support.microsoft.com/kb/316609
1025 static const unsigned long error_sharing_violation_tries = 3L;
1026 static const unsigned long error_sharing_violation_sleep_ms = 250L;
1027 static const unsigned long error_file_too_large = 223L;
1028 static const unsigned long error_insufficient_buffer = 122L;
1029 static const unsigned long error_handle_eof = 38L;
1030 static const unsigned long semaphore_all_access = (0x000F0000L)|(0x00100000L)|0x3;
1031 static const unsigned long mutex_all_access = (0x000F0000L)|(0x00100000L)|0x0001;
1032
1033 static const unsigned long page_readonly = 0x02;
1034 static const unsigned long page_readwrite = 0x04;
1035 static const unsigned long page_writecopy = 0x08;
1036 static const unsigned long page_noaccess = 0x01;
1037
1038 static const unsigned long standard_rights_required = 0x000F0000L;
1039 static const unsigned long section_query = 0x0001;
1040 static const unsigned long section_map_write = 0x0002;
1041 static const unsigned long section_map_read = 0x0004;
1042 static const unsigned long section_map_execute = 0x0008;
1043 static const unsigned long section_extend_size = 0x0010;
1044 static const unsigned long section_all_access = standard_rights_required |
1045 section_query |
1046 section_map_write |
1047 section_map_read |
1048 section_map_execute |
1049 section_extend_size;
1050
1051 static const unsigned long file_map_copy = section_query;
1052 static const unsigned long file_map_write = section_map_write;
1053 static const unsigned long file_map_read = section_map_read;
1054 static const unsigned long file_map_all_access = section_all_access;
1055 static const unsigned long delete_access = 0x00010000L;
1056 static const unsigned long file_flag_backup_semantics = 0x02000000;
1057 static const long file_flag_delete_on_close = 0x04000000;
1058
1059 //Native API constants
1060 static const unsigned long file_open_for_backup_intent = 0x00004000;
1061 static const int file_share_valid_flags = 0x00000007;
1062 static const long file_delete_on_close = 0x00001000L;
1063 static const long obj_case_insensitive = 0x00000040L;
1064 static const long delete_flag = 0x00010000L;
1065
1066 static const unsigned long movefile_copy_allowed = 0x02;
1067 static const unsigned long movefile_delay_until_reboot = 0x04;
1068 static const unsigned long movefile_replace_existing = 0x01;
1069 static const unsigned long movefile_write_through = 0x08;
1070 static const unsigned long movefile_create_hardlink = 0x10;
1071 static const unsigned long movefile_fail_if_not_trackable = 0x20;
1072
1073 static const unsigned long file_share_read = 0x00000001;
1074 static const unsigned long file_share_write = 0x00000002;
1075 static const unsigned long file_share_delete = 0x00000004;
1076
1077 static const unsigned long file_attribute_readonly = 0x00000001;
1078 static const unsigned long file_attribute_hidden = 0x00000002;
1079 static const unsigned long file_attribute_system = 0x00000004;
1080 static const unsigned long file_attribute_directory = 0x00000010;
1081 static const unsigned long file_attribute_archive = 0x00000020;
1082 static const unsigned long file_attribute_device = 0x00000040;
1083 static const unsigned long file_attribute_normal = 0x00000080;
1084 static const unsigned long file_attribute_temporary = 0x00000100;
1085
1086 static const unsigned long generic_read = 0x80000000L;
1087 static const unsigned long generic_write = 0x40000000L;
1088
1089 static const unsigned long wait_object_0 = 0;
1090 static const unsigned long wait_abandoned = 0x00000080L;
1091 static const unsigned long wait_timeout = 258L;
1092 static const unsigned long wait_failed = (unsigned long)0xFFFFFFFF;
1093
1094 static const unsigned long duplicate_close_source = (unsigned long)0x00000001;
1095 static const unsigned long duplicate_same_access = (unsigned long)0x00000002;
1096
1097 static const unsigned long format_message_allocate_buffer
1098 = (unsigned long)0x00000100;
1099 static const unsigned long format_message_ignore_inserts
1100 = (unsigned long)0x00000200;
1101 static const unsigned long format_message_from_string
1102 = (unsigned long)0x00000400;
1103 static const unsigned long format_message_from_hmodule
1104 = (unsigned long)0x00000800;
1105 static const unsigned long format_message_from_system
1106 = (unsigned long)0x00001000;
1107 static const unsigned long format_message_argument_array
1108 = (unsigned long)0x00002000;
1109 static const unsigned long format_message_max_width_mask
1110 = (unsigned long)0x000000FF;
1111 static const unsigned long lang_neutral = (unsigned long)0x00;
1112 static const unsigned long sublang_default = (unsigned long)0x01;
1113 static const unsigned long invalid_file_size = (unsigned long)0xFFFFFFFF;
1114 static const unsigned long invalid_file_attributes = ((unsigned long)-1);
1115 static void * const invalid_handle_value = ((void*)(long)(-1));
1116
1117 static const unsigned long file_type_char = 0x0002L;
1118 static const unsigned long file_type_disk = 0x0001L;
1119 static const unsigned long file_type_pipe = 0x0003L;
1120 static const unsigned long file_type_remote = 0x8000L;
1121 static const unsigned long file_type_unknown = 0x0000L;
1122
1123 static const unsigned long create_new = 1;
1124 static const unsigned long create_always = 2;
1125 static const unsigned long open_existing = 3;
1126 static const unsigned long open_always = 4;
1127 static const unsigned long truncate_existing = 5;
1128
1129 static const unsigned long file_begin = 0;
1130 static const unsigned long file_current = 1;
1131 static const unsigned long file_end = 2;
1132
1133 static const unsigned long lockfile_fail_immediately = 1;
1134 static const unsigned long lockfile_exclusive_lock = 2;
1135 static const unsigned long error_lock_violation = 33;
1136 static const unsigned long security_descriptor_revision = 1;
1137
1138 const unsigned long max_record_buffer_size = 0x10000L; // 64K
1139 const unsigned long max_path = 260;
1140
1141 //Keys
1142 static const hkey hkey_local_machine = (hkey)(unsigned long*)(long)(0x80000002);
1143 static unsigned long key_query_value = 0x0001;
1144
1145 //COM API
1146 const unsigned long RPC_C_AUTHN_LEVEL_PKT_BIPC = 4;
1147 const unsigned long RPC_C_AUTHN_DEFAULT_BIPC = 0xffffffffL;
1148 const unsigned long RPC_C_AUTHZ_DEFAULT_BIPC = 0xffffffffL;
1149 const unsigned long RPC_C_IMP_LEVEL_IMPERSONATE_BIPC = 3;
1150 const signed long EOAC_NONE_BIPC = 0;
1151 const signed long CLSCTX_INPROC_SERVER_BIPC = 0x1;
1152 const signed long CLSCTX_LOCAL_SERVER_BIPC = 0x4;
1153 const signed long WBEM_FLAG_RETURN_IMMEDIATELY_BIPC = 0x10;
1154 const signed long WBEM_FLAG_RETURN_WHEN_COMPLETE_BIPC = 0x0;
1155 const signed long WBEM_FLAG_FORWARD_ONLY_BIPC = 0x20;
1156 const signed long WBEM_INFINITE_BIPC = 0xffffffffL;
1157 const signed long RPC_E_TOO_LATE_BIPC = 0x80010119L;
1158 const signed long S_OK_BIPC = 0L;
1159 const signed long S_FALSE_BIPC = 1;
1160 const signed long RPC_E_CHANGED_MODE_BIPC = 0x80010106L;
1161 const unsigned long COINIT_APARTMENTTHREADED_BIPC = 0x2;
1162 const unsigned long COINIT_MULTITHREADED_BIPC = 0x0;
1163 const unsigned long COINIT_DISABLE_OLE1DDE_BIPC = 0x4;
1164 const unsigned long COINIT_SPEED_OVER_MEMORY_BIPC = 0x4;
1165
1166
1167 //If the user needs to change default COM initialization model,
1168 //it can define BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL to one of these:
1169 //
1170 // COINIT_APARTMENTTHREADED_BIPC
1171 // COINIT_MULTITHREADED_BIPC
1172 // COINIT_DISABLE_OLE1DDE_BIPC
1173 // COINIT_SPEED_OVER_MEMORY_BIPC
1174 #if !defined(BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL)
1175 #define BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL COINIT_APARTMENTTHREADED_BIPC
1176 #elif (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_APARTMENTTHREADED_BIPC) &&\
1177 (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_MULTITHREADED_BIPC) &&\
1178 (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_DISABLE_OLE1DDE_BIPC) &&\
1179 (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_SPEED_OVER_MEMORY_BIPC)
1180 #error "Wrong value for BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL macro"
1181 #endif
1182
1183 const GUID_BIPC CLSID_WbemAdministrativeLocator =
1184 { 0xcb8555cc, 0x9128, 0x11d1, {0xad, 0x9b, 0x00, 0xc0, 0x4f, 0xd8, 0xfd, 0xff}};
1185
1186 const GUID_BIPC IID_IUnknown = { 0x00000000, 0x0000, 0x0000, {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}};
1187
1188 static const unsigned long eventlog_sequential_read = 0x0001;
1189 static const unsigned long eventlog_backwards_read = 0x0008;
1190
1191 } //namespace winapi {
1192 } //namespace interprocess {
1193 } //namespace boost {
1194
1195
1196 namespace boost {
1197 namespace interprocess {
1198 namespace winapi {
1199
get_last_error()1200 inline unsigned long get_last_error()
1201 { return GetLastError(); }
1202
set_last_error(unsigned long err)1203 inline void set_last_error(unsigned long err)
1204 { return SetLastError(err); }
1205
format_message(unsigned long dwFlags,const void * lpSource,unsigned long dwMessageId,unsigned long dwLanguageId,char * lpBuffer,unsigned long nSize,std::va_list * Arguments)1206 inline unsigned long format_message
1207 (unsigned long dwFlags, const void *lpSource,
1208 unsigned long dwMessageId, unsigned long dwLanguageId,
1209 char *lpBuffer, unsigned long nSize, std::va_list *Arguments)
1210 {
1211 return FormatMessageA
1212 (dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments);
1213 }
1214
1215 //And now, wrapper functions
local_free(void * hmem)1216 inline void * local_free(void *hmem)
1217 { return LocalFree(hmem); }
1218
make_lang_id(unsigned long p,unsigned long s)1219 inline unsigned long make_lang_id(unsigned long p, unsigned long s)
1220 { return ((((unsigned short)(s)) << 10) | (unsigned short)(p)); }
1221
sched_yield()1222 inline void sched_yield()
1223 {
1224 if(!SwitchToThread()){
1225 Sleep(0);
1226 }
1227 }
1228
sleep_tick()1229 inline void sleep_tick()
1230 { Sleep(1); }
1231
sleep(unsigned long ms)1232 inline void sleep(unsigned long ms)
1233 { Sleep(ms); }
1234
get_current_thread_id()1235 inline unsigned long get_current_thread_id()
1236 { return GetCurrentThreadId(); }
1237
get_process_times(void * hProcess,interprocess_filetime * lpCreationTime,interprocess_filetime * lpExitTime,interprocess_filetime * lpKernelTime,interprocess_filetime * lpUserTime)1238 inline bool get_process_times
1239 ( void *hProcess, interprocess_filetime* lpCreationTime
1240 , interprocess_filetime *lpExitTime, interprocess_filetime *lpKernelTime
1241 , interprocess_filetime *lpUserTime )
1242 { return 0 != GetProcessTimes(hProcess, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime); }
1243
get_current_process_id()1244 inline unsigned long get_current_process_id()
1245 { return GetCurrentProcessId(); }
1246
close_handle(void * handle)1247 inline unsigned int close_handle(void* handle)
1248 { return CloseHandle(handle); }
1249
find_first_file(const char * lpFileName,win32_find_data * lpFindFileData)1250 inline void * find_first_file(const char *lpFileName, win32_find_data *lpFindFileData)
1251 { return FindFirstFileA(lpFileName, lpFindFileData); }
1252
find_next_file(void * hFindFile,win32_find_data * lpFindFileData)1253 inline bool find_next_file(void *hFindFile, win32_find_data *lpFindFileData)
1254 { return FindNextFileA(hFindFile, lpFindFileData) != 0; }
1255
find_close(void * handle)1256 inline bool find_close(void *handle)
1257 { return FindClose(handle) != 0; }
1258
duplicate_current_process_handle(void * hSourceHandle,void ** lpTargetHandle)1259 inline bool duplicate_current_process_handle
1260 (void *hSourceHandle, void **lpTargetHandle)
1261 {
1262 return 0 != DuplicateHandle
1263 ( GetCurrentProcess(), hSourceHandle, GetCurrentProcess()
1264 , lpTargetHandle, 0, 0
1265 , duplicate_same_access);
1266 }
1267
get_file_type(void * hFile)1268 inline unsigned long get_file_type(void *hFile)
1269 {
1270 return GetFileType(hFile);
1271 }
1272
1273 /*
1274 inline void get_system_time_as_file_time(interprocess_filetime *filetime)
1275 { GetSystemTimeAsFileTime(filetime); }
1276
1277 inline bool file_time_to_local_file_time
1278 (const interprocess_filetime *in, const interprocess_filetime *out)
1279 { return 0 != FileTimeToLocalFileTime(in, out); }
1280 */
open_or_create_mutex(const char * name,bool initial_owner,interprocess_security_attributes * attr)1281 inline void *open_or_create_mutex(const char *name, bool initial_owner, interprocess_security_attributes *attr)
1282 { return CreateMutexA(attr, (int)initial_owner, name); }
1283
wait_for_single_object(void * handle,unsigned long time)1284 inline unsigned long wait_for_single_object(void *handle, unsigned long time)
1285 { return WaitForSingleObject(handle, time); }
1286
release_mutex(void * handle)1287 inline int release_mutex(void *handle)
1288 { return ReleaseMutex(handle); }
1289
unmap_view_of_file(void * address)1290 inline int unmap_view_of_file(void *address)
1291 { return UnmapViewOfFile(address); }
1292
open_or_create_semaphore(const char * name,long initial_count,long maximum_count,interprocess_security_attributes * attr)1293 inline void *open_or_create_semaphore(const char *name, long initial_count, long maximum_count, interprocess_security_attributes *attr)
1294 { return CreateSemaphoreA(attr, initial_count, maximum_count, name); }
1295
open_semaphore(const char * name)1296 inline void *open_semaphore(const char *name)
1297 { return OpenSemaphoreA(semaphore_all_access, 0, name); }
1298
release_semaphore(void * handle,long release_count,long * prev_count)1299 inline int release_semaphore(void *handle, long release_count, long *prev_count)
1300 { return ReleaseSemaphore(handle, release_count, prev_count); }
1301
1302 class interprocess_all_access_security
1303 {
1304 interprocess_security_attributes sa;
1305 interprocess_security_descriptor sd;
1306 bool initialized;
1307
1308 public:
interprocess_all_access_security()1309 interprocess_all_access_security()
1310 : initialized(false)
1311 {
1312 if(!InitializeSecurityDescriptor(&sd, security_descriptor_revision))
1313 return;
1314 if(!SetSecurityDescriptorDacl(&sd, true, 0, false))
1315 return;
1316 sa.lpSecurityDescriptor = &sd;
1317 sa.nLength = sizeof(interprocess_security_attributes);
1318 sa.bInheritHandle = false;
1319 initialized = false;
1320 }
1321
get_attributes()1322 interprocess_security_attributes *get_attributes()
1323 { return &sa; }
1324 };
1325
create_file_mapping(void * handle,unsigned long access,::boost::ulong_long_type file_offset,const char * name,interprocess_security_attributes * psec)1326 inline void * create_file_mapping (void * handle, unsigned long access, ::boost::ulong_long_type file_offset, const char * name, interprocess_security_attributes *psec)
1327 {
1328 const unsigned long high_size(file_offset >> 32), low_size((boost::uint32_t)file_offset);
1329 return CreateFileMappingA (handle, psec, access, high_size, low_size, name);
1330 }
1331
open_file_mapping(unsigned long access,const char * name)1332 inline void * open_file_mapping (unsigned long access, const char *name)
1333 { return OpenFileMappingA (access, 0, name); }
1334
map_view_of_file_ex(void * handle,unsigned long file_access,::boost::ulong_long_type offset,std::size_t numbytes,void * base_addr)1335 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)
1336 {
1337 const unsigned long offset_low = (unsigned long)(offset & ((::boost::ulong_long_type)0xFFFFFFFF));
1338 const unsigned long offset_high = offset >> 32;
1339 return MapViewOfFileEx(handle, file_access, offset_high, offset_low, numbytes, base_addr);
1340 }
1341
create_file(const char * name,unsigned long access,unsigned long creation_flags,unsigned long attributes,interprocess_security_attributes * psec)1342 inline void *create_file(const char *name, unsigned long access, unsigned long creation_flags, unsigned long attributes, interprocess_security_attributes *psec)
1343 {
1344 for (unsigned int attempt(0); attempt < error_sharing_violation_tries; ++attempt){
1345 void * const handle = CreateFileA(name, access,
1346 file_share_read | file_share_write | file_share_delete,
1347 psec, creation_flags, attributes, 0);
1348 bool const invalid(invalid_handle_value == handle);
1349 if (!invalid){
1350 return handle;
1351 }
1352 if (error_sharing_violation != get_last_error()){
1353 return handle;
1354 }
1355 sleep(error_sharing_violation_sleep_ms);
1356 }
1357 return invalid_handle_value;
1358 }
1359
get_system_info(system_info * info)1360 inline void get_system_info(system_info *info)
1361 { GetSystemInfo(info); }
1362
flush_view_of_file(void * base_addr,std::size_t numbytes)1363 inline bool flush_view_of_file(void *base_addr, std::size_t numbytes)
1364 { return 0 != FlushViewOfFile(base_addr, numbytes); }
1365
virtual_unlock(void * base_addr,std::size_t numbytes)1366 inline bool virtual_unlock(void *base_addr, std::size_t numbytes)
1367 { return 0 != VirtualUnlock(base_addr, numbytes); }
1368
virtual_protect(void * base_addr,std::size_t numbytes,unsigned long flNewProtect,unsigned long & lpflOldProtect)1369 inline bool virtual_protect(void *base_addr, std::size_t numbytes, unsigned long flNewProtect, unsigned long &lpflOldProtect)
1370 { return 0 != VirtualProtect(base_addr, numbytes, flNewProtect, &lpflOldProtect); }
1371
flush_file_buffers(void * handle)1372 inline bool flush_file_buffers(void *handle)
1373 { return 0 != FlushFileBuffers(handle); }
1374
get_file_size(void * handle,__int64 & size)1375 inline bool get_file_size(void *handle, __int64 &size)
1376 { return 0 != GetFileSizeEx(handle, (large_integer*)&size); }
1377
create_directory(const char * name)1378 inline bool create_directory(const char *name)
1379 {
1380 interprocess_all_access_security sec;
1381 return 0 != CreateDirectoryA(name, sec.get_attributes());
1382 }
1383
remove_directory(const char * lpPathName)1384 inline bool remove_directory(const char *lpPathName)
1385 { return 0 != RemoveDirectoryA(lpPathName); }
1386
get_temp_path(unsigned long length,char * buffer)1387 inline unsigned long get_temp_path(unsigned long length, char *buffer)
1388 { return GetTempPathA(length, buffer); }
1389
set_end_of_file(void * handle)1390 inline int set_end_of_file(void *handle)
1391 { return 0 != SetEndOfFile(handle); }
1392
set_file_pointer_ex(void * handle,__int64 distance,__int64 * new_file_pointer,unsigned long move_method)1393 inline bool set_file_pointer_ex(void *handle, __int64 distance, __int64 *new_file_pointer, unsigned long move_method)
1394 {
1395 large_integer d; d.QuadPart = distance;
1396 return 0 != SetFilePointerEx(handle, d, (large_integer*)new_file_pointer, move_method);
1397 }
1398
lock_file_ex(void * hnd,unsigned long flags,unsigned long reserved,unsigned long size_low,unsigned long size_high,interprocess_overlapped * overlapped)1399 inline bool lock_file_ex(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
1400 { return 0 != LockFileEx(hnd, flags, reserved, size_low, size_high, overlapped); }
1401
unlock_file_ex(void * hnd,unsigned long reserved,unsigned long size_low,unsigned long size_high,interprocess_overlapped * overlapped)1402 inline bool unlock_file_ex(void *hnd, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
1403 { return 0 != UnlockFileEx(hnd, reserved, size_low, size_high, overlapped); }
1404
write_file(void * hnd,const void * buffer,unsigned long bytes_to_write,unsigned long * bytes_written,interprocess_overlapped * overlapped)1405 inline bool write_file(void *hnd, const void *buffer, unsigned long bytes_to_write, unsigned long *bytes_written, interprocess_overlapped* overlapped)
1406 { return 0 != WriteFile(hnd, buffer, bytes_to_write, bytes_written, overlapped); }
1407
read_file(void * hnd,void * buffer,unsigned long bytes_to_read,unsigned long * bytes_read,interprocess_overlapped * overlapped)1408 inline bool read_file(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped)
1409 { return 0 != ReadFile(hnd, buffer, bytes_to_read, bytes_read, overlapped); }
1410
get_file_information_by_handle(void * hnd,interprocess_by_handle_file_information * info)1411 inline bool get_file_information_by_handle(void *hnd, interprocess_by_handle_file_information *info)
1412 { return 0 != GetFileInformationByHandle(hnd, info); }
1413
interlocked_increment(long volatile * addr)1414 inline long interlocked_increment(long volatile *addr)
1415 { return BOOST_INTERLOCKED_INCREMENT(addr); }
1416
interlocked_decrement(long volatile * addr)1417 inline long interlocked_decrement(long volatile *addr)
1418 { return BOOST_INTERLOCKED_DECREMENT(addr); }
1419
interlocked_compare_exchange(long volatile * addr,long val1,long val2)1420 inline long interlocked_compare_exchange(long volatile *addr, long val1, long val2)
1421 { return BOOST_INTERLOCKED_COMPARE_EXCHANGE(addr, val1, val2); }
1422
interlocked_exchange_add(long volatile * addend,long value)1423 inline long interlocked_exchange_add(long volatile* addend, long value)
1424 { return BOOST_INTERLOCKED_EXCHANGE_ADD(const_cast<long*>(addend), value); }
1425
interlocked_exchange(long volatile * addend,long value)1426 inline long interlocked_exchange(long volatile* addend, long value)
1427 { return BOOST_INTERLOCKED_EXCHANGE(const_cast<long*>(addend), value); }
1428
1429 //Forward functions
load_library(const char * name)1430 inline hmodule load_library(const char *name)
1431 { return LoadLibraryA(name); }
1432
free_library(hmodule module)1433 inline bool free_library(hmodule module)
1434 { return 0 != FreeLibrary(module); }
1435
get_proc_address(hmodule module,const char * name)1436 inline farproc_t get_proc_address(hmodule module, const char *name)
1437 { return GetProcAddress(module, name); }
1438
get_current_process()1439 inline void *get_current_process()
1440 { return GetCurrentProcess(); }
1441
get_module_handle(const char * name)1442 inline hmodule get_module_handle(const char *name)
1443 { return GetModuleHandleA(name); }
1444
reg_open_key_ex(hkey hKey,const char * lpSubKey,unsigned long ulOptions,unsigned long samDesired,hkey * phkResult)1445 inline long reg_open_key_ex(hkey hKey, const char *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
1446 { return RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
1447
reg_query_value_ex(hkey hKey,const char * lpValueName,unsigned long * lpReserved,unsigned long * lpType,unsigned char * lpData,unsigned long * lpcbData)1448 inline long reg_query_value_ex(hkey hKey, const char *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
1449 { return RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
1450
reg_close_key(hkey hKey)1451 inline long reg_close_key(hkey hKey)
1452 { return RegCloseKey(hKey); }
1453
initialize_object_attributes(object_attributes_t * pobject_attr,unicode_string_t * name,unsigned long attr,void * rootdir,void * security_descr)1454 inline void initialize_object_attributes
1455 ( object_attributes_t *pobject_attr, unicode_string_t *name
1456 , unsigned long attr, void *rootdir, void *security_descr)
1457
1458 {
1459 pobject_attr->Length = sizeof(object_attributes_t);
1460 pobject_attr->RootDirectory = rootdir;
1461 pobject_attr->Attributes = attr;
1462 pobject_attr->ObjectName = name;
1463 pobject_attr->SecurityDescriptor = security_descr;
1464 pobject_attr->SecurityQualityOfService = 0;
1465 }
1466
rtl_init_empty_unicode_string(unicode_string_t * ucStr,wchar_t * buf,unsigned short bufSize)1467 inline void rtl_init_empty_unicode_string(unicode_string_t *ucStr, wchar_t *buf, unsigned short bufSize)
1468 {
1469 ucStr->Buffer = buf;
1470 ucStr->Length = 0;
1471 ucStr->MaximumLength = bufSize;
1472 }
1473
1474 //A class that locates and caches loaded DLL function addresses.
1475 template<int Dummy>
1476 struct function_address_holder
1477 {
1478 enum { NtSetInformationFile
1479 , NtQuerySystemInformation
1480 , NtQueryObject
1481 , NtQuerySemaphore
1482 , NtQuerySection
1483 , NtOpenFile
1484 , NtClose
1485 , NtQueryTimerResolution
1486 , NtSetTimerResolution
1487 , QueryPerformanceCounter
1488 , QueryPerformanceFrequency
1489 , NumFunction
1490 };
1491 enum { NtDll_dll, Kernel32_dll, NumModule };
1492
1493 private:
1494 static const char *FunctionNames[NumFunction];
1495 static const char *ModuleNames[NumModule];
1496 static farproc_t FunctionAddresses[NumFunction];
1497 static unsigned int FunctionModules[NumFunction];
1498 static volatile long FunctionStates[NumFunction];
1499 static hmodule ModuleAddresses[NumModule];
1500 static volatile long ModuleStates[NumModule];
1501
get_module_from_idboost::interprocess::winapi::function_address_holder1502 static hmodule get_module_from_id(unsigned int id)
1503 {
1504 BOOST_ASSERT(id < (unsigned int)NumModule);
1505 hmodule addr = get_module_handle(ModuleNames[id]);
1506 BOOST_ASSERT(addr);
1507 return addr;
1508 }
1509
get_moduleboost::interprocess::winapi::function_address_holder1510 static hmodule get_module(const unsigned int id)
1511 {
1512 BOOST_ASSERT(id < (unsigned int)NumModule);
1513 for(unsigned i = 0; ModuleStates[id] < 2; ++i){
1514 if(interlocked_compare_exchange(&ModuleStates[id], 1, 0) == 0){
1515 ModuleAddresses[id] = get_module_from_id(id);
1516 interlocked_increment(&ModuleStates[id]);
1517 break;
1518 }
1519 else if(i & 1){
1520 sched_yield();
1521 }
1522 else{
1523 sleep_tick();
1524 }
1525 }
1526 return ModuleAddresses[id];
1527 }
1528
get_address_from_dllboost::interprocess::winapi::function_address_holder1529 static farproc_t get_address_from_dll(const unsigned int id)
1530 {
1531 BOOST_ASSERT(id < (unsigned int)NumFunction);
1532 farproc_t addr = get_proc_address(get_module(FunctionModules[id]), FunctionNames[id]);
1533 BOOST_ASSERT(addr);
1534 return addr;
1535 }
1536
1537 public:
getboost::interprocess::winapi::function_address_holder1538 static farproc_t get(const unsigned int id)
1539 {
1540 BOOST_ASSERT(id < (unsigned int)NumFunction);
1541 for(unsigned i = 0; FunctionStates[id] < 2; ++i){
1542 if(interlocked_compare_exchange(&FunctionStates[id], 1, 0) == 0){
1543 FunctionAddresses[id] = get_address_from_dll(id);
1544 interlocked_increment(&FunctionStates[id]);
1545 break;
1546 }
1547 else if(i & 1){
1548 sched_yield();
1549 }
1550 else{
1551 sleep_tick();
1552 }
1553 }
1554 return FunctionAddresses[id];
1555 }
1556 };
1557
1558 template<int Dummy>
1559 const char *function_address_holder<Dummy>::FunctionNames[function_address_holder<Dummy>::NumFunction] =
1560 {
1561 "NtSetInformationFile",
1562 "NtQuerySystemInformation",
1563 "NtQueryObject",
1564 "NtQuerySemaphore",
1565 "NtQuerySection",
1566 "NtOpenFile",
1567 "NtClose",
1568 "NtQueryTimerResolution",
1569 "NtSetTimerResolution",
1570 "QueryPerformanceCounter",
1571 "QueryPerformanceFrequency"
1572 };
1573
1574 template<int Dummy>
1575 unsigned int function_address_holder<Dummy>::FunctionModules[function_address_holder<Dummy>::NumFunction] =
1576 {
1577 NtDll_dll,
1578 NtDll_dll,
1579 NtDll_dll,
1580 NtDll_dll,
1581 NtDll_dll,
1582 NtDll_dll,
1583 NtDll_dll,
1584 NtDll_dll,
1585 NtDll_dll,
1586 Kernel32_dll,
1587 Kernel32_dll
1588 };
1589
1590 template<int Dummy>
1591 const char *function_address_holder<Dummy>::ModuleNames[function_address_holder<Dummy>::NumModule] =
1592 {
1593 "ntdll.dll",
1594 "kernel32.dll"
1595 };
1596
1597
1598 template<int Dummy>
1599 farproc_t function_address_holder<Dummy>::FunctionAddresses[function_address_holder<Dummy>::NumFunction];
1600
1601 template<int Dummy>
1602 volatile long function_address_holder<Dummy>::FunctionStates[function_address_holder<Dummy>::NumFunction];
1603
1604 template<int Dummy>
1605 hmodule function_address_holder<Dummy>::ModuleAddresses[function_address_holder<Dummy>::NumModule];
1606
1607 template<int Dummy>
1608 volatile long function_address_holder<Dummy>::ModuleStates[function_address_holder<Dummy>::NumModule];
1609
1610
1611 struct dll_func
1612 : public function_address_holder<0>
1613 {};
1614
1615 //Complex winapi based functions...
1616 struct library_unloader
1617 {
1618 hmodule lib_;
library_unloaderboost::interprocess::winapi::library_unloader1619 library_unloader(hmodule module) : lib_(module){}
~library_unloaderboost::interprocess::winapi::library_unloader1620 ~library_unloader(){ free_library(lib_); }
1621 };
1622
1623
get_system_time_of_day_information(system_timeofday_information & info)1624 inline bool get_system_time_of_day_information(system_timeofday_information &info)
1625 {
1626 NtQuerySystemInformation_t pNtQuerySystemInformation = (NtQuerySystemInformation_t)
1627 dll_func::get(dll_func::NtQuerySystemInformation);
1628 unsigned long res;
1629 long status = pNtQuerySystemInformation(system_time_of_day_information, &info, sizeof(info), &res);
1630 if(status){
1631 return false;
1632 }
1633 return true;
1634 }
1635
get_boot_time(unsigned char (& bootstamp)[BootstampLength])1636 inline bool get_boot_time(unsigned char (&bootstamp) [BootstampLength])
1637 {
1638 system_timeofday_information info;
1639 bool ret = get_system_time_of_day_information(info);
1640 if(!ret){
1641 return false;
1642 }
1643 std::memcpy(&bootstamp[0], &info.Reserved1, sizeof(bootstamp));
1644 return true;
1645 }
1646
get_boot_and_system_time(unsigned char (& bootsystemstamp)[BootAndSystemstampLength])1647 inline bool get_boot_and_system_time(unsigned char (&bootsystemstamp) [BootAndSystemstampLength])
1648 {
1649 system_timeofday_information info;
1650 bool ret = get_system_time_of_day_information(info);
1651 if(!ret){
1652 return false;
1653 }
1654 std::memcpy(&bootsystemstamp[0], &info.Reserved1, sizeof(bootsystemstamp));
1655 return true;
1656 }
1657
get_boot_time_str(char * bootstamp_str,std::size_t & s)1658 inline bool get_boot_time_str(char *bootstamp_str, std::size_t &s)
1659 //will write BootstampLength chars
1660 {
1661 if(s < (BootstampLength*2))
1662 return false;
1663 system_timeofday_information info;
1664 bool ret = get_system_time_of_day_information(info);
1665 if(!ret){
1666 return false;
1667 }
1668 const char Characters [] =
1669 { '0', '1', '2', '3', '4', '5', '6', '7'
1670 , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1671 std::size_t char_counter = 0;
1672 for(std::size_t i = 0; i != static_cast<std::size_t>(BootstampLength); ++i){
1673 bootstamp_str[char_counter++] = Characters[(info.Reserved1[i]&0xF0)>>4];
1674 bootstamp_str[char_counter++] = Characters[(info.Reserved1[i]&0x0F)];
1675 }
1676 s = BootstampLength*2;
1677 return true;
1678 }
1679
1680 //Writes the hexadecimal value of the buffer, in the wide character string.
1681 //str must be twice length
buffer_to_wide_str(const void * buf,std::size_t length,wchar_t * str)1682 inline void buffer_to_wide_str(const void *buf, std::size_t length, wchar_t *str)
1683 {
1684 const wchar_t Characters [] =
1685 { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7'
1686 , L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
1687 std::size_t char_counter = 0;
1688 const char *chbuf = static_cast<const char *>(buf);
1689 for(std::size_t i = 0; i != length; ++i){
1690 str[char_counter++] = Characters[(chbuf[i]&0xF0)>>4];
1691 str[char_counter++] = Characters[(chbuf[i]&0x0F)];
1692 }
1693 }
1694
get_boot_and_system_time_wstr(wchar_t * bootsystemstamp,std::size_t & s)1695 inline bool get_boot_and_system_time_wstr(wchar_t *bootsystemstamp, std::size_t &s)
1696 //will write BootAndSystemstampLength chars
1697 {
1698 if(s < (BootAndSystemstampLength*2))
1699 return false;
1700 system_timeofday_information info;
1701 bool ret = get_system_time_of_day_information(info);
1702 if(!ret){
1703 return false;
1704 }
1705
1706 buffer_to_wide_str(&info.Reserved1[0], BootAndSystemstampLength, bootsystemstamp);
1707 s = BootAndSystemstampLength*2;
1708 return true;
1709 }
1710
1711 class handle_closer
1712 {
1713 void *handle_;
1714 handle_closer(const handle_closer &);
1715 handle_closer& operator=(const handle_closer &);
1716 public:
handle_closer(void * handle)1717 explicit handle_closer(void *handle) : handle_(handle){}
~handle_closer()1718 ~handle_closer()
1719 { close_handle(handle_); }
1720 };
1721
1722 class eventlog_handle_closer
1723 {
1724 void *handle_;
1725 eventlog_handle_closer(const handle_closer &);
1726 eventlog_handle_closer& operator=(const eventlog_handle_closer &);
1727 public:
eventlog_handle_closer(void * handle)1728 explicit eventlog_handle_closer(void *handle) : handle_(handle){}
~eventlog_handle_closer()1729 ~eventlog_handle_closer()
1730 { CloseEventLog(handle_); }
1731 };
1732
1733 union ntquery_mem_t
1734 {
1735 object_name_information_t name;
1736 struct ren_t
1737 {
1738 file_rename_information_t info;
1739 wchar_t buf[1];
1740 } ren;
1741 };
1742
1743 class nt_query_mem_deleter
1744 {
1745 static const std::size_t rename_offset = offsetof(ntquery_mem_t, ren.info.FileName) -
1746 offsetof(ntquery_mem_t, name.Name.Buffer);
1747 // Timestamp process id atomic count
1748 static const std::size_t rename_suffix =
1749 (SystemTimeOfDayInfoLength + sizeof(unsigned long) + sizeof(boost::uint32_t))*2;
1750
1751 public:
nt_query_mem_deleter(std::size_t object_name_information_size)1752 nt_query_mem_deleter(std::size_t object_name_information_size)
1753 : m_size(object_name_information_size + rename_offset + rename_suffix)
1754 , m_buf(new char [m_size])
1755 {}
1756
~nt_query_mem_deleter()1757 ~nt_query_mem_deleter()
1758 {
1759 delete[]m_buf;
1760 }
1761
realloc_mem(std::size_t num_bytes)1762 void realloc_mem(std::size_t num_bytes)
1763 {
1764 num_bytes += rename_suffix + rename_offset;
1765 char *buf = m_buf;
1766 m_buf = new char[num_bytes];
1767 delete[]buf;
1768 m_size = num_bytes;
1769 }
1770
query_mem() const1771 ntquery_mem_t *query_mem() const
1772 { return static_cast<ntquery_mem_t *>(static_cast<void*>(m_buf)); }
1773
object_name_information_size() const1774 unsigned long object_name_information_size() const
1775 {
1776 return static_cast<unsigned long>(m_size - rename_offset - SystemTimeOfDayInfoLength*2);
1777 }
1778
file_rename_information_size() const1779 std::size_t file_rename_information_size() const
1780 { return static_cast<unsigned long>(m_size); }
1781
1782 private:
1783 std::size_t m_size;
1784 char *m_buf;
1785 };
1786
1787 class c_heap_deleter
1788 {
1789 public:
c_heap_deleter(std::size_t size)1790 c_heap_deleter(std::size_t size)
1791 : m_buf(::malloc(size))
1792 {}
1793
~c_heap_deleter()1794 ~c_heap_deleter()
1795 {
1796 if(m_buf) ::free(m_buf);
1797 }
1798
realloc_mem(std::size_t num_bytes)1799 void realloc_mem(std::size_t num_bytes)
1800 {
1801 void *buf = ::realloc(m_buf, num_bytes);
1802 if(!buf){
1803 free(m_buf);
1804 m_buf = 0;
1805 }
1806 }
1807
get() const1808 void *get() const
1809 { return m_buf; }
1810
1811 private:
1812 void *m_buf;
1813 };
1814
unlink_file(const char * filename)1815 inline bool unlink_file(const char *filename)
1816 {
1817 //Don't try to optimize doing a DeleteFile first
1818 //as there are interactions with permissions and
1819 //in-use files.
1820 //
1821 //if(!delete_file(filename)){
1822 // (...)
1823 //
1824
1825 //This functions tries to emulate UNIX unlink semantics in windows.
1826 //
1827 //- Open the file and mark the handle as delete-on-close
1828 //- Rename the file to an arbitrary name based on a random number
1829 //- Close the handle. If there are no file users, it will be deleted.
1830 // Otherwise it will be used by already connected handles but the
1831 // file name can't be used to open this file again
1832 try{
1833 NtSetInformationFile_t pNtSetInformationFile =
1834 (NtSetInformationFile_t)dll_func::get(dll_func::NtSetInformationFile);
1835
1836 NtQueryObject_t pNtQueryObject = (NtQueryObject_t)dll_func::get(dll_func::NtQueryObject);
1837
1838 //First step: Obtain a handle to the file using Win32 rules. This resolves relative paths
1839 void *fh = create_file(filename, generic_read | delete_access, open_existing, 0, 0);
1840 if(fh == invalid_handle_value){
1841 return false;
1842 }
1843
1844 handle_closer h_closer(fh);
1845 {
1846 //Obtain name length
1847 unsigned long size;
1848 const std::size_t initial_string_mem = 512u;
1849
1850 nt_query_mem_deleter nt_query_mem(sizeof(ntquery_mem_t)+initial_string_mem);
1851 //Obtain file name with guessed length
1852 if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
1853 //Obtain file name with exact length buffer
1854 nt_query_mem.realloc_mem(size);
1855 if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
1856 return false;
1857 }
1858 }
1859 ntquery_mem_t *pmem = nt_query_mem.query_mem();
1860 file_rename_information_t *pfri = &pmem->ren.info;
1861 const std::size_t RenMaxNumChars =
1862 (((char*)(pmem) + nt_query_mem.file_rename_information_size()) - (char*)&pmem->ren.info.FileName[0])/sizeof(wchar_t);
1863
1864 //Copy filename to the rename member
1865 std::memmove(pmem->ren.info.FileName, pmem->name.Name.Buffer, pmem->name.Name.Length);
1866 std::size_t filename_string_length = pmem->name.Name.Length/sizeof(wchar_t);
1867
1868 //Search '\\' character to replace from it
1869 for(std::size_t i = filename_string_length; i != 0; --filename_string_length){
1870 if(pmem->ren.info.FileName[--i] == L'\\')
1871 break;
1872 }
1873
1874 //Add random number
1875 std::size_t s = RenMaxNumChars - filename_string_length;
1876 if(!get_boot_and_system_time_wstr(&pfri->FileName[filename_string_length], s)){
1877 return false;
1878 }
1879 filename_string_length += s;
1880
1881 //Sometimes the precission of the timestamp is not enough and we need to add another random number.
1882 //The process id (to exclude concurrent processes) and an atomic count (to exclude concurrent threads).
1883 //should be enough
1884 const unsigned long pid = get_current_process_id();
1885 buffer_to_wide_str(&pid, sizeof(pid), &pfri->FileName[filename_string_length]);
1886 filename_string_length += sizeof(pid)*2;
1887
1888 static volatile boost::uint32_t u32_count = 0;
1889 interlocked_decrement(reinterpret_cast<volatile long*>(&u32_count));
1890 buffer_to_wide_str(const_cast<const boost::uint32_t *>(&u32_count), sizeof(boost::uint32_t), &pfri->FileName[filename_string_length]);
1891 filename_string_length += sizeof(boost::uint32_t)*2;
1892
1893 //Fill rename information (FileNameLength is in bytes)
1894 pfri->FileNameLength = static_cast<unsigned long>(sizeof(wchar_t)*(filename_string_length));
1895 pfri->Replace = 1;
1896 pfri->RootDir = 0;
1897
1898 //Cange the name of the in-use file...
1899 io_status_block_t io;
1900 if(0 != pNtSetInformationFile(fh, &io, pfri, nt_query_mem.file_rename_information_size(), file_rename_information)){
1901 return false;
1902 }
1903 }
1904 //...and mark it as delete-on-close
1905 {
1906 //Don't use pNtSetInformationFile with file_disposition_information as it can return STATUS_CANNOT_DELETE
1907 //if the file is still mapped. Reopen it with NtOpenFile and file_delete_on_close
1908 NtOpenFile_t pNtOpenFile = (NtOpenFile_t)dll_func::get(dll_func::NtOpenFile);
1909 NtClose_t pNtClose = (NtClose_t)dll_func::get(dll_func::NtClose);
1910 const wchar_t empty_str [] = L"";
1911 unicode_string_t ustring = { sizeof(empty_str) - sizeof (wchar_t) //length in bytes without null
1912 , sizeof(empty_str) //total size in bytes of memory allocated for Buffer.
1913 , const_cast<wchar_t*>(empty_str)
1914 };
1915 object_attributes_t object_attr;
1916 initialize_object_attributes(&object_attr, &ustring, 0, fh, 0);
1917 void* fh2 = 0;
1918 io_status_block_t io;
1919 pNtOpenFile( &fh2, delete_flag, &object_attr, &io
1920 , file_share_read | file_share_write | file_share_delete, file_delete_on_close);
1921 pNtClose(fh2);
1922 //Even if NtOpenFile fails, the file was renamed and the original no longer exists, so return a success status
1923 return true;
1924 }
1925 }
1926 catch(...){
1927 return false;
1928 }
1929 return true;
1930 }
1931
1932 struct reg_closer
1933 {
1934 hkey key_;
reg_closerboost::interprocess::winapi::reg_closer1935 reg_closer(hkey key) : key_(key){}
~reg_closerboost::interprocess::winapi::reg_closer1936 ~reg_closer(){ reg_close_key(key_); }
1937 };
1938
get_shared_documents_folder(std::string & s)1939 inline void get_shared_documents_folder(std::string &s)
1940 {
1941 #if 1 //Original registry search code
1942 s.clear();
1943 hkey key;
1944 if (reg_open_key_ex( hkey_local_machine
1945 , "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
1946 , 0
1947 , key_query_value
1948 , &key) == 0){
1949 reg_closer key_closer(key);
1950
1951 //Obtain the value
1952 unsigned long size;
1953 unsigned long type;
1954 const char *const reg_value = "Common AppData";
1955 //long err = (*pRegQueryValue)( key, reg_value, 0, &type, 0, &size);
1956 long err = reg_query_value_ex( key, reg_value, 0, &type, 0, &size);
1957 if(!err){
1958 //Size includes terminating NULL
1959 s.resize(size);
1960 //err = (*pRegQueryValue)( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
1961 err = reg_query_value_ex( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
1962 if(!err)
1963 s.erase(s.end()-1);
1964 (void)err;
1965 }
1966 }
1967 #else //registry alternative: SHGetSpecialFolderPathA
1968 const int BIPC_CSIDL_COMMON_APPDATA = 0x0023; // All Users\Application Data
1969 const int BIPC_CSIDL_FLAG_CREATE = 0x8000; // new for Win2K, or this in to force creation of folder
1970 const int BIPC_SHGFP_TYPE_CURRENT = 0; // current value for user, verify it exists
1971
1972 s.clear();
1973 char szPath[max_path];
1974 if(0 == SHGetFolderPathA(0, BIPC_CSIDL_COMMON_APPDATA | BIPC_CSIDL_FLAG_CREATE, 0, BIPC_SHGFP_TYPE_CURRENT, szPath)){
1975 s = szPath;
1976 }
1977
1978 #endif
1979 }
1980
get_registry_value(const char * folder,const char * value_key,std::vector<unsigned char> & s)1981 inline void get_registry_value(const char *folder, const char *value_key, std::vector<unsigned char> &s)
1982 {
1983 s.clear();
1984 hkey key;
1985 if (reg_open_key_ex( hkey_local_machine
1986 , folder
1987 , 0
1988 , key_query_value
1989 , &key) == 0){
1990 reg_closer key_closer(key);
1991
1992 //Obtain the value
1993 unsigned long size;
1994 unsigned long type;
1995 const char *const reg_value = value_key;
1996 //long err = (*pRegQueryValue)( key, reg_value, 0, &type, 0, &size);
1997 long err = reg_query_value_ex( key, reg_value, 0, &type, 0, &size);
1998 if(!err){
1999 //Size includes terminating NULL
2000 s.resize(size);
2001 //err = (*pRegQueryValue)( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
2002 err = reg_query_value_ex( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
2003 if(!err)
2004 s.erase(s.end()-1);
2005 (void)err;
2006 }
2007 }
2008 }
2009
2010 struct co_uninitializer
2011 {
co_uninitializerboost::interprocess::winapi::co_uninitializer2012 co_uninitializer(bool b_uninitialize)
2013 : m_b_uninitialize(b_uninitialize)
2014 {}
2015
~co_uninitializerboost::interprocess::winapi::co_uninitializer2016 ~co_uninitializer()
2017 {
2018 if(m_b_uninitialize){
2019 CoUninitialize();
2020 }
2021 }
2022
2023 private:
2024 const bool m_b_uninitialize;
2025 };
2026
2027 template<class Object>
2028 struct com_releaser
2029 {
2030 Object *&object_;
com_releaserboost::interprocess::winapi::com_releaser2031 com_releaser(Object *&object) : object_(object) {}
~com_releaserboost::interprocess::winapi::com_releaser2032 ~com_releaser() { object_->Release(); object_ = 0; }
2033 };
2034
get_wmi_class_attribute(std::wstring & strValue,const wchar_t * wmi_class,const wchar_t * wmi_class_var)2035 inline bool get_wmi_class_attribute( std::wstring& strValue, const wchar_t *wmi_class, const wchar_t *wmi_class_var)
2036 {
2037 //See example http://msdn.microsoft.com/en-us/library/aa390423%28v=VS.85%29.aspx
2038 //
2039 //See BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL definition if you need to change the
2040 //default value of this macro in your application
2041 long co_init_ret = CoInitializeEx(0, BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL);
2042 if(co_init_ret != S_OK_BIPC && co_init_ret != S_FALSE_BIPC && co_init_ret != RPC_E_CHANGED_MODE_BIPC)
2043 return false;
2044 co_uninitializer co_initialize_end(co_init_ret != RPC_E_CHANGED_MODE_BIPC);
2045 (void)co_initialize_end;
2046
2047 bool bRet = false;
2048 long sec_init_ret = CoInitializeSecurity
2049 ( 0 //pVoid
2050 ,-1 //cAuthSvc
2051 , 0 //asAuthSvc
2052 , 0 //pReserved1
2053 , RPC_C_AUTHN_LEVEL_PKT_BIPC //dwAuthnLevel
2054 , RPC_C_IMP_LEVEL_IMPERSONATE_BIPC //dwImpLevel
2055 , 0 //pAuthList
2056 , EOAC_NONE_BIPC //dwCapabilities
2057 , 0 //pReserved3
2058 );
2059 if( 0 == sec_init_ret || RPC_E_TOO_LATE_BIPC == sec_init_ret)
2060 {
2061 IWbemLocator_BIPC * pIWbemLocator = 0;
2062 const wchar_t * bstrNamespace = L"root\\cimv2";
2063
2064 if( 0 != CoCreateInstance(
2065 CLSID_WbemAdministrativeLocator,
2066 0,
2067 CLSCTX_INPROC_SERVER_BIPC | CLSCTX_LOCAL_SERVER_BIPC,
2068 IID_IUnknown, (void **)&pIWbemLocator)){
2069 return false;
2070 }
2071
2072 com_releaser<IWbemLocator_BIPC> IWbemLocator_releaser(pIWbemLocator);
2073
2074 IWbemServices_BIPC *pWbemServices = 0;
2075
2076 if( 0 != pIWbemLocator->ConnectServer(
2077 (bstr)bstrNamespace, // Namespace
2078 0, // Userid
2079 0, // PW
2080 0, // Locale
2081 0, // flags
2082 0, // Authority
2083 0, // Context
2084 &pWbemServices
2085 )
2086 ){
2087 return false;
2088 }
2089
2090 if( S_OK_BIPC != CoSetProxyBlanket(
2091 pWbemServices,
2092 RPC_C_AUTHN_DEFAULT_BIPC,
2093 RPC_C_AUTHZ_DEFAULT_BIPC,
2094 0,
2095 RPC_C_AUTHN_LEVEL_PKT_BIPC,
2096 RPC_C_IMP_LEVEL_IMPERSONATE_BIPC,
2097 0,
2098 EOAC_NONE_BIPC
2099 )
2100 ){
2101 return false;
2102 }
2103
2104 com_releaser<IWbemServices_BIPC> IWbemServices_releaser(pWbemServices);
2105
2106 strValue.clear();
2107 strValue += L"Select ";
2108 strValue += wmi_class_var;
2109 strValue += L" from ";
2110 strValue += wmi_class;
2111
2112 IEnumWbemClassObject_BIPC * pEnumObject = 0;
2113
2114 if ( 0 != pWbemServices->ExecQuery(
2115 (bstr)L"WQL",
2116 (bstr)strValue.c_str(),
2117 //WBEM_FLAG_RETURN_IMMEDIATELY_BIPC,
2118 WBEM_FLAG_RETURN_WHEN_COMPLETE_BIPC | WBEM_FLAG_FORWARD_ONLY_BIPC,
2119 0,
2120 &pEnumObject
2121 )
2122 ){
2123 return false;
2124 }
2125
2126 com_releaser<IEnumWbemClassObject_BIPC> IEnumWbemClassObject_releaser(pEnumObject);
2127
2128 //WBEM_FLAG_FORWARD_ONLY_BIPC incompatible with Reset
2129 //if ( 0 != pEnumObject->Reset() ){
2130 //return false;
2131 //}
2132
2133 wchar_variant vwchar;
2134 unsigned long uCount = 1, uReturned;
2135 IWbemClassObject_BIPC * pClassObject = 0;
2136 while( 0 == pEnumObject->Next( WBEM_INFINITE_BIPC, uCount, &pClassObject, &uReturned ) )
2137 {
2138 com_releaser<IWbemClassObject_BIPC> IWbemClassObject_releaser(pClassObject);
2139 if ( 0 == pClassObject->Get( (bstr)L"LastBootUpTime", 0, &vwchar, 0, 0 ) ){
2140 bRet = true;
2141 strValue = (wchar_t*)vwchar.bstrVal;
2142 VariantClear(&vwchar );
2143 break;
2144 }
2145 }
2146 }
2147 return bRet;
2148 }
2149
2150 #ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME
2151
2152 //Obtains the bootup time from WMI LastBootUpTime.
2153 //This time seems to change with hibernation and clock synchronization so avoid it.
get_last_bootup_time(std::wstring & strValue)2154 inline bool get_last_bootup_time( std::wstring& strValue )
2155 {
2156 bool ret = get_wmi_class_attribute(strValue, L"Win32_OperatingSystem", L"LastBootUpTime");
2157 std::size_t timezone = strValue.find(L'+');
2158 if(timezone != std::wstring::npos){
2159 strValue.erase(timezone);
2160 }
2161 timezone = strValue.find(L'-');
2162 if(timezone != std::wstring::npos){
2163 strValue.erase(timezone);
2164 }
2165 return ret;
2166 }
2167
get_last_bootup_time(std::string & str)2168 inline bool get_last_bootup_time( std::string& str )
2169 {
2170 std::wstring wstr;
2171 bool ret = get_last_bootup_time(wstr);
2172 str.resize(wstr.size());
2173 for(std::size_t i = 0, max = str.size(); i != max; ++i){
2174 str[i] = '0' + (wstr[i]-L'0');
2175 }
2176 return ret;
2177 }
2178
2179 #else
2180
2181 // Loop through the buffer and obtain the contents of the
2182 // requested record in the buffer.
find_record_in_buffer(const void * pBuffer,unsigned long dwBytesRead,const char * provider_name,unsigned int id_to_find,interprocess_eventlogrecord * & pevent_log_record)2183 inline bool find_record_in_buffer( const void* pBuffer, unsigned long dwBytesRead, const char *provider_name
2184 , unsigned int id_to_find, interprocess_eventlogrecord *&pevent_log_record)
2185 {
2186 const unsigned char * pRecord = static_cast<const unsigned char*>(pBuffer);
2187 const unsigned char * pEndOfRecords = pRecord + dwBytesRead;
2188
2189 while (pRecord < pEndOfRecords){
2190 interprocess_eventlogrecord *pTypedRecord = (interprocess_eventlogrecord*)pRecord;
2191 // Check provider, written at the end of the fixed-part of the record
2192 if (0 == std::strcmp(provider_name, (char*)(pRecord + sizeof(interprocess_eventlogrecord))))
2193 {
2194 // Check event id
2195 if(id_to_find == (pTypedRecord->EventID & 0xFFFF)){
2196 pevent_log_record = pTypedRecord;
2197 return true;
2198 }
2199 }
2200
2201 pRecord += pTypedRecord->Length;
2202 }
2203 pevent_log_record = 0;
2204 return false;
2205 }
2206
2207 //Obtains the bootup time from the System Event Log,
2208 //event ID == 6005 (event log started).
2209 //Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/bb427356.aspx
get_last_bootup_time(std::string & stamp)2210 inline bool get_last_bootup_time(std::string &stamp)
2211 {
2212 const char *source_name = "System";
2213 const char *provider_name = "EventLog";
2214 const unsigned short event_id = 6005u;
2215
2216 unsigned long status = 0;
2217 unsigned long dwBytesToRead = 0;
2218 unsigned long dwBytesRead = 0;
2219 unsigned long dwMinimumBytesToRead = 0;
2220
2221 // The source name (provider) must exist as a subkey of Application.
2222 void *hEventLog = OpenEventLogA(0, source_name);
2223 if (hEventLog){
2224 eventlog_handle_closer hnd_closer(hEventLog); (void)hnd_closer;
2225 // Allocate an initial block of memory used to read event records. The number
2226 // of records read into the buffer will vary depending on the size of each event.
2227 // The size of each event will vary based on the size of the user-defined
2228 // data included with each event, the number and length of insertion
2229 // strings, and other data appended to the end of the event record.
2230 dwBytesToRead = max_record_buffer_size;
2231 c_heap_deleter heap_deleter(dwBytesToRead);
2232
2233 // Read blocks of records until you reach the end of the log or an
2234 // error occurs. The records are read from newest to oldest. If the buffer
2235 // is not big enough to hold a complete event record, reallocate the buffer.
2236 if (heap_deleter.get() != 0){
2237 while (0 == status){
2238 if (!ReadEventLogA(hEventLog,
2239 eventlog_sequential_read | eventlog_backwards_read,
2240 0,
2241 heap_deleter.get(),
2242 dwBytesToRead,
2243 &dwBytesRead,
2244 &dwMinimumBytesToRead)) {
2245 status = get_last_error();
2246 if (error_insufficient_buffer == status) {
2247 status = 0;
2248 dwBytesToRead = dwMinimumBytesToRead;
2249 heap_deleter.realloc_mem(dwMinimumBytesToRead);
2250 if (!heap_deleter.get()){
2251 return false;
2252 }
2253 }
2254 else{ //Not found or EOF
2255 return false;
2256 }
2257 }
2258 else
2259 {
2260 interprocess_eventlogrecord *pTypedRecord;
2261 // Print the contents of each record in the buffer.
2262 if(find_record_in_buffer(heap_deleter.get(), dwBytesRead, provider_name, event_id, pTypedRecord)){
2263 char stamp_str[sizeof(unsigned long)*3+1];
2264 std::sprintf(&stamp_str[0], "%u", ((unsigned int)pTypedRecord->TimeGenerated));
2265 stamp = stamp_str;
2266 break;
2267 }
2268 }
2269 }
2270 }
2271 }
2272 return true;
2273 }
2274
2275 #endif
2276
is_directory(const char * path)2277 inline bool is_directory(const char *path)
2278 {
2279 unsigned long attrib = GetFileAttributesA(path);
2280
2281 return (attrib != invalid_file_attributes &&
2282 (attrib & file_attribute_directory));
2283 }
2284
get_file_mapping_size(void * file_mapping_hnd,__int64 & size)2285 inline bool get_file_mapping_size(void *file_mapping_hnd, __int64 &size)
2286 {
2287 NtQuerySection_t pNtQuerySection =
2288 (NtQuerySection_t)dll_func::get(dll_func::NtQuerySection);
2289 //Obtain file name
2290 interprocess_section_basic_information info;
2291 unsigned long ntstatus =
2292 pNtQuerySection(file_mapping_hnd, section_basic_information, &info, sizeof(info), 0);
2293 size = info.section_size;
2294 return !ntstatus;
2295 }
2296
get_semaphore_info(void * handle,long & count,long & limit)2297 inline bool get_semaphore_info(void *handle, long &count, long &limit)
2298 {
2299 winapi::interprocess_semaphore_basic_information info;
2300 winapi::NtQuerySemaphore_t pNtQuerySemaphore =
2301 (winapi::NtQuerySemaphore_t)dll_func::get(winapi::dll_func::NtQuerySemaphore);
2302 unsigned int ret_len;
2303 long status = pNtQuerySemaphore(handle, winapi::semaphore_basic_information, &info, sizeof(info), &ret_len);
2304 count = info.count;
2305 limit = info.limit;
2306 return !status;
2307 }
2308
query_timer_resolution(unsigned long * lowres,unsigned long * highres,unsigned long * curres)2309 inline bool query_timer_resolution(unsigned long *lowres, unsigned long *highres, unsigned long *curres)
2310 {
2311 winapi::NtQueryTimerResolution_t pNtQueryTimerResolution =
2312 (winapi::NtQueryTimerResolution_t)dll_func::get(winapi::dll_func::NtQueryTimerResolution);
2313 return !pNtQueryTimerResolution(lowres, highres, curres);
2314 }
2315
set_timer_resolution(unsigned long RequestedResolution,int Set,unsigned long * ActualResolution)2316 inline bool set_timer_resolution(unsigned long RequestedResolution, int Set, unsigned long* ActualResolution)
2317 {
2318 winapi::NtSetTimerResolution_t pNtSetTimerResolution =
2319 (winapi::NtSetTimerResolution_t)dll_func::get(winapi::dll_func::NtSetTimerResolution);
2320 return !pNtSetTimerResolution(RequestedResolution, Set, ActualResolution);
2321 }
2322
query_performance_counter(__int64 * lpPerformanceCount)2323 inline bool query_performance_counter(__int64 *lpPerformanceCount)
2324 {
2325 QueryPerformanceCounter_t pQueryPerformanceCounter = (QueryPerformanceCounter_t)
2326 dll_func::get(dll_func::QueryPerformanceCounter);
2327 return 0 != pQueryPerformanceCounter(lpPerformanceCount);
2328 }
2329
query_performance_frequency(__int64 * lpFrequency)2330 inline bool query_performance_frequency(__int64 *lpFrequency)
2331 {
2332 QueryPerformanceCounter_t pQueryPerformanceFrequency = (QueryPerformanceFrequency_t)
2333 dll_func::get(dll_func::QueryPerformanceFrequency);
2334 return 0 != pQueryPerformanceFrequency(lpFrequency);
2335 }
2336
get_tick_count()2337 inline unsigned long get_tick_count()
2338 { return GetTickCount(); }
2339
2340 } //namespace winapi
2341 } //namespace interprocess
2342 } //namespace boost
2343
2344 #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
2345 # pragma GCC diagnostic pop
2346 #endif
2347
2348 #include <boost/interprocess/detail/config_end.hpp>
2349
2350 #endif //#ifdef BOOST_INTERPROCESS_WIN32_API_HPP
2351