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