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/cstdint.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 #endif
37 
38 #if defined(_MSC_VER)
39 #  pragma once
40 #  pragma comment( lib, "Advapi32.lib" )
41 #  pragma comment( lib, "oleaut32.lib" )
42 #  pragma comment( lib, "Ole32.lib" )
43 #endif
44 
45 #if defined (BOOST_INTERPROCESS_WINDOWS)
46 #  include <cstdarg>
47 #  include <boost/detail/interlocked.hpp>
48 #else
49 # error "This file can only be included in Windows OS"
50 #endif
51 
52 //////////////////////////////////////////////////////////////////////////////
53 //
54 // Declaration of Windows structures or typedefs if BOOST_USE_WINDOWS_H is used
55 //
56 //////////////////////////////////////////////////////////////////////////////
57 
58 
59 #if defined(BOOST_GCC)
60 //Ignore -pedantic errors here (anonymous structs, etc.)
61 #  if (BOOST_GCC >= 40600)
62 #     pragma GCC diagnostic push
63 #     if (BOOST_GCC >= 40800)
64 #        pragma GCC diagnostic ignored "-Wpedantic"
65 #     else
66 #        pragma GCC diagnostic ignored "-pedantic"
67 #     endif
68 #     pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
69 #  else
70 #     pragma GCC system_header
71 #  endif
72 //When loading DLLs we have no option but reinterpret casting function types
73 #  if (BOOST_GCC >= 80000)
74 #        pragma GCC diagnostic ignored "-Wcast-function-type"
75 #  endif
76 #endif
77 
78 
79 //#define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
80 //#define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
81 
82 #ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
83 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE 1
84 #else
85 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE 0
86 #endif
87 
88 #ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
89 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE 1
90 #else
91 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE 0
92 #endif
93 
94 #define BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM \
95    (BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE + \
96     BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE)
97 
98 #if 1 < BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM
99 #  error "Only one of \
100           BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED and \
101           BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED can be defined"
102 #endif
103 
104 #if 0 == BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM
105 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
106 #endif
107 
108 
109 namespace boost  {
110 namespace interprocess  {
111 namespace winapi {
112 
113 //Own defines
114 static const unsigned long MaxPath           = 260;
115 
116 //////////////////////////////////////////////////////////////////////////////
117 //
118 // Nt native structures
119 //
120 //////////////////////////////////////////////////////////////////////////////
121 
122 struct interprocess_semaphore_basic_information
123 {
124    unsigned int count;      // current semaphore count
125    unsigned int limit;      // max semaphore count
126 };
127 
128 struct interprocess_section_basic_information
129 {
130   void *          base_address;
131   unsigned long   section_attributes;
132   __int64         section_size;
133 };
134 
135 struct file_rename_information_t {
136    int Replace;
137    void *RootDir;
138    unsigned long FileNameLength;
139    wchar_t FileName[1];
140 };
141 
142 struct unicode_string_t {
143    unsigned short Length;
144    unsigned short MaximumLength;
145    wchar_t *Buffer;
146 };
147 
148 struct object_attributes_t {
149    unsigned long Length;
150    void * RootDirectory;
151    unicode_string_t *ObjectName;
152    unsigned long Attributes;
153    void *SecurityDescriptor;
154    void *SecurityQualityOfService;
155 };
156 
157 struct io_status_block_t {
158    union {
159       long Status;
160       void *Pointer;
161    };
162 
163    unsigned long *Information;
164 };
165 
166 union system_timeofday_information
167 {
168    struct data_t
169    {
170       __int64 liKeBootTime;
171       __int64 liKeSystemTime;
172       __int64 liExpTimeZoneBias;
173       unsigned long uCurrentTimeZoneId;
174       unsigned long dwReserved;
175       ::boost::ulong_long_type ullBootTimeBias;
176       ::boost::ulong_long_type ullSleepTimeBias;
177    } data;
178    unsigned char Reserved1[sizeof(data_t)];
179 };
180 
181 static const long BootstampLength            = sizeof(__int64);
182 static const long BootAndSystemstampLength   = sizeof(__int64)*2;
183 static const long SystemTimeOfDayInfoLength  = sizeof(system_timeofday_information::data_t);
184 
185 struct object_name_information_t
186 {
187    unicode_string_t Name;
188    wchar_t NameBuffer[1];
189 };
190 
191 enum file_information_class_t {
192    file_directory_information = 1,
193    file_full_directory_information,
194    file_both_directory_information,
195    file_basic_information,
196    file_standard_information,
197    file_internal_information,
198    file_ea_information,
199    file_access_information,
200    file_name_information,
201    file_rename_information,
202    file_link_information,
203    file_names_information,
204    file_disposition_information,
205    file_position_information,
206    file_full_ea_information,
207    file_mode_information,
208    file_alignment_information,
209    file_all_information,
210    file_allocation_information,
211    file_end_of_file_information,
212    file_alternate_name_information,
213    file_stream_information,
214    file_pipe_information,
215    file_pipe_local_information,
216    file_pipe_remote_information,
217    file_mailslot_query_information,
218    file_mailslot_set_information,
219    file_compression_information,
220    file_copy_on_write_information,
221    file_completion_information,
222    file_move_cluster_information,
223    file_quota_information,
224    file_reparse_point_information,
225    file_network_open_information,
226    file_object_id_information,
227    file_tracking_information,
228    file_ole_directory_information,
229    file_content_index_information,
230    file_inherit_content_index_information,
231    file_ole_information,
232    file_maximum_information
233 };
234 
235 enum semaphore_information_class {
236    semaphore_basic_information = 0
237 };
238 
239 
240 enum system_information_class {
241    system_basic_information = 0,
242    system_performance_information = 2,
243    system_time_of_day_information = 3,
244    system_process_information = 5,
245    system_processor_performance_information = 8,
246    system_interrupt_information = 23,
247    system_exception_information = 33,
248    system_registry_quota_information = 37,
249    system_lookaside_information = 45
250 };
251 
252 enum object_information_class
253 {
254    object_basic_information,
255    object_name_information,
256    object_type_information,
257    object_all_information,
258    object_data_information
259 };
260 
261 enum section_information_class
262 {
263    section_basic_information,
264    section_image_information
265 };
266 
267 }  //namespace winapi {
268 }  //namespace interprocess  {
269 }  //namespace boost  {
270 
271 
272 //////////////////////////////////////////////////////////////////////////////
273 //
274 // Forward declaration of winapi
275 //
276 //////////////////////////////////////////////////////////////////////////////
277 
278 #include <boost/winapi/get_current_process_id.hpp>
279 #include <boost/winapi/get_current_thread_id.hpp>
280 #include <boost/winapi/get_current_process.hpp>
281 #include <boost/winapi/get_process_times.hpp>
282 #include <boost/winapi/error_codes.hpp>
283 #include <boost/winapi/thread.hpp>
284 #include <boost/winapi/system.hpp>
285 #include <boost/winapi/time.hpp>
286 #include <boost/winapi/timers.hpp>
287 #include <boost/winapi/get_last_error.hpp>
288 #include <boost/winapi/handles.hpp>
289 #include <boost/winapi/file_management.hpp>
290 #include <boost/winapi/mutex.hpp>
291 #include <boost/winapi/wait.hpp>
292 #include <boost/winapi/file_mapping.hpp>
293 #include <boost/winapi/semaphore.hpp>
294 #include <boost/winapi/system.hpp>
295 #include <boost/winapi/error_handling.hpp>
296 #include <boost/winapi/local_memory.hpp>
297 #include <boost/winapi/directory_management.hpp>
298 #include <boost/winapi/security.hpp>
299 #include <boost/winapi/dll.hpp>
300 #include <boost/winapi/basic_types.hpp>
301 
302 //This should go in winapi's basic_types.hpp
303 namespace boost {
304 namespace ipwinapiext {
305 typedef boost::winapi::LONG_ LSTATUS;
306 
307 //#ifndef BOOST_USE_WINDOWS_H
308 //typedef boost::winapi::LARGE_INTEGER_ LARGE_INTEGER_EXT;
309 //#else
310 //typedef LARGE_INTEGER LARGE_INTEGER_EXT;
311 //#endif
312 
313 }} //namespace boost::ipwinapiext
314 
315 #ifndef BOOST_USE_WINDOWS_H
316 
317 extern "C" {
318 
319 //Error handling
320 BOOST_SYMBOL_IMPORT BOOST_WINAPI_DETAIL_VOID BOOST_WINAPI_WINAPI_CC SetLastError(boost::winapi::DWORD_ dwErrCode);
321 
322 //File management
323 BOOST_SYMBOL_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC GetFileType(boost::winapi::HANDLE_ hTemplateFile);
324 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC FlushFileBuffers(boost::winapi::HANDLE_ hFile);
325 //Virtual Memory
326 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualLock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize);
327 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualUnlock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize);
328 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualProtect( boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize
329                                                                               , boost::winapi::DWORD_ flNewProtect, boost::winapi::PDWORD_ lpflOldProtect);
330 //registry.hpp
331 BOOST_WINAPI_DETAIL_DECLARE_HANDLE(HKEY);
332 
333 
334 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegOpenKeyExA
335    (::HKEY hKey, const char *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, ::HKEY *phkResult);
336 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegQueryValueExA
337    (::HKEY hKey, const char *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData);
338 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegCloseKey(::HKEY hKey);
339 
340 
341 //Event Log
342 BOOST_SYMBOL_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC OpenEventLogA(const char* lpUNCServerName, const char* lpSourceName);
343 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_   BOOST_WINAPI_WINAPI_CC CloseEventLog(boost::winapi::HANDLE_ hEventLog);
344 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_   BOOST_WINAPI_WINAPI_CC ReadEventLogA
345    ( boost::winapi::HANDLE_ hEventLog, boost::winapi::DWORD_ dwReadFlags, boost::winapi::DWORD_ dwRecordOffset, void* lpBuffer
346    , boost::winapi::DWORD_ nNumberOfBytesToRead, boost::winapi::DWORD_ *pnBytesRead, boost::winapi::DWORD_ *pnMinNumberOfBytesNeeded);
347 
348 }  //extern "C" {
349 
350 #endif   //#ifndef BOOST_USE_WINDOWS_H
351 
352 namespace boost {
353 namespace ipwinapiext {
354 
355 typedef ::HKEY HKEY_;
356 
357 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
358 
359 //Error handling
SetLastError(boost::winapi::DWORD_ dwErrCode)360 BOOST_FORCEINLINE BOOST_WINAPI_DETAIL_VOID SetLastError(boost::winapi::DWORD_ dwErrCode)
361 {  ::SetLastError(dwErrCode); }
362 
363 //File management
GetFileType(boost::winapi::HANDLE_ hTemplateFile)364 BOOST_FORCEINLINE boost::winapi::DWORD_ GetFileType(boost::winapi::HANDLE_ hTemplateFile)
365 {  return ::GetFileType(hTemplateFile);   }
366 
FlushFileBuffers(boost::winapi::HANDLE_ hFile)367 BOOST_FORCEINLINE boost::winapi::BOOL_ FlushFileBuffers(boost::winapi::HANDLE_ hFile)
368 {  return ::FlushFileBuffers(hFile);   }
369 
370 //Virtual Memory
VirtualLock(boost::winapi::LPVOID_ lpAddress,boost::winapi::SIZE_T_ dwSize)371 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualLock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize)
372 {  return ::VirtualLock(lpAddress, dwSize);  }
373 
VirtualUnlock(boost::winapi::LPVOID_ lpAddress,boost::winapi::SIZE_T_ dwSize)374 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualUnlock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize)
375 {  return ::VirtualUnlock(lpAddress, dwSize);   }
376 
VirtualProtect(boost::winapi::LPVOID_ lpAddress,boost::winapi::SIZE_T_ dwSize,boost::winapi::DWORD_ flNewProtect,boost::winapi::PDWORD_ lpflOldProtect)377 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualProtect( boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize
378                                                      , boost::winapi::DWORD_ flNewProtect, boost::winapi::PDWORD_ lpflOldProtect)
379 {  return ::VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect);  }
380 
381 //registry.hpp
RegOpenKeyExA(boost::ipwinapiext::HKEY_ hKey,const char * lpSubKey,boost::winapi::DWORD_ ulOptions,boost::winapi::DWORD_ samDesired,boost::ipwinapiext::HKEY_ * phkResult)382 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegOpenKeyExA
383    (boost::ipwinapiext::HKEY_ hKey, const char *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, boost::ipwinapiext::HKEY_ *phkResult)
384 {
385    return ::RegOpenKeyExA(reinterpret_cast< ::HKEY >(hKey), lpSubKey, ulOptions, samDesired, reinterpret_cast< ::HKEY* >(phkResult));
386 }
387 
RegQueryValueExA(boost::ipwinapiext::HKEY_ hKey,const char * lpValueName,boost::winapi::DWORD_ * lpReserved,boost::winapi::DWORD_ * lpType,boost::winapi::BYTE_ * lpData,boost::winapi::DWORD_ * lpcbData)388 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegQueryValueExA
389    (boost::ipwinapiext::HKEY_ hKey, const char *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData)
390 {
391    return ::RegQueryValueExA(reinterpret_cast< ::HKEY >(hKey), lpValueName, lpReserved, lpType, lpData, lpcbData);
392 }
393 
RegCloseKey(boost::ipwinapiext::HKEY_ hKey)394 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegCloseKey(boost::ipwinapiext::HKEY_ hKey)
395 {
396    return ::RegCloseKey(reinterpret_cast< ::HKEY >(hKey));
397 }
398 
GetSystemInfo(boost::winapi::LPSYSTEM_INFO_ lpSystemInfo)399 BOOST_FORCEINLINE void GetSystemInfo(boost::winapi::LPSYSTEM_INFO_ lpSystemInfo)
400 {  return ::GetSystemInfo(reinterpret_cast< ::_SYSTEM_INFO* >(lpSystemInfo));   }
401 
402 #endif   //BOOST_WINAPI_PARTITION_APP_SYSTEM
403 
404 }  //namespace ipwinapiext {
405 }  //namespace boost {
406 
407 namespace boost  {
408 namespace interprocess  {
409 namespace winapi {
410 
411 typedef boost::winapi::SYSTEM_INFO_ interprocess_system_info;
412 typedef boost::winapi::OVERLAPPED_ interprocess_overlapped;
413 typedef boost::winapi::FILETIME_ interprocess_filetime;
414 typedef boost::winapi::WIN32_FIND_DATAA_ win32_find_data;
415 typedef boost::winapi::SECURITY_ATTRIBUTES_ interprocess_security_attributes;
416 typedef boost::winapi::SECURITY_DESCRIPTOR_ interprocess_security_descriptor;
417 typedef boost::winapi::BY_HANDLE_FILE_INFORMATION_ interprocess_by_handle_file_information;
418 typedef boost::winapi::HMODULE_ hmodule;
419 typedef boost::ipwinapiext::HKEY_ hkey;
420 typedef boost::winapi::FARPROC_ farproc_t;
421 
422 //ntdll.dll
423 typedef long (__stdcall *NtDeleteFile_t)(object_attributes_t *ObjectAttributes);
424 typedef long (__stdcall *NtSetInformationFile_t)(void *FileHandle, io_status_block_t *IoStatusBlock, void *FileInformation, unsigned long Length, int FileInformationClass );
425 typedef long (__stdcall *NtOpenFile)(void **FileHandle, unsigned long DesiredAccess, object_attributes_t *ObjectAttributes
426                                     , io_status_block_t *IoStatusBlock, unsigned long ShareAccess, unsigned long Length, unsigned long OpenOptions);
427 typedef long (__stdcall *NtQuerySystemInformation_t)(int, void*, unsigned long, unsigned long *);
428 typedef long (__stdcall *NtQueryObject_t)(void*, object_information_class, void *, unsigned long, unsigned long *);
429 typedef long (__stdcall *NtQuerySemaphore_t)(void*, unsigned int info_class, interprocess_semaphore_basic_information *pinfo, unsigned int info_size, unsigned int *ret_len);
430 typedef long (__stdcall *NtQuerySection_t)(void*, section_information_class, interprocess_section_basic_information *pinfo, unsigned long info_size, unsigned long *ret_len);
431 typedef long (__stdcall *NtQueryInformationFile_t)(void *,io_status_block_t *,void *, long, int);
432 typedef long (__stdcall *NtOpenFile_t)(void*,unsigned long ,object_attributes_t*,io_status_block_t*,unsigned long,unsigned long);
433 typedef long (__stdcall *NtClose_t) (void*);
434 typedef long (__stdcall *NtQueryTimerResolution_t) (unsigned long* LowestResolution, unsigned long* HighestResolution, unsigned long* CurrentResolution);
435 typedef long (__stdcall *NtSetTimerResolution_t) (unsigned long RequestedResolution, int Set, unsigned long* ActualResolution);
436 
437 }  //namespace winapi {
438 }  //namespace interprocess  {
439 }  //namespace boost  {
440 
441 //////////////////////////////////////////////////////////////////////////////
442 //
443 // Forward declaration of constants
444 //
445 //////////////////////////////////////////////////////////////////////////////
446 
447 namespace boost {
448 namespace interprocess {
449 namespace winapi {
450 
451 //Some used constants
452 static const unsigned long infinite_time        = 0xFFFFFFFF;
453 static const unsigned long error_already_exists = 183L;
454 static const unsigned long error_invalid_handle = 6L;
455 static const unsigned long error_sharing_violation = 32L;
456 static const unsigned long error_file_not_found = 2u;
457 static const unsigned long error_no_more_files  = 18u;
458 static const unsigned long error_not_locked     = 158L;
459 //Retries in CreateFile, see http://support.microsoft.com/kb/316609
460 static const unsigned long error_sharing_violation_tries = 3L;
461 static const unsigned long error_sharing_violation_sleep_ms = 250L;
462 static const unsigned long error_file_too_large = 223L;
463 static const unsigned long error_insufficient_buffer = 122L;
464 static const unsigned long error_handle_eof = 38L;
465 static const unsigned long semaphore_all_access = (0x000F0000L)|(0x00100000L)|0x3;
466 static const unsigned long mutex_all_access     = (0x000F0000L)|(0x00100000L)|0x0001;
467 
468 static const unsigned long page_readonly        = 0x02;
469 static const unsigned long page_readwrite       = 0x04;
470 static const unsigned long page_writecopy       = 0x08;
471 static const unsigned long page_noaccess        = 0x01;
472 
473 static const unsigned long standard_rights_required   = 0x000F0000L;
474 static const unsigned long section_query              = 0x0001;
475 static const unsigned long section_map_write          = 0x0002;
476 static const unsigned long section_map_read           = 0x0004;
477 static const unsigned long section_map_execute        = 0x0008;
478 static const unsigned long section_extend_size        = 0x0010;
479 static const unsigned long section_all_access         = standard_rights_required |
480                                                         section_query            |
481                                                         section_map_write        |
482                                                         section_map_read         |
483                                                         section_map_execute      |
484                                                         section_extend_size;
485 
486 static const unsigned long file_map_copy        = section_query;
487 static const unsigned long file_map_write       = section_map_write;
488 static const unsigned long file_map_read        = section_map_read;
489 static const unsigned long file_map_all_access  = section_all_access;
490 static const unsigned long delete_access = 0x00010000L;
491 static const unsigned long file_flag_backup_semantics = 0x02000000;
492 static const long file_flag_delete_on_close = 0x04000000;
493 
494 //Native API constants
495 static const unsigned long file_open_for_backup_intent = 0x00004000;
496 static const int file_share_valid_flags = 0x00000007;
497 static const long file_delete_on_close = 0x00001000L;
498 static const long obj_case_insensitive = 0x00000040L;
499 static const long delete_flag = 0x00010000L;
500 
501 static const unsigned long movefile_copy_allowed            = 0x02;
502 static const unsigned long movefile_delay_until_reboot      = 0x04;
503 static const unsigned long movefile_replace_existing        = 0x01;
504 static const unsigned long movefile_write_through           = 0x08;
505 static const unsigned long movefile_create_hardlink         = 0x10;
506 static const unsigned long movefile_fail_if_not_trackable   = 0x20;
507 
508 static const unsigned long file_share_read      = 0x00000001;
509 static const unsigned long file_share_write     = 0x00000002;
510 static const unsigned long file_share_delete    = 0x00000004;
511 
512 static const unsigned long file_attribute_readonly    = 0x00000001;
513 static const unsigned long file_attribute_hidden      = 0x00000002;
514 static const unsigned long file_attribute_system      = 0x00000004;
515 static const unsigned long file_attribute_directory   = 0x00000010;
516 static const unsigned long file_attribute_archive     = 0x00000020;
517 static const unsigned long file_attribute_device      = 0x00000040;
518 static const unsigned long file_attribute_normal      = 0x00000080;
519 static const unsigned long file_attribute_temporary   = 0x00000100;
520 
521 static const unsigned long generic_read         = 0x80000000L;
522 static const unsigned long generic_write        = 0x40000000L;
523 
524 static const unsigned long wait_object_0        = 0;
525 static const unsigned long wait_abandoned       = 0x00000080L;
526 static const unsigned long wait_timeout         = 258L;
527 static const unsigned long wait_failed          = (unsigned long)0xFFFFFFFF;
528 
529 static const unsigned long duplicate_close_source  = (unsigned long)0x00000001;
530 static const unsigned long duplicate_same_access   = (unsigned long)0x00000002;
531 
532 static const unsigned long format_message_allocate_buffer
533    = (unsigned long)0x00000100;
534 static const unsigned long format_message_ignore_inserts
535    = (unsigned long)0x00000200;
536 static const unsigned long format_message_from_string
537    = (unsigned long)0x00000400;
538 static const unsigned long format_message_from_hmodule
539    = (unsigned long)0x00000800;
540 static const unsigned long format_message_from_system
541    = (unsigned long)0x00001000;
542 static const unsigned long format_message_argument_array
543    = (unsigned long)0x00002000;
544 static const unsigned long format_message_max_width_mask
545    = (unsigned long)0x000000FF;
546 static const unsigned long lang_neutral         = (unsigned long)0x00;
547 static const unsigned long sublang_default      = (unsigned long)0x01;
548 static const unsigned long invalid_file_size    = (unsigned long)0xFFFFFFFF;
549 static const unsigned long invalid_file_attributes =  ((unsigned long)-1);
550 static       void * const  invalid_handle_value = ((void*)(long)(-1));
551 
552 static const unsigned long file_type_char    =  0x0002L;
553 static const unsigned long file_type_disk    =  0x0001L;
554 static const unsigned long file_type_pipe    =  0x0003L;
555 static const unsigned long file_type_remote  =  0x8000L;
556 static const unsigned long file_type_unknown =  0x0000L;
557 
558 static const unsigned long create_new        = 1;
559 static const unsigned long create_always     = 2;
560 static const unsigned long open_existing     = 3;
561 static const unsigned long open_always       = 4;
562 static const unsigned long truncate_existing = 5;
563 
564 static const unsigned long file_begin     = 0;
565 static const unsigned long file_current   = 1;
566 static const unsigned long file_end       = 2;
567 
568 static const unsigned long lockfile_fail_immediately  = 1;
569 static const unsigned long lockfile_exclusive_lock    = 2;
570 static const unsigned long error_lock_violation       = 33;
571 static const unsigned long security_descriptor_revision = 1;
572 
573 const unsigned long max_record_buffer_size = 0x10000L;   // 64K
574 const unsigned long max_path = 260;
575 
576 //Keys
577 static const  hkey hkey_local_machine = (hkey)(unsigned long*)(long)(0x80000002);
578 static unsigned long key_query_value    = 0x0001;
579 
580 // Registry types
581 #define reg_none                       ( 0 )   // No value type
582 #define reg_sz                         ( 1 )   // Unicode nul terminated string
583 #define reg_expand_sz                  ( 2 )   // Unicode nul terminated string
584                                                // (with environment variable references)
585 #define reg_binary                     ( 3 )   // Free form binary
586 #define reg_dword                      ( 4 )   // 32-bit number
587 #define reg_dword_little_endian        ( 4 )   // 32-bit number (same as REG_DWORD)
588 #define reg_dword_big_endian           ( 5 )   // 32-bit number
589 #define reg_link                       ( 6 )   // Symbolic Link (unicode)
590 #define reg_multi_sz                   ( 7 )   // Multiple Unicode strings
591 #define reg_resource_list              ( 8 )   // Resource list in the resource map
592 #define reg_full_resource_descriptor   ( 9 )  // Resource list in the hardware description
593 #define reg_resource_requirements_list ( 10 )
594 #define reg_qword                      ( 11 )  // 64-bit number
595 #define reg_qword_little_endian        ( 11 )  // 64-bit number (same as reg_qword)
596 
597 
598 }  //namespace winapi {
599 }  //namespace interprocess  {
600 }  //namespace boost  {
601 
602 
603 namespace boost {
604 namespace interprocess {
605 namespace winapi {
606 
get_last_error()607 inline unsigned long get_last_error()
608 {  return GetLastError();  }
609 
set_last_error(unsigned long err)610 inline void set_last_error(unsigned long err)
611 {  return SetLastError(err);  }
612 
format_message(unsigned long dwFlags,const void * lpSource,unsigned long dwMessageId,unsigned long dwLanguageId,char * lpBuffer,unsigned long nSize,std::va_list * Arguments)613 inline unsigned long format_message
614    (unsigned long dwFlags, const void *lpSource,
615     unsigned long dwMessageId, unsigned long dwLanguageId,
616     char *lpBuffer, unsigned long nSize, std::va_list *Arguments)
617 {
618    return FormatMessageA
619       (dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments);
620 }
621 
622 //And now, wrapper functions
local_free(void * hmem)623 inline void * local_free(void *hmem)
624 {  return LocalFree(hmem); }
625 
make_lang_id(unsigned long p,unsigned long s)626 inline unsigned long make_lang_id(unsigned long p, unsigned long s)
627 {  return ((((unsigned short)(s)) << 10) | (unsigned short)(p));   }
628 
sched_yield()629 inline void sched_yield()
630 {
631    if(!SwitchToThread()){
632       Sleep(0);
633    }
634 }
635 
sleep_tick()636 inline void sleep_tick()
637 {  Sleep(1);   }
638 
sleep(unsigned long ms)639 inline void sleep(unsigned long ms)
640 {  Sleep(ms);  }
641 
get_current_thread_id()642 inline unsigned long get_current_thread_id()
643 {  return GetCurrentThreadId();  }
644 
get_process_times(void * hProcess,interprocess_filetime * lpCreationTime,interprocess_filetime * lpExitTime,interprocess_filetime * lpKernelTime,interprocess_filetime * lpUserTime)645 inline bool get_process_times
646    ( void *hProcess, interprocess_filetime* lpCreationTime
647    , interprocess_filetime *lpExitTime, interprocess_filetime *lpKernelTime
648    , interprocess_filetime *lpUserTime )
649 {  return 0 != GetProcessTimes(hProcess, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime); }
650 
get_current_process_id()651 inline unsigned long get_current_process_id()
652 {  return GetCurrentProcessId();  }
653 
close_handle(void * handle)654 inline unsigned int close_handle(void* handle)
655 {  return CloseHandle(handle);   }
656 
find_first_file(const char * lpFileName,win32_find_data * lpFindFileData)657 inline void * find_first_file(const char *lpFileName, win32_find_data *lpFindFileData)
658 {  return FindFirstFileA(lpFileName, lpFindFileData);   }
659 
find_next_file(void * hFindFile,win32_find_data * lpFindFileData)660 inline bool find_next_file(void *hFindFile, win32_find_data *lpFindFileData)
661 {  return FindNextFileA(hFindFile, lpFindFileData) != 0;   }
662 
find_close(void * handle)663 inline bool find_close(void *handle)
664 {  return FindClose(handle) != 0;   }
665 
duplicate_current_process_handle(void * hSourceHandle,void ** lpTargetHandle)666 inline bool duplicate_current_process_handle
667    (void *hSourceHandle, void **lpTargetHandle)
668 {
669    return 0 != DuplicateHandle
670       ( GetCurrentProcess(),  hSourceHandle,    GetCurrentProcess()
671       , lpTargetHandle,       0,                0
672       , duplicate_same_access);
673 }
674 
get_file_type(void * hFile)675 inline unsigned long get_file_type(void *hFile)
676 {
677    return GetFileType(hFile);
678 }
679 
680 /*
681 inline void get_system_time_as_file_time(interprocess_filetime *filetime)
682 {  GetSystemTimeAsFileTime(filetime);  }
683 
684 inline bool file_time_to_local_file_time
685    (const interprocess_filetime *in, const interprocess_filetime *out)
686 {  return 0 != FileTimeToLocalFileTime(in, out);  }
687 */
open_or_create_mutex(const char * name,bool initial_owner,interprocess_security_attributes * attr)688 inline void *open_or_create_mutex(const char *name, bool initial_owner, interprocess_security_attributes *attr)
689 {  return CreateMutexA(attr, (int)initial_owner, name);  }
690 
wait_for_single_object(void * handle,unsigned long time)691 inline unsigned long wait_for_single_object(void *handle, unsigned long time)
692 {  return WaitForSingleObject(handle, time); }
693 
release_mutex(void * handle)694 inline int release_mutex(void *handle)
695 {  return ReleaseMutex(handle);  }
696 
unmap_view_of_file(void * address)697 inline int unmap_view_of_file(void *address)
698 {  return UnmapViewOfFile(address); }
699 
open_or_create_semaphore(const char * name,long initial_count,long maximum_count,interprocess_security_attributes * attr)700 inline void *open_or_create_semaphore(const char *name, long initial_count, long maximum_count, interprocess_security_attributes *attr)
701 {  return CreateSemaphoreA(attr, initial_count, maximum_count, name);  }
702 
open_semaphore(const char * name)703 inline void *open_semaphore(const char *name)
704 {  return OpenSemaphoreA(semaphore_all_access, 0, name);  }
705 
release_semaphore(void * handle,long release_count,long * prev_count)706 inline int release_semaphore(void *handle, long release_count, long *prev_count)
707 {  return ReleaseSemaphore(handle, release_count, prev_count); }
708 
709 class interprocess_all_access_security
710 {
711    interprocess_security_attributes sa;
712    interprocess_security_descriptor sd;
713    bool initialized;
714 
715    public:
interprocess_all_access_security()716    interprocess_all_access_security()
717       : initialized(false)
718    {
719       if(!boost::winapi::InitializeSecurityDescriptor(&sd, security_descriptor_revision))
720          return;
721       if(!boost::winapi::SetSecurityDescriptorDacl(&sd, true, 0, false))
722          return;
723       sa.lpSecurityDescriptor = &sd;
724       sa.nLength = sizeof(interprocess_security_attributes);
725       sa.bInheritHandle = false;
726       initialized = true;
727    }
728 
get_attributes()729    interprocess_security_attributes *get_attributes()
730    {  return &sa; }
731 };
732 
create_file_mapping(void * handle,unsigned long access,::boost::ulong_long_type file_offset,const char * name,interprocess_security_attributes * psec)733 inline void * create_file_mapping (void * handle, unsigned long access, ::boost::ulong_long_type file_offset, const char * name, interprocess_security_attributes *psec)
734 {
735    const boost::winapi::DWORD_ high_size(file_offset >> 32), low_size((boost::winapi::DWORD_)file_offset);
736    return CreateFileMappingA (handle, psec, access, high_size, low_size, name);
737 }
738 
open_file_mapping(unsigned long access,const char * name)739 inline void * open_file_mapping (unsigned long access, const char *name)
740 {  return OpenFileMappingA (access, 0, name);   }
741 
map_view_of_file_ex(void * handle,unsigned long file_access,::boost::ulong_long_type offset,std::size_t numbytes,void * base_addr)742 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)
743 {
744    const unsigned long offset_low  = (unsigned long)(offset & ((::boost::ulong_long_type)0xFFFFFFFF));
745    const unsigned long offset_high = offset >> 32;
746    return MapViewOfFileEx(handle, file_access, offset_high, offset_low, numbytes, base_addr);
747 }
748 
create_file(const char * name,unsigned long access,unsigned long creation_flags,unsigned long attributes,interprocess_security_attributes * psec)749 inline void *create_file(const char *name, unsigned long access, unsigned long creation_flags, unsigned long attributes, interprocess_security_attributes *psec)
750 {
751    for (unsigned int attempt(0); attempt < error_sharing_violation_tries; ++attempt){
752       void * const handle = CreateFileA(name, access,
753                                         file_share_read | file_share_write | file_share_delete,
754                                         psec, creation_flags, attributes, 0);
755       bool const invalid(invalid_handle_value == handle);
756       if (!invalid){
757          return handle;
758       }
759       if (error_sharing_violation != get_last_error()){
760          return handle;
761       }
762       sleep(error_sharing_violation_sleep_ms);
763    }
764    return invalid_handle_value;
765 }
766 
get_system_info(interprocess_system_info * info)767 inline void get_system_info(interprocess_system_info *info)
768 {  boost::ipwinapiext::GetSystemInfo(info); }
769 
flush_view_of_file(void * base_addr,std::size_t numbytes)770 inline bool flush_view_of_file(void *base_addr, std::size_t numbytes)
771 {  return 0 != boost::winapi::FlushViewOfFile(base_addr, numbytes); }
772 
virtual_unlock(void * base_addr,std::size_t numbytes)773 inline bool virtual_unlock(void *base_addr, std::size_t numbytes)
774 {  return 0 != boost::ipwinapiext::VirtualUnlock(base_addr, numbytes); }
775 
virtual_protect(void * base_addr,std::size_t numbytes,unsigned long flNewProtect,unsigned long & lpflOldProtect)776 inline bool virtual_protect(void *base_addr, std::size_t numbytes, unsigned long flNewProtect, unsigned long &lpflOldProtect)
777 {  return 0 != boost::ipwinapiext::VirtualProtect(base_addr, numbytes, flNewProtect, &lpflOldProtect); }
778 
flush_file_buffers(void * handle)779 inline bool flush_file_buffers(void *handle)
780 {  return 0 != boost::ipwinapiext::FlushFileBuffers(handle); }
781 
get_file_size(void * handle,__int64 & size)782 inline bool get_file_size(void *handle, __int64 &size)
783 {  return 0 != boost::winapi::GetFileSizeEx(handle, (boost::winapi::LARGE_INTEGER_*)&size);  }
784 
create_directory(const char * name)785 inline bool create_directory(const char *name)
786 {
787    interprocess_all_access_security sec;
788    return 0 != boost::winapi::CreateDirectoryA(name, sec.get_attributes());
789 }
790 
remove_directory(const char * lpPathName)791 inline bool remove_directory(const char *lpPathName)
792 {  return 0 != boost::winapi::RemoveDirectoryA(lpPathName);   }
793 
get_temp_path(unsigned long length,char * buffer)794 inline unsigned long get_temp_path(unsigned long length, char *buffer)
795 {  return boost::winapi::GetTempPathA(length, buffer);   }
796 
set_end_of_file(void * handle)797 inline int set_end_of_file(void *handle)
798 {  return 0 != boost::winapi::SetEndOfFile(handle);   }
799 
set_file_pointer(void * handle,__int64 distance,__int64 * new_file_pointer,unsigned long move_method)800 inline bool set_file_pointer(void *handle, __int64 distance, __int64 *new_file_pointer, unsigned long move_method)
801 {
802    long highPart = distance >> 32u;
803    boost::winapi::DWORD_ r = boost::winapi::SetFilePointer(handle, (unsigned long)distance, &highPart, move_method);
804    bool br = r != boost::winapi::INVALID_SET_FILE_POINTER_ || boost::winapi::GetLastError() != 0;
805    if (br && new_file_pointer){
806       *new_file_pointer = (unsigned __int64)r + ((__int64)highPart << 32);
807    }
808 
809    return br;
810 }
811 
lock_file_ex(void * hnd,unsigned long flags,unsigned long reserved,unsigned long size_low,unsigned long size_high,interprocess_overlapped * overlapped)812 inline bool lock_file_ex(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
813 {  return 0 != boost::winapi::LockFileEx(hnd, flags, reserved, size_low, size_high, overlapped); }
814 
unlock_file_ex(void * hnd,unsigned long reserved,unsigned long size_low,unsigned long size_high,interprocess_overlapped * overlapped)815 inline bool unlock_file_ex(void *hnd, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
816 {  return 0 != boost::winapi::UnlockFileEx(hnd, reserved, size_low, size_high, overlapped);  }
817 
write_file(void * hnd,const void * buffer,unsigned long bytes_to_write,unsigned long * bytes_written,interprocess_overlapped * overlapped)818 inline bool write_file(void *hnd, const void *buffer, unsigned long bytes_to_write, unsigned long *bytes_written, interprocess_overlapped* overlapped)
819 {  return 0 != boost::winapi::WriteFile(hnd, buffer, bytes_to_write, bytes_written, overlapped);  }
820 
read_file(void * hnd,void * buffer,unsigned long bytes_to_read,unsigned long * bytes_read,interprocess_overlapped * overlapped)821 inline bool read_file(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped)
822 {  return 0 != boost::winapi::ReadFile(hnd, buffer, bytes_to_read, bytes_read, overlapped);  }
823 
get_file_information_by_handle(void * hnd,interprocess_by_handle_file_information * info)824 inline bool get_file_information_by_handle(void *hnd, interprocess_by_handle_file_information *info)
825 {  return 0 != boost::winapi::GetFileInformationByHandle(hnd, info);  }
826 
interlocked_increment(long volatile * addr)827 inline long interlocked_increment(long volatile *addr)
828 {  return BOOST_INTERLOCKED_INCREMENT(const_cast<long*>(addr));  }
829 
interlocked_decrement(long volatile * addr)830 inline long interlocked_decrement(long volatile *addr)
831 {  return BOOST_INTERLOCKED_DECREMENT(const_cast<long*>(addr));  }
832 
interlocked_compare_exchange(long volatile * addr,long val1,long val2)833 inline long interlocked_compare_exchange(long volatile *addr, long val1, long val2)
834 {  return BOOST_INTERLOCKED_COMPARE_EXCHANGE(const_cast<long*>(addr), val1, val2);  }
835 
interlocked_exchange_add(long volatile * addend,long value)836 inline long interlocked_exchange_add(long volatile* addend, long value)
837 {  return BOOST_INTERLOCKED_EXCHANGE_ADD(const_cast<long*>(addend), value);  }
838 
interlocked_exchange(long volatile * addend,long value)839 inline long interlocked_exchange(long volatile* addend, long value)
840 {  return BOOST_INTERLOCKED_EXCHANGE(const_cast<long*>(addend), value);  }
841 
842 //Forward functions
load_library(const char * name)843 inline hmodule load_library(const char *name)
844 {  return boost::winapi::LoadLibraryA(name); }
845 
free_library(hmodule module)846 inline bool free_library(hmodule module)
847 {  return 0 != boost::winapi::FreeLibrary(module); }
848 
get_proc_address(hmodule module,const char * name)849 inline farproc_t get_proc_address(hmodule module, const char *name)
850 {  return boost::winapi::GetProcAddress(module, name); }
851 
get_current_process()852 inline void *get_current_process()
853 {  return boost::winapi::GetCurrentProcess();  }
854 
get_module_handle(const char * name)855 inline hmodule get_module_handle(const char *name)
856 {  return boost::winapi::GetModuleHandleA(name); }
857 
reg_open_key_ex(hkey hKey,const char * lpSubKey,unsigned long ulOptions,unsigned long samDesired,hkey * phkResult)858 inline long reg_open_key_ex(hkey hKey, const char *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
859 {  return boost::ipwinapiext::RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
860 
reg_query_value_ex(hkey hKey,const char * lpValueName,unsigned long * lpReserved,unsigned long * lpType,unsigned char * lpData,unsigned long * lpcbData)861 inline long reg_query_value_ex(hkey hKey, const char *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
862 {  return boost::ipwinapiext::RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
863 
reg_close_key(hkey hKey)864 inline long reg_close_key(hkey hKey)
865 {  return boost::ipwinapiext::RegCloseKey(hKey); }
866 
initialize_object_attributes(object_attributes_t * pobject_attr,unicode_string_t * name,unsigned long attr,void * rootdir,void * security_descr)867 inline void initialize_object_attributes
868 ( object_attributes_t *pobject_attr, unicode_string_t *name
869  , unsigned long attr, void *rootdir, void *security_descr)
870 
871 {
872    pobject_attr->Length = sizeof(object_attributes_t);
873    pobject_attr->RootDirectory = rootdir;
874    pobject_attr->Attributes = attr;
875    pobject_attr->ObjectName = name;
876    pobject_attr->SecurityDescriptor = security_descr;
877    pobject_attr->SecurityQualityOfService = 0;
878 }
879 
rtl_init_empty_unicode_string(unicode_string_t * ucStr,wchar_t * buf,unsigned short bufSize)880 inline void rtl_init_empty_unicode_string(unicode_string_t *ucStr, wchar_t *buf, unsigned short bufSize)
881 {
882    ucStr->Buffer = buf;
883    ucStr->Length = 0;
884    ucStr->MaximumLength = bufSize;
885 }
886 
887 //A class that locates and caches loaded DLL function addresses.
888 template<int Dummy>
889 struct function_address_holder
890 {
891    enum  { NtSetInformationFile
892          , NtQuerySystemInformation
893          , NtQueryObject
894          , NtQuerySemaphore
895          , NtQuerySection
896          , NtOpenFile
897          , NtClose
898          , NtQueryTimerResolution
899          , NumFunction
900          };
901    enum { NtDll_dll, Kernel32_dll, NumModule };
902 
903    private:
904    static const char *FunctionNames[NumFunction];
905    static const char *ModuleNames[NumModule];
906    static farproc_t FunctionAddresses[NumFunction];
907    static unsigned int FunctionModules[NumFunction];
908    static volatile long FunctionStates[NumFunction];
909    static hmodule ModuleAddresses[NumModule];
910    static volatile long ModuleStates[NumModule];
911 
get_module_from_idboost::interprocess::winapi::function_address_holder912    static hmodule get_module_from_id(unsigned int id)
913    {
914       BOOST_ASSERT(id < (unsigned int)NumModule);
915       hmodule addr = get_module_handle(ModuleNames[id]);
916       BOOST_ASSERT(addr);
917       return addr;
918    }
919 
get_moduleboost::interprocess::winapi::function_address_holder920    static hmodule get_module(const unsigned int id)
921    {
922       BOOST_ASSERT(id < (unsigned int)NumModule);
923       for(unsigned i = 0; ModuleStates[id] < 2; ++i){
924          if(interlocked_compare_exchange(&ModuleStates[id], 1, 0) == 0){
925             ModuleAddresses[id] = get_module_from_id(id);
926             interlocked_increment(&ModuleStates[id]);
927             break;
928          }
929          else if(i & 1){
930             sched_yield();
931          }
932          else{
933             sleep_tick();
934          }
935       }
936       return ModuleAddresses[id];
937    }
938 
get_address_from_dllboost::interprocess::winapi::function_address_holder939    static farproc_t get_address_from_dll(const unsigned int id)
940    {
941       BOOST_ASSERT(id < (unsigned int)NumFunction);
942       farproc_t addr = get_proc_address(get_module(FunctionModules[id]), FunctionNames[id]);
943       BOOST_ASSERT(addr);
944       return addr;
945    }
946 
947    public:
getboost::interprocess::winapi::function_address_holder948    static farproc_t get(const unsigned int id)
949    {
950       BOOST_ASSERT(id < (unsigned int)NumFunction);
951       for(unsigned i = 0; FunctionStates[id] < 2; ++i){
952          if(interlocked_compare_exchange(&FunctionStates[id], 1, 0) == 0){
953             FunctionAddresses[id] = get_address_from_dll(id);
954             interlocked_increment(&FunctionStates[id]);
955             break;
956          }
957          else if(i & 1){
958             sched_yield();
959          }
960          else{
961             sleep_tick();
962          }
963       }
964       return FunctionAddresses[id];
965    }
966 };
967 
968 template<int Dummy>
969 const char *function_address_holder<Dummy>::FunctionNames[function_address_holder<Dummy>::NumFunction] =
970 {
971    "NtSetInformationFile",
972    "NtQuerySystemInformation",
973    "NtQueryObject",
974    "NtQuerySemaphore",
975    "NtQuerySection",
976    "NtOpenFile",
977    "NtClose",
978    "NtQueryTimerResolution",
979 };
980 
981 template<int Dummy>
982 unsigned int function_address_holder<Dummy>::FunctionModules[function_address_holder<Dummy>::NumFunction] =
983 {
984    NtDll_dll,
985    NtDll_dll,
986    NtDll_dll,
987    NtDll_dll,
988    NtDll_dll,
989    NtDll_dll,
990    NtDll_dll,
991    NtDll_dll,
992 };
993 
994 template<int Dummy>
995 const char *function_address_holder<Dummy>::ModuleNames[function_address_holder<Dummy>::NumModule] =
996 {
997    "ntdll.dll"//, "kernel32.dll"
998 };
999 
1000 
1001 template<int Dummy>
1002 farproc_t function_address_holder<Dummy>::FunctionAddresses[function_address_holder<Dummy>::NumFunction];
1003 
1004 template<int Dummy>
1005 volatile long function_address_holder<Dummy>::FunctionStates[function_address_holder<Dummy>::NumFunction];
1006 
1007 template<int Dummy>
1008 hmodule function_address_holder<Dummy>::ModuleAddresses[function_address_holder<Dummy>::NumModule];
1009 
1010 template<int Dummy>
1011 volatile long function_address_holder<Dummy>::ModuleStates[function_address_holder<Dummy>::NumModule];
1012 
1013 
1014 struct dll_func
1015    : public function_address_holder<0>
1016 {};
1017 
1018 //Complex winapi based functions...
1019 struct library_unloader
1020 {
1021    hmodule lib_;
library_unloaderboost::interprocess::winapi::library_unloader1022    library_unloader(hmodule module) : lib_(module){}
~library_unloaderboost::interprocess::winapi::library_unloader1023    ~library_unloader(){ free_library(lib_);  }
1024 };
1025 
1026 
get_system_time_of_day_information(system_timeofday_information & info)1027 inline bool get_system_time_of_day_information(system_timeofday_information &info)
1028 {
1029    NtQuerySystemInformation_t pNtQuerySystemInformation = reinterpret_cast<NtQuerySystemInformation_t>
1030          (dll_func::get(dll_func::NtQuerySystemInformation));
1031    unsigned long res;
1032    long status = pNtQuerySystemInformation(system_time_of_day_information, &info, sizeof(info), &res);
1033    if(status){
1034       return false;
1035    }
1036    return true;
1037 }
1038 
get_boot_time(unsigned char (& bootstamp)[BootstampLength])1039 inline bool get_boot_time(unsigned char (&bootstamp) [BootstampLength])
1040 {
1041    system_timeofday_information info;
1042    bool ret = get_system_time_of_day_information(info);
1043    if(!ret){
1044       return false;
1045    }
1046    std::memcpy(&bootstamp[0], &info.Reserved1, sizeof(bootstamp));
1047    return true;
1048 }
1049 
get_boot_and_system_time(unsigned char (& bootsystemstamp)[BootAndSystemstampLength])1050 inline bool get_boot_and_system_time(unsigned char (&bootsystemstamp) [BootAndSystemstampLength])
1051 {
1052    system_timeofday_information info;
1053    bool ret = get_system_time_of_day_information(info);
1054    if(!ret){
1055       return false;
1056    }
1057    std::memcpy(&bootsystemstamp[0], &info.Reserved1, sizeof(bootsystemstamp));
1058    return true;
1059 }
1060 
1061 //Writes the hexadecimal value of the buffer, in the wide character string.
1062 //str must be twice length
buffer_to_wide_str(const void * buf,std::size_t length,wchar_t * str)1063 inline void buffer_to_wide_str(const void *buf, std::size_t length, wchar_t *str)
1064 {
1065    const wchar_t Characters [] =
1066       { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7'
1067       , L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
1068    std::size_t char_counter = 0;
1069    const char *chbuf = static_cast<const char *>(buf);
1070    for(std::size_t i = 0; i != length; ++i){
1071       str[char_counter++] = Characters[(chbuf[i]&0xF0)>>4];
1072       str[char_counter++] = Characters[(chbuf[i]&0x0F)];
1073    }
1074 }
1075 
1076 //Writes the hexadecimal value of the buffer, in the narrow character string.
1077 //str must be twice length
buffer_to_narrow_str(const void * buf,std::size_t length,char * str)1078 inline void buffer_to_narrow_str(const void *buf, std::size_t length, char *str)
1079 {
1080    const char Characters [] =
1081       { '0', '1', '2', '3', '4', '5', '6', '7'
1082       , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1083    std::size_t char_counter = 0;
1084    const char *chbuf = static_cast<const char *>(buf);
1085    for(std::size_t i = 0; i != length; ++i){
1086       str[char_counter++] = Characters[(chbuf[i]&0xF0)>>4];
1087       str[char_counter++] = Characters[(chbuf[i]&0x0F)];
1088    }
1089 }
1090 
get_boot_time_str(char * bootstamp_str,std::size_t & s)1091 inline bool get_boot_time_str(char *bootstamp_str, std::size_t &s)
1092    //will write BootstampLength chars
1093 {
1094    if(s < (BootstampLength*2))
1095       return false;
1096    system_timeofday_information info;
1097    bool ret = get_system_time_of_day_information(info);
1098    if(!ret){
1099       return false;
1100    }
1101 
1102    buffer_to_narrow_str(info.Reserved1, BootstampLength, bootstamp_str);
1103    s = BootstampLength*2;
1104    return true;
1105 }
1106 
get_boot_and_system_time_wstr(wchar_t * bootsystemstamp,std::size_t & s)1107 inline bool get_boot_and_system_time_wstr(wchar_t *bootsystemstamp, std::size_t &s)
1108    //will write BootAndSystemstampLength chars
1109 {
1110    if(s < (BootAndSystemstampLength*2))
1111       return false;
1112    system_timeofday_information info;
1113    bool ret = get_system_time_of_day_information(info);
1114    if(!ret){
1115       return false;
1116    }
1117 
1118    buffer_to_wide_str(&info.Reserved1[0], BootAndSystemstampLength, bootsystemstamp);
1119    s = BootAndSystemstampLength*2;
1120    return true;
1121 }
1122 
1123 class handle_closer
1124 {
1125    void *handle_;
1126    handle_closer(const handle_closer &);
1127    handle_closer& operator=(const handle_closer &);
1128    public:
handle_closer(void * handle)1129    explicit handle_closer(void *handle) : handle_(handle){}
~handle_closer()1130    ~handle_closer()
1131    {  close_handle(handle_);  }
1132 };
1133 
1134 union ntquery_mem_t
1135 {
1136    object_name_information_t name;
1137    struct ren_t
1138    {
1139       file_rename_information_t info;
1140       wchar_t buf[1];
1141    } ren;
1142 };
1143 
1144 class nt_query_mem_deleter
1145 {
1146    static const std::size_t rename_offset = offsetof(ntquery_mem_t, ren.info.FileName) -
1147       offsetof(ntquery_mem_t, name.Name.Buffer);
1148    //                                           Timestamp                      process id              atomic count
1149    static const std::size_t rename_suffix =
1150       (SystemTimeOfDayInfoLength + sizeof(unsigned long) + sizeof(boost::winapi::DWORD_))*2;
1151 
1152    public:
nt_query_mem_deleter(std::size_t object_name_information_size)1153    explicit nt_query_mem_deleter(std::size_t object_name_information_size)
1154       : m_size(object_name_information_size + rename_offset + rename_suffix)
1155       , m_buf(new char [m_size])
1156    {}
1157 
~nt_query_mem_deleter()1158    ~nt_query_mem_deleter()
1159    {
1160       delete[]m_buf;
1161    }
1162 
realloc_mem(std::size_t num_bytes)1163    void realloc_mem(std::size_t num_bytes)
1164    {
1165       num_bytes += rename_suffix + rename_offset;
1166       char *buf = m_buf;
1167       m_buf = new char[num_bytes];
1168       delete[]buf;
1169       m_size = num_bytes;
1170    }
1171 
query_mem() const1172    ntquery_mem_t *query_mem() const
1173    {  return static_cast<ntquery_mem_t *>(static_cast<void*>(m_buf));  }
1174 
object_name_information_size() const1175    unsigned long object_name_information_size() const
1176    {
1177       return static_cast<unsigned long>(m_size - rename_offset - SystemTimeOfDayInfoLength*2);
1178    }
1179 
file_rename_information_size() const1180    std::size_t file_rename_information_size() const
1181    {  return static_cast<unsigned long>(m_size);  }
1182 
1183    private:
1184    std::size_t m_size;
1185    char *m_buf;
1186 };
1187 
1188 class c_heap_deleter
1189 {
1190    public:
c_heap_deleter(std::size_t size)1191    explicit c_heap_deleter(std::size_t size)
1192       : m_buf(::malloc(size))
1193    {}
1194 
~c_heap_deleter()1195    ~c_heap_deleter()
1196    {
1197       if(m_buf) ::free(m_buf);
1198    }
1199 
realloc_mem(std::size_t num_bytes)1200    void realloc_mem(std::size_t num_bytes)
1201    {
1202       void *oldBuf = m_buf;
1203       m_buf = ::realloc(m_buf, num_bytes);
1204       if (!m_buf){
1205          free(oldBuf);
1206       }
1207    }
1208 
get() const1209    void *get() const
1210    {  return m_buf;  }
1211 
1212    private:
1213    void *m_buf;
1214 };
1215 
unlink_file(const char * filename)1216 inline bool unlink_file(const char *filename)
1217 {
1218    //Don't try to optimize doing a DeleteFile first
1219    //as there are interactions with permissions and
1220    //in-use files.
1221    //
1222    //if(!delete_file(filename)){
1223    //   (...)
1224    //
1225 
1226    //This functions tries to emulate UNIX unlink semantics in windows.
1227    //
1228    //- Open the file and mark the handle as delete-on-close
1229    //- Rename the file to an arbitrary name based on a random number
1230    //- Close the handle. If there are no file users, it will be deleted.
1231    //  Otherwise it will be used by already connected handles but the
1232    //  file name can't be used to open this file again
1233    try{
1234       NtSetInformationFile_t pNtSetInformationFile =
1235          reinterpret_cast<NtSetInformationFile_t>(dll_func::get(dll_func::NtSetInformationFile));
1236 
1237       NtQueryObject_t pNtQueryObject = reinterpret_cast<NtQueryObject_t>(dll_func::get(dll_func::NtQueryObject));
1238 
1239       //First step: Obtain a handle to the file using Win32 rules. This resolves relative paths
1240       void *fh = create_file(filename, generic_read | delete_access, open_existing, 0, 0);
1241       if(fh == invalid_handle_value){
1242          return false;
1243       }
1244 
1245       handle_closer h_closer(fh);
1246       {
1247          //Obtain name length
1248          unsigned long size;
1249          const std::size_t initial_string_mem = 512u;
1250 
1251          nt_query_mem_deleter nt_query_mem(sizeof(ntquery_mem_t)+initial_string_mem);
1252          //Obtain file name with guessed length
1253          if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
1254             //Obtain file name with exact length buffer
1255             nt_query_mem.realloc_mem(size);
1256             if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
1257                return false;
1258             }
1259          }
1260          ntquery_mem_t *pmem = nt_query_mem.query_mem();
1261          file_rename_information_t *pfri = &pmem->ren.info;
1262          const std::size_t RenMaxNumChars =
1263             (((char*)(pmem) + nt_query_mem.file_rename_information_size()) - (char*)&pmem->ren.info.FileName[0])/sizeof(wchar_t);
1264 
1265          //Copy filename to the rename member
1266          std::memmove(pmem->ren.info.FileName, pmem->name.Name.Buffer, pmem->name.Name.Length);
1267          std::size_t filename_string_length = pmem->name.Name.Length/sizeof(wchar_t);
1268 
1269          //Search '\\' character to replace from it
1270          for(std::size_t i = filename_string_length; i != 0; --filename_string_length){
1271             if(pmem->ren.info.FileName[--i] == L'\\')
1272                break;
1273          }
1274 
1275          //Add random number
1276          std::size_t s = RenMaxNumChars - filename_string_length;
1277          if(!get_boot_and_system_time_wstr(&pfri->FileName[filename_string_length], s)){
1278             return false;
1279          }
1280          filename_string_length += s;
1281 
1282          //Sometimes the precission of the timestamp is not enough and we need to add another random number.
1283          //The process id (to exclude concurrent processes) and an atomic count (to exclude concurrent threads).
1284          //should be enough
1285          const unsigned long pid = get_current_process_id();
1286          buffer_to_wide_str(&pid, sizeof(pid), &pfri->FileName[filename_string_length]);
1287          filename_string_length += sizeof(pid)*2;
1288 
1289          static volatile boost::uint32_t u32_count = 0;
1290          interlocked_decrement(reinterpret_cast<volatile long*>(&u32_count));
1291          buffer_to_wide_str(const_cast<const boost::uint32_t *>(&u32_count), sizeof(boost::uint32_t), &pfri->FileName[filename_string_length]);
1292          filename_string_length += sizeof(boost::uint32_t)*2;
1293 
1294          //Fill rename information (FileNameLength is in bytes)
1295          pfri->FileNameLength = static_cast<unsigned long>(sizeof(wchar_t)*(filename_string_length));
1296          pfri->Replace = 1;
1297          pfri->RootDir = 0;
1298 
1299          //Cange the name of the in-use file...
1300          io_status_block_t io;
1301          if(0 != pNtSetInformationFile(fh, &io, pfri, nt_query_mem.file_rename_information_size(), file_rename_information)){
1302             return false;
1303          }
1304       }
1305       //...and mark it as delete-on-close
1306       {
1307          //Don't use pNtSetInformationFile with file_disposition_information as it can return STATUS_CANNOT_DELETE
1308          //if the file is still mapped. Reopen it with NtOpenFile and file_delete_on_close
1309          NtOpenFile_t pNtOpenFile = reinterpret_cast<NtOpenFile_t>(dll_func::get(dll_func::NtOpenFile));
1310          NtClose_t pNtClose = reinterpret_cast<NtClose_t>(dll_func::get(dll_func::NtClose));
1311          const wchar_t empty_str [] = L"";
1312          unicode_string_t ustring = { sizeof(empty_str) - sizeof (wchar_t)   //length in bytes without null
1313                                     , sizeof(empty_str)   //total size in bytes of memory allocated for Buffer.
1314                                     , const_cast<wchar_t*>(empty_str)
1315                                     };
1316          object_attributes_t object_attr;
1317          initialize_object_attributes(&object_attr, &ustring, 0, fh, 0);
1318          void* fh2 = 0;
1319          io_status_block_t io;
1320          pNtOpenFile( &fh2, delete_flag, &object_attr, &io
1321                     , file_share_read | file_share_write | file_share_delete, file_delete_on_close);
1322          pNtClose(fh2);
1323          //Even if NtOpenFile fails, the file was renamed and the original no longer exists, so return a success status
1324          return true;
1325       }
1326    }
1327    catch(...){
1328       return false;
1329    }
1330    return true;
1331 }
1332 
1333 struct reg_closer
1334 {
1335    hkey key_;
reg_closerboost::interprocess::winapi::reg_closer1336    reg_closer(hkey key) : key_(key){}
~reg_closerboost::interprocess::winapi::reg_closer1337    ~reg_closer(){ reg_close_key(key_);  }
1338 };
1339 
get_registry_value_buffer(hkey key_type,const char * subkey_name,const char * value_name,void * buf,std::size_t & buflen)1340 inline bool get_registry_value_buffer(hkey key_type, const char *subkey_name, const char *value_name, void *buf, std::size_t &buflen)
1341 {
1342    bool bret = false;
1343    hkey key;
1344    if (reg_open_key_ex( key_type
1345                      , subkey_name
1346                      , 0
1347                      , key_query_value
1348                      , &key) == 0){
1349       reg_closer key_closer(key);
1350 
1351       //Obtain the value
1352       unsigned long size = buflen;
1353       unsigned long type;
1354       buflen = 0;
1355       bret = 0 == reg_query_value_ex( key, value_name, 0, &type, (unsigned char*)buf, &size);
1356       if(bret)
1357          buflen = (std::size_t)size;
1358    }
1359    return bret;
1360 }
1361 
get_registry_value_string(hkey key_type,const char * subkey_name,const char * value_name,std::string & s)1362 inline bool get_registry_value_string(hkey key_type, const char *subkey_name, const char *value_name, std::string &s)
1363 {
1364    bool bret = false;
1365    s.clear();
1366    hkey key;
1367    if (reg_open_key_ex( key_type
1368                      , subkey_name
1369                      , 0
1370                      , key_query_value
1371                      , &key) == 0){
1372       reg_closer key_closer(key);
1373 
1374       //Obtain the value
1375       unsigned long size;
1376       unsigned long type;
1377       long err = reg_query_value_ex( key, value_name, 0, &type, 0, &size);
1378       if((reg_sz == type || reg_expand_sz == type) && !err){
1379          //Size includes terminating NULL
1380          s.resize(size);
1381          err = reg_query_value_ex( key, value_name, 0, &type, (unsigned char*)(&s[0]), &size);
1382          if(!err){
1383             s.erase(s.end()-1);
1384             bret = true;
1385          }
1386          (void)err;
1387       }
1388    }
1389    return bret;
1390 }
1391 
get_shared_documents_folder(std::string & s)1392 inline void get_shared_documents_folder(std::string &s)
1393 {
1394    get_registry_value_string( hkey_local_machine
1395                             , "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
1396                             , "Common AppData"
1397                             , s);
1398 }
1399 
get_registry_value(const char * folder,const char * value_key,std::vector<unsigned char> & s)1400 inline void get_registry_value(const char *folder, const char *value_key, std::vector<unsigned char> &s)
1401 {
1402    s.clear();
1403    hkey key;
1404    if (reg_open_key_ex( hkey_local_machine
1405                      , folder
1406                      , 0
1407                      , key_query_value
1408                      , &key) == 0){
1409       reg_closer key_closer(key);
1410 
1411       //Obtain the value
1412       unsigned long size;
1413       unsigned long type;
1414       const char *const reg_value = value_key;
1415       //long err = (*pRegQueryValue)( key, reg_value, 0, &type, 0, &size);
1416       long err = reg_query_value_ex( key, reg_value, 0, &type, 0, &size);
1417       if(!err){
1418          //Size includes terminating NULL
1419          s.resize(size);
1420          //err = (*pRegQueryValue)( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
1421          err = reg_query_value_ex( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
1422          if(!err)
1423             s.erase(s.end()-1);
1424          (void)err;
1425       }
1426    }
1427 }
1428 
is_directory(const char * path)1429 inline bool is_directory(const char *path)
1430 {
1431    unsigned long attrib = GetFileAttributesA(path);
1432 
1433    return (attrib != invalid_file_attributes &&
1434            (attrib & file_attribute_directory));
1435 }
1436 
get_file_mapping_size(void * file_mapping_hnd,__int64 & size)1437 inline bool get_file_mapping_size(void *file_mapping_hnd, __int64 &size)
1438 {
1439    NtQuerySection_t pNtQuerySection =
1440       reinterpret_cast<NtQuerySection_t>(dll_func::get(dll_func::NtQuerySection));
1441    //Obtain file name
1442    interprocess_section_basic_information info;
1443    unsigned long ntstatus =
1444       pNtQuerySection(file_mapping_hnd, section_basic_information, &info, sizeof(info), 0);
1445    size = info.section_size;
1446    return !ntstatus;
1447 }
1448 
get_semaphore_info(void * handle,long & count,long & limit)1449 inline bool get_semaphore_info(void *handle, long &count, long &limit)
1450 {
1451    winapi::interprocess_semaphore_basic_information info;
1452    winapi::NtQuerySemaphore_t pNtQuerySemaphore =
1453          reinterpret_cast<winapi::NtQuerySemaphore_t>(dll_func::get(winapi::dll_func::NtQuerySemaphore));
1454    unsigned int ret_len;
1455    long status = pNtQuerySemaphore(handle, winapi::semaphore_basic_information, &info, sizeof(info), &ret_len);
1456    count = info.count;
1457    limit = info.limit;
1458    return !status;
1459 }
1460 
query_timer_resolution(unsigned long * lowres,unsigned long * highres,unsigned long * curres)1461 inline bool query_timer_resolution(unsigned long *lowres, unsigned long *highres, unsigned long *curres)
1462 {
1463    winapi::NtQueryTimerResolution_t pNtQueryTimerResolution =
1464          reinterpret_cast<winapi::NtQueryTimerResolution_t>(dll_func::get(winapi::dll_func::NtQueryTimerResolution));
1465    return !pNtQueryTimerResolution(lowres, highres, curres);
1466 }
1467 
query_performance_counter(__int64 * lpPerformanceCount)1468 inline bool query_performance_counter(__int64 *lpPerformanceCount)
1469 {
1470    return 0 != boost::winapi::QueryPerformanceCounter(reinterpret_cast<boost::winapi::LARGE_INTEGER_*>(lpPerformanceCount));
1471 }
1472 
query_performance_frequency(__int64 * lpFrequency)1473 inline bool query_performance_frequency(__int64 *lpFrequency)
1474 {
1475    return 0 != boost::winapi::QueryPerformanceFrequency(reinterpret_cast<boost::winapi::LARGE_INTEGER_*>(lpFrequency));
1476 }
1477 
get_tick_count()1478 inline unsigned long get_tick_count()
1479 {  return GetTickCount();  }
1480 
1481 
1482 
1483 
1484 #if defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED)
1485 
1486 
get_last_bootup_time(std::string & stamp)1487 inline bool get_last_bootup_time(std::string &stamp)
1488 {
1489    unsigned dword_val = 0;
1490    std::size_t dword_size = sizeof(dword_val);
1491    bool b_ret = get_registry_value_buffer( hkey_local_machine
1492       , "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management\\PrefetchParameters"
1493       , "BootId", &dword_val, dword_size);
1494    if (b_ret)
1495    {
1496       char dword_str[sizeof(dword_val)*2u+1];
1497       buffer_to_narrow_str(&dword_val, dword_size, dword_str);
1498       dword_str[sizeof(dword_val)*2] = '\0';
1499       stamp = dword_str;
1500 
1501       b_ret = get_registry_value_buffer( hkey_local_machine
1502          , "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Power"
1503          , "HybridBootAnimationTime", &dword_val, dword_size);
1504       //Old Windows versions have no HybridBootAnimationTime
1505       if(b_ret)
1506       {
1507          buffer_to_narrow_str(&dword_val, dword_size, dword_str);
1508          dword_str[sizeof(dword_val)*2] = '\0';
1509          stamp += "_";
1510          stamp += dword_str;
1511       }
1512       b_ret = true;
1513    }
1514    return b_ret;
1515 }
1516 
1517 #elif defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED)
1518 
1519 static const unsigned long eventlog_sequential_read = 0x0001;
1520 static const unsigned long eventlog_backwards_read  = 0x0008;
1521 
1522 struct interprocess_eventlogrecord
1523 {
1524     unsigned long  Length;        // Length of full record
1525     unsigned long  Reserved;      // Used by the service
1526     unsigned long  RecordNumber;  // Absolute record number
1527     unsigned long  TimeGenerated; // Seconds since 1-1-1970
1528     unsigned long  TimeWritten;   // Seconds since 1-1-1970
1529     unsigned long  EventID;
1530     unsigned short EventType;
1531     unsigned short NumStrings;
1532     unsigned short EventCategory;
1533     unsigned short ReservedFlags; // For use with paired events (auditing)
1534     unsigned long  ClosingRecordNumber; // For use with paired events (auditing)
1535     unsigned long  StringOffset;  // Offset from beginning of record
1536     unsigned long  UserSidLength;
1537     unsigned long  UserSidOffset;
1538     unsigned long  DataLength;
1539     unsigned long  DataOffset;    // Offset from beginning of record
1540     //
1541     // Then follow:
1542     //
1543     // wchar_t SourceName[]
1544     // wchar_t Computername[]
1545     // SID   UserSid
1546     // wchar_t Strings[]
1547     // BYTE  Data[]
1548     // CHAR  Pad[]
1549     // unsigned long Length;
1550     //
1551 };
1552 
1553 class eventlog_handle_closer
1554 {
1555    void *handle_;
1556    eventlog_handle_closer(const handle_closer &);
1557    eventlog_handle_closer& operator=(const eventlog_handle_closer &);
1558    public:
eventlog_handle_closer(void * handle)1559    explicit eventlog_handle_closer(void *handle) : handle_(handle){}
~eventlog_handle_closer()1560    ~eventlog_handle_closer()
1561    {  CloseEventLog(handle_);  }
1562 };
1563 
1564 // Loop through the buffer and obtain the contents of the
1565 // 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)1566 inline bool find_record_in_buffer( const void* pBuffer, unsigned long dwBytesRead, const char *provider_name
1567                                  , unsigned int id_to_find, interprocess_eventlogrecord *&pevent_log_record)
1568 {
1569    const unsigned char * pRecord = static_cast<const unsigned char*>(pBuffer);
1570    const unsigned char * pEndOfRecords = pRecord + dwBytesRead;
1571 
1572    while (pRecord < pEndOfRecords){
1573       interprocess_eventlogrecord *pTypedRecord = (interprocess_eventlogrecord*)pRecord;
1574       // Check provider, written at the end of the fixed-part of the record
1575       if (0 == std::strcmp(provider_name, (char*)(pRecord + sizeof(interprocess_eventlogrecord))))
1576       {
1577          // Check event id
1578          if(id_to_find == (pTypedRecord->EventID & 0xFFFF)){
1579             pevent_log_record = pTypedRecord;
1580             return true;
1581          }
1582       }
1583 
1584       pRecord += pTypedRecord->Length;
1585    }
1586    pevent_log_record = 0;
1587    return false;
1588 }
1589 
1590 //Obtains the bootup time from the System Event Log,
1591 //event ID == 6005 (event log started).
1592 //Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/bb427356.aspx
get_last_bootup_time(std::string & stamp)1593 inline bool get_last_bootup_time(std::string &stamp)
1594 {
1595    const char *source_name = "System";
1596    const char *provider_name = "EventLog";
1597    const unsigned short event_id = 6005u;
1598 
1599    unsigned long status = 0;
1600    unsigned long dwBytesToRead = 0;
1601    unsigned long dwBytesRead = 0;
1602    unsigned long dwMinimumBytesToRead = 0;
1603 
1604    // The source name (provider) must exist as a subkey of Application.
1605    void *hEventLog = OpenEventLogA(0, source_name);
1606    if (hEventLog){
1607       eventlog_handle_closer hnd_closer(hEventLog); (void)hnd_closer;
1608       // Allocate an initial block of memory used to read event records. The number
1609       // of records read into the buffer will vary depending on the size of each event.
1610       // The size of each event will vary based on the size of the user-defined
1611       // data included with each event, the number and length of insertion
1612       // strings, and other data appended to the end of the event record.
1613       dwBytesToRead = max_record_buffer_size;
1614       c_heap_deleter heap_deleter(dwBytesToRead);
1615 
1616       // Read blocks of records until you reach the end of the log or an
1617       // error occurs. The records are read from newest to oldest. If the buffer
1618       // is not big enough to hold a complete event record, reallocate the buffer.
1619       if (heap_deleter.get() != 0){
1620          while (0 == status){
1621             if (!ReadEventLogA(hEventLog,
1622                   eventlog_sequential_read | eventlog_backwards_read,
1623                   0,
1624                   heap_deleter.get(),
1625                   dwBytesToRead,
1626                   &dwBytesRead,
1627                   &dwMinimumBytesToRead)) {
1628                status = get_last_error();
1629                if (error_insufficient_buffer == status) {
1630                   status = 0;
1631                   dwBytesToRead = dwMinimumBytesToRead;
1632                   heap_deleter.realloc_mem(dwMinimumBytesToRead);
1633                   if (!heap_deleter.get()){
1634                      return false;
1635                   }
1636                }
1637                else{  //Not found or EOF
1638                   return false;
1639                }
1640             }
1641             else
1642             {
1643                interprocess_eventlogrecord *pTypedRecord;
1644                // Print the contents of each record in the buffer.
1645                if(find_record_in_buffer(heap_deleter.get(), dwBytesRead, provider_name, event_id, pTypedRecord)){
1646                   char stamp_str[sizeof(unsigned long)*3+1];
1647                   std::sprintf(&stamp_str[0], "%u", ((unsigned int)pTypedRecord->TimeGenerated));
1648                   stamp = stamp_str;
1649                   break;
1650                }
1651             }
1652          }
1653       }
1654    }
1655    return true;
1656 }
1657 
1658 #endif   //BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
1659 
1660 
1661 }  //namespace winapi
1662 }  //namespace interprocess
1663 }  //namespace boost
1664 
1665 #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
1666 #  pragma GCC diagnostic pop
1667 #endif
1668 
1669 #include <boost/interprocess/detail/config_end.hpp>
1670 
1671 #endif //#ifdef BOOST_INTERPROCESS_WIN32_API_HPP
1672