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