]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/detail/win32_api.hpp
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / boost / boost / interprocess / detail / win32_api.hpp
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 long count; // current semaphore count
125 unsigned long 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 typedef boost::winapi::DWORD_ (__stdcall *LPTHREAD_START_ROUTINE_)
308 (boost::winapi::LPVOID_ lpThreadParameter);
309
310
311 //#ifndef BOOST_USE_WINDOWS_H
312 //typedef boost::winapi::LARGE_INTEGER_ LARGE_INTEGER_EXT;
313 //#else
314 //typedef LARGE_INTEGER LARGE_INTEGER_EXT;
315 //#endif
316
317 }} //namespace boost::ipwinapiext
318
319 #ifndef BOOST_USE_WINDOWS_H
320
321 extern "C" {
322
323 //Error handling
324 BOOST_SYMBOL_IMPORT BOOST_WINAPI_DETAIL_VOID BOOST_WINAPI_WINAPI_CC SetLastError(boost::winapi::DWORD_ dwErrCode);
325
326 //File management
327 BOOST_SYMBOL_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC GetFileType(boost::winapi::HANDLE_ hTemplateFile);
328 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC FlushFileBuffers(boost::winapi::HANDLE_ hFile);
329
330 //threading
331
332 BOOST_SYMBOL_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC CreateThread
333 ( ::_SECURITY_ATTRIBUTES* lpThreadAttributes
334 , boost::winapi::SIZE_T_ dwStackSize
335 , boost::ipwinapiext::LPTHREAD_START_ROUTINE_ lpStartAddress
336 , boost::winapi::LPVOID_ lpParameter
337 , boost::winapi::DWORD_ dwCreationFlags
338 , boost::winapi::LPDWORD_ lpThreadId
339 );
340
341 //Virtual Memory
342 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualLock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize);
343 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualUnlock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize);
344 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualProtect( boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize
345 , boost::winapi::DWORD_ flNewProtect, boost::winapi::PDWORD_ lpflOldProtect);
346 //registry.hpp
347 BOOST_WINAPI_DETAIL_DECLARE_HANDLE(HKEY);
348
349
350 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegOpenKeyExA
351 (::HKEY hKey, const char *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, ::HKEY *phkResult);
352 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegOpenKeyExW
353 (::HKEY hKey, const wchar_t *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, ::HKEY *phkResult);
354 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegQueryValueExA
355 (::HKEY hKey, const char *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData);
356 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegQueryValueExW
357 (::HKEY hKey, const wchar_t *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData);
358 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegCloseKey(::HKEY hKey);
359
360
361 //Event Log
362 BOOST_SYMBOL_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC OpenEventLogA(const char* lpUNCServerName, const char* lpSourceName);
363 BOOST_SYMBOL_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC OpenEventLogW(const wchar_t* lpUNCServerName, const wchar_t* lpSourceName);
364 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC CloseEventLog(boost::winapi::HANDLE_ hEventLog);
365 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC ReadEventLogA
366 ( boost::winapi::HANDLE_ hEventLog, boost::winapi::DWORD_ dwReadFlags, boost::winapi::DWORD_ dwRecordOffset, void* lpBuffer
367 , boost::winapi::DWORD_ nNumberOfBytesToRead, boost::winapi::DWORD_ *pnBytesRead, boost::winapi::DWORD_ *pnMinNumberOfBytesNeeded);
368 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC ReadEventLogW
369 ( boost::winapi::HANDLE_ hEventLog, boost::winapi::DWORD_ dwReadFlags, boost::winapi::DWORD_ dwRecordOffset, void* lpBuffer
370 , boost::winapi::DWORD_ nNumberOfBytesToRead, boost::winapi::DWORD_ *pnBytesRead, boost::winapi::DWORD_ *pnMinNumberOfBytesNeeded);
371
372 } //extern "C" {
373
374 #endif //#ifndef BOOST_USE_WINDOWS_H
375
376 namespace boost {
377 namespace ipwinapiext {
378
379 typedef ::HKEY HKEY_;
380
381 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
382
383 //Threads
384 BOOST_FORCEINLINE boost::winapi::HANDLE_ CreateThread
385 ( boost::winapi::SECURITY_ATTRIBUTES_* lpThreadAttributes
386 , boost::winapi::SIZE_T_ dwStackSize
387 , boost::ipwinapiext::LPTHREAD_START_ROUTINE_ lpStartAddress
388 , boost::winapi::LPVOID_ lpParameter
389 , boost::winapi::DWORD_ dwCreationFlags
390 , boost::winapi::LPDWORD_ lpThreadId
391 )
392 {
393 return ::CreateThread( reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpThreadAttributes)
394 , dwStackSize, lpStartAddress
395 , lpParameter, dwCreationFlags, lpThreadId);
396 }
397
398 //Error handling
399 BOOST_FORCEINLINE BOOST_WINAPI_DETAIL_VOID SetLastError(boost::winapi::DWORD_ dwErrCode)
400 { ::SetLastError(dwErrCode); }
401
402 //File management
403 BOOST_FORCEINLINE boost::winapi::DWORD_ GetFileType(boost::winapi::HANDLE_ hTemplateFile)
404 { return ::GetFileType(hTemplateFile); }
405
406 BOOST_FORCEINLINE boost::winapi::BOOL_ FlushFileBuffers(boost::winapi::HANDLE_ hFile)
407 { return ::FlushFileBuffers(hFile); }
408
409 //Virtual Memory
410 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualLock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize)
411 { return ::VirtualLock(lpAddress, dwSize); }
412
413 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualUnlock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize)
414 { return ::VirtualUnlock(lpAddress, dwSize); }
415
416 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualProtect( boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize
417 , boost::winapi::DWORD_ flNewProtect, boost::winapi::PDWORD_ lpflOldProtect)
418 { return ::VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect); }
419
420 //registry.hpp
421 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegOpenKeyExA
422 (boost::ipwinapiext::HKEY_ hKey, const char *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, boost::ipwinapiext::HKEY_ *phkResult)
423 {
424 return ::RegOpenKeyExA(reinterpret_cast< ::HKEY >(hKey), lpSubKey, ulOptions, samDesired, reinterpret_cast< ::HKEY* >(phkResult));
425 }
426
427 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegOpenKeyExW
428 (boost::ipwinapiext::HKEY_ hKey, const wchar_t *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, boost::ipwinapiext::HKEY_ *phkResult)
429 {
430 return ::RegOpenKeyExW(reinterpret_cast< ::HKEY >(hKey), lpSubKey, ulOptions, samDesired, reinterpret_cast< ::HKEY* >(phkResult));
431 }
432
433 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegQueryValueExA
434 (boost::ipwinapiext::HKEY_ hKey, const char *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData)
435 {
436 return ::RegQueryValueExA(reinterpret_cast< ::HKEY >(hKey), lpValueName, lpReserved, lpType, lpData, lpcbData);
437 }
438
439 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegQueryValueExW
440 (boost::ipwinapiext::HKEY_ hKey, const wchar_t *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData)
441 {
442 return ::RegQueryValueExW(reinterpret_cast< ::HKEY >(hKey), lpValueName, lpReserved, lpType, lpData, lpcbData);
443 }
444
445 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegCloseKey(boost::ipwinapiext::HKEY_ hKey)
446 {
447 return ::RegCloseKey(reinterpret_cast< ::HKEY >(hKey));
448 }
449
450 BOOST_FORCEINLINE void GetSystemInfo(boost::winapi::LPSYSTEM_INFO_ lpSystemInfo)
451 { return ::GetSystemInfo(reinterpret_cast< ::_SYSTEM_INFO* >(lpSystemInfo)); }
452
453 #endif //BOOST_WINAPI_PARTITION_APP_SYSTEM
454
455 } //namespace ipwinapiext {
456 } //namespace boost {
457
458 namespace boost {
459 namespace interprocess {
460 namespace winapi {
461
462 typedef boost::winapi::SYSTEM_INFO_ interprocess_system_info;
463 typedef boost::winapi::OVERLAPPED_ interprocess_overlapped;
464 typedef boost::winapi::FILETIME_ interprocess_filetime;
465 typedef boost::winapi::WIN32_FIND_DATAA_ win32_find_data_a;
466 typedef boost::winapi::WIN32_FIND_DATAW_ win32_find_data_w;
467 typedef boost::winapi::SECURITY_ATTRIBUTES_ interprocess_security_attributes;
468 typedef boost::winapi::SECURITY_DESCRIPTOR_ interprocess_security_descriptor;
469 typedef boost::winapi::BY_HANDLE_FILE_INFORMATION_ interprocess_by_handle_file_information;
470 typedef boost::winapi::HMODULE_ hmodule;
471 typedef boost::ipwinapiext::HKEY_ hkey;
472 typedef boost::winapi::FARPROC_ farproc_t;
473
474 //ntdll.dll
475 typedef long (__stdcall *NtDeleteFile_t)(object_attributes_t *ObjectAttributes);
476 typedef long (__stdcall *NtSetInformationFile_t)(void *FileHandle, io_status_block_t *IoStatusBlock, void *FileInformation, unsigned long Length, int FileInformationClass );
477 typedef long (__stdcall *NtOpenFile)(void **FileHandle, unsigned long DesiredAccess, object_attributes_t *ObjectAttributes
478 , io_status_block_t *IoStatusBlock, unsigned long ShareAccess, unsigned long Length, unsigned long OpenOptions);
479 typedef long (__stdcall *NtQuerySystemInformation_t)(int, void*, unsigned long, unsigned long *);
480 typedef long (__stdcall *NtQueryObject_t)(void*, object_information_class, void *, unsigned long, unsigned long *);
481 typedef long (__stdcall *NtQuerySemaphore_t)(void*, unsigned int info_class, interprocess_semaphore_basic_information *pinfo, unsigned int info_size, unsigned int *ret_len);
482 typedef long (__stdcall *NtQuerySection_t)(void*, section_information_class, interprocess_section_basic_information *pinfo, unsigned long info_size, unsigned long *ret_len);
483 typedef long (__stdcall *NtQueryInformationFile_t)(void *,io_status_block_t *,void *, long, int);
484 typedef long (__stdcall *NtOpenFile_t)(void*,unsigned long ,object_attributes_t*,io_status_block_t*,unsigned long,unsigned long);
485 typedef long (__stdcall *NtClose_t) (void*);
486 typedef long (__stdcall *NtQueryTimerResolution_t) (unsigned long* LowestResolution, unsigned long* HighestResolution, unsigned long* CurrentResolution);
487 typedef long (__stdcall *NtSetTimerResolution_t) (unsigned long RequestedResolution, int Set, unsigned long* ActualResolution);
488
489 } //namespace winapi {
490 } //namespace interprocess {
491 } //namespace boost {
492
493 //////////////////////////////////////////////////////////////////////////////
494 //
495 // Forward declaration of constants
496 //
497 //////////////////////////////////////////////////////////////////////////////
498
499 namespace boost {
500 namespace interprocess {
501 namespace winapi {
502
503 //Some used constants
504 static const unsigned long infinite_time = 0xFFFFFFFF;
505 static const unsigned long error_already_exists = 183L;
506 static const unsigned long error_invalid_handle = 6L;
507 static const unsigned long error_sharing_violation = 32L;
508 static const unsigned long error_file_not_found = 2u;
509 static const unsigned long error_no_more_files = 18u;
510 static const unsigned long error_not_locked = 158L;
511 //Retries in CreateFile, see http://support.microsoft.com/kb/316609
512 static const unsigned long error_sharing_violation_tries = 3L;
513 static const unsigned long error_sharing_violation_sleep_ms = 250L;
514 static const unsigned long error_file_too_large = 223L;
515 static const unsigned long error_insufficient_buffer = 122L;
516 static const unsigned long error_handle_eof = 38L;
517 static const unsigned long semaphore_all_access = (0x000F0000L)|(0x00100000L)|0x3;
518 static const unsigned long mutex_all_access = (0x000F0000L)|(0x00100000L)|0x0001;
519
520 static const unsigned long page_readonly = 0x02;
521 static const unsigned long page_readwrite = 0x04;
522 static const unsigned long page_writecopy = 0x08;
523 static const unsigned long page_noaccess = 0x01;
524
525 static const unsigned long standard_rights_required = 0x000F0000L;
526 static const unsigned long section_query = 0x0001;
527 static const unsigned long section_map_write = 0x0002;
528 static const unsigned long section_map_read = 0x0004;
529 static const unsigned long section_map_execute = 0x0008;
530 static const unsigned long section_extend_size = 0x0010;
531 static const unsigned long section_all_access = standard_rights_required |
532 section_query |
533 section_map_write |
534 section_map_read |
535 section_map_execute |
536 section_extend_size;
537
538 static const unsigned long file_map_copy = section_query;
539 static const unsigned long file_map_write = section_map_write;
540 static const unsigned long file_map_read = section_map_read;
541 static const unsigned long file_map_all_access = section_all_access;
542 static const unsigned long delete_access = 0x00010000L;
543 static const unsigned long file_flag_backup_semantics = 0x02000000;
544 static const long file_flag_delete_on_close = 0x04000000;
545
546 //Native API constants
547 static const unsigned long file_open_for_backup_intent = 0x00004000;
548 static const int file_share_valid_flags = 0x00000007;
549 static const long file_delete_on_close = 0x00001000L;
550 static const long obj_case_insensitive = 0x00000040L;
551 static const long delete_flag = 0x00010000L;
552
553 static const unsigned long movefile_copy_allowed = 0x02;
554 static const unsigned long movefile_delay_until_reboot = 0x04;
555 static const unsigned long movefile_replace_existing = 0x01;
556 static const unsigned long movefile_write_through = 0x08;
557 static const unsigned long movefile_create_hardlink = 0x10;
558 static const unsigned long movefile_fail_if_not_trackable = 0x20;
559
560 static const unsigned long file_share_read = 0x00000001;
561 static const unsigned long file_share_write = 0x00000002;
562 static const unsigned long file_share_delete = 0x00000004;
563
564 static const unsigned long file_attribute_readonly = 0x00000001;
565 static const unsigned long file_attribute_hidden = 0x00000002;
566 static const unsigned long file_attribute_system = 0x00000004;
567 static const unsigned long file_attribute_directory = 0x00000010;
568 static const unsigned long file_attribute_archive = 0x00000020;
569 static const unsigned long file_attribute_device = 0x00000040;
570 static const unsigned long file_attribute_normal = 0x00000080;
571 static const unsigned long file_attribute_temporary = 0x00000100;
572
573 static const unsigned long generic_read = 0x80000000L;
574 static const unsigned long generic_write = 0x40000000L;
575
576 static const unsigned long wait_object_0 = 0;
577 static const unsigned long wait_abandoned = 0x00000080L;
578 static const unsigned long wait_timeout = 258L;
579 static const unsigned long wait_failed = (unsigned long)0xFFFFFFFF;
580
581 static const unsigned long duplicate_close_source = (unsigned long)0x00000001;
582 static const unsigned long duplicate_same_access = (unsigned long)0x00000002;
583
584 static const unsigned long format_message_allocate_buffer
585 = (unsigned long)0x00000100;
586 static const unsigned long format_message_ignore_inserts
587 = (unsigned long)0x00000200;
588 static const unsigned long format_message_from_string
589 = (unsigned long)0x00000400;
590 static const unsigned long format_message_from_hmodule
591 = (unsigned long)0x00000800;
592 static const unsigned long format_message_from_system
593 = (unsigned long)0x00001000;
594 static const unsigned long format_message_argument_array
595 = (unsigned long)0x00002000;
596 static const unsigned long format_message_max_width_mask
597 = (unsigned long)0x000000FF;
598 static const unsigned long lang_neutral = (unsigned long)0x00;
599 static const unsigned long sublang_default = (unsigned long)0x01;
600 static const unsigned long invalid_file_size = (unsigned long)0xFFFFFFFF;
601 static const unsigned long invalid_file_attributes = ((unsigned long)-1);
602 static void * const invalid_handle_value = ((void*)(long)(-1));
603
604 static const unsigned long file_type_char = 0x0002L;
605 static const unsigned long file_type_disk = 0x0001L;
606 static const unsigned long file_type_pipe = 0x0003L;
607 static const unsigned long file_type_remote = 0x8000L;
608 static const unsigned long file_type_unknown = 0x0000L;
609
610 static const unsigned long create_new = 1;
611 static const unsigned long create_always = 2;
612 static const unsigned long open_existing = 3;
613 static const unsigned long open_always = 4;
614 static const unsigned long truncate_existing = 5;
615
616 static const unsigned long file_begin = 0;
617 static const unsigned long file_current = 1;
618 static const unsigned long file_end = 2;
619
620 static const unsigned long lockfile_fail_immediately = 1;
621 static const unsigned long lockfile_exclusive_lock = 2;
622 static const unsigned long error_lock_violation = 33;
623 static const unsigned long security_descriptor_revision = 1;
624
625 const unsigned long max_record_buffer_size = 0x10000L; // 64K
626 const unsigned long max_path = 260;
627
628 //Keys
629 static const hkey hkey_local_machine = (hkey)(unsigned long*)(long)(0x80000002);
630 static unsigned long key_query_value = 0x0001;
631
632 // Registry types
633 #define reg_none ( 0 ) // No value type
634 #define reg_sz ( 1 ) // Unicode nul terminated string
635 #define reg_expand_sz ( 2 ) // Unicode nul terminated string
636 // (with environment variable references)
637 #define reg_binary ( 3 ) // Free form binary
638 #define reg_dword ( 4 ) // 32-bit number
639 #define reg_dword_little_endian ( 4 ) // 32-bit number (same as REG_DWORD)
640 #define reg_dword_big_endian ( 5 ) // 32-bit number
641 #define reg_link ( 6 ) // Symbolic Link (unicode)
642 #define reg_multi_sz ( 7 ) // Multiple Unicode strings
643 #define reg_resource_list ( 8 ) // Resource list in the resource map
644 #define reg_full_resource_descriptor ( 9 ) // Resource list in the hardware description
645 #define reg_resource_requirements_list ( 10 )
646 #define reg_qword ( 11 ) // 64-bit number
647 #define reg_qword_little_endian ( 11 ) // 64-bit number (same as reg_qword)
648
649
650 } //namespace winapi {
651 } //namespace interprocess {
652 } //namespace boost {
653
654
655 namespace boost {
656 namespace interprocess {
657 namespace winapi {
658
659 inline unsigned long get_last_error()
660 { return GetLastError(); }
661
662 inline void set_last_error(unsigned long err)
663 { return SetLastError(err); }
664
665 inline unsigned long format_message
666 (unsigned long dwFlags, const void *lpSource,
667 unsigned long dwMessageId, unsigned long dwLanguageId,
668 char *lpBuffer, unsigned long nSize, std::va_list *Arguments)
669 {
670 return FormatMessageA
671 (dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments);
672 }
673
674 //And now, wrapper functions
675 inline void * local_free(void *hmem)
676 { return LocalFree(hmem); }
677
678 inline unsigned long make_lang_id(unsigned long p, unsigned long s)
679 {
680 const unsigned short s_us = (unsigned short)s;
681 const unsigned short p_us = (unsigned short)p;
682 return (unsigned long)((s_us << 10) | p_us);
683 }
684
685 inline void sched_yield()
686 {
687 if(!SwitchToThread()){
688 Sleep(0);
689 }
690 }
691
692 inline void sleep_tick()
693 { Sleep(1); }
694
695 inline void sleep(unsigned long ms)
696 { Sleep(ms); }
697
698 inline unsigned long get_current_thread_id()
699 { return GetCurrentThreadId(); }
700
701 inline bool get_process_times
702 ( void *hProcess, interprocess_filetime* lpCreationTime
703 , interprocess_filetime *lpExitTime, interprocess_filetime *lpKernelTime
704 , interprocess_filetime *lpUserTime )
705 { return 0 != GetProcessTimes(hProcess, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime); }
706
707 inline unsigned long get_current_process_id()
708 { return GetCurrentProcessId(); }
709
710 inline bool close_handle(void* handle)
711 { return CloseHandle(handle) != 0; }
712
713 inline void * find_first_file(const char *lpFileName, win32_find_data_a *lpFindFileData)
714 { return FindFirstFileA(lpFileName, lpFindFileData); }
715
716 inline void * find_first_file(const wchar_t *lpFileName, win32_find_data_w *lpFindFileData)
717 { return FindFirstFileW(lpFileName, lpFindFileData); }
718
719 inline bool find_next_file(void *hFindFile, win32_find_data_a *lpFindFileData)
720 { return FindNextFileA(hFindFile, lpFindFileData) != 0; }
721
722 inline bool find_next_file(void *hFindFile, win32_find_data_w *lpFindFileData)
723 { return FindNextFileW(hFindFile, lpFindFileData) != 0; }
724
725 inline bool find_close(void *handle)
726 { return FindClose(handle) != 0; }
727
728 inline bool duplicate_current_process_handle
729 (void *hSourceHandle, void **lpTargetHandle)
730 {
731 return 0 != DuplicateHandle
732 ( GetCurrentProcess(), hSourceHandle, GetCurrentProcess()
733 , lpTargetHandle, 0, 0
734 , duplicate_same_access);
735 }
736
737 inline unsigned long get_file_type(void *hFile)
738 {
739 return GetFileType(hFile);
740 }
741
742 /*
743 inline void get_system_time_as_file_time(interprocess_filetime *filetime)
744 { GetSystemTimeAsFileTime(filetime); }
745
746 inline bool file_time_to_local_file_time
747 (const interprocess_filetime *in, const interprocess_filetime *out)
748 { return 0 != FileTimeToLocalFileTime(in, out); }
749 */
750 inline void *open_or_create_mutex(const char *name, bool initial_owner, interprocess_security_attributes *attr)
751 { return CreateMutexA(attr, (int)initial_owner, name); }
752
753 inline void *open_or_create_mutex(const wchar_t *name, bool initial_owner, interprocess_security_attributes *attr)
754 { return CreateMutexW(attr, (int)initial_owner, name); }
755
756 inline unsigned long wait_for_single_object(void *handle, unsigned long time)
757 { return WaitForSingleObject(handle, time); }
758
759 inline int release_mutex(void *handle)
760 { return ReleaseMutex(handle); }
761
762 inline int unmap_view_of_file(void *address)
763 { return UnmapViewOfFile(address); }
764
765 inline void *open_or_create_semaphore(const char *name, long initial_count, long maximum_count, interprocess_security_attributes *attr)
766 { return CreateSemaphoreA(attr, initial_count, maximum_count, name); }
767
768 inline void *open_or_create_semaphore(const wchar_t *name, long initial_count, long maximum_count, interprocess_security_attributes *attr)
769 { return CreateSemaphoreW(attr, initial_count, maximum_count, name); }
770
771 inline void *open_semaphore(const char *name)
772 { return OpenSemaphoreA(semaphore_all_access, 0, name); }
773
774 inline void *open_semaphore(const wchar_t *name)
775 { return OpenSemaphoreW(semaphore_all_access, 0, name); }
776
777 inline int release_semaphore(void *handle, long release_count, long *prev_count)
778 { return ReleaseSemaphore(handle, release_count, prev_count); }
779
780 class interprocess_all_access_security
781 {
782 interprocess_security_attributes sa;
783 interprocess_security_descriptor sd;
784 bool initialized;
785
786 public:
787 interprocess_all_access_security()
788 : initialized(false)
789 {
790 if(!boost::winapi::InitializeSecurityDescriptor(&sd, security_descriptor_revision))
791 return;
792 if(!boost::winapi::SetSecurityDescriptorDacl(&sd, true, 0, false))
793 return;
794 sa.lpSecurityDescriptor = &sd;
795 sa.nLength = sizeof(interprocess_security_attributes);
796 sa.bInheritHandle = false;
797 initialized = true;
798 }
799
800 interprocess_security_attributes *get_attributes()
801 { return &sa; }
802 };
803
804 inline void * create_file_mapping (void * handle, unsigned long access, ::boost::ulong_long_type file_offset, const char * name, interprocess_security_attributes *psec)
805 {
806 const boost::winapi::DWORD_ high_size = boost::winapi::DWORD_(file_offset >> 32);
807 const boost::winapi::DWORD_ low_size = boost::winapi::DWORD_(file_offset);
808 return CreateFileMappingA (handle, psec, access, high_size, low_size, name);
809 }
810
811 inline void * create_file_mapping (void * handle, unsigned long access, ::boost::ulong_long_type file_offset, const wchar_t * name, interprocess_security_attributes *psec)
812 {
813 const boost::winapi::DWORD_ high_size = boost::winapi::DWORD_(file_offset >> 32);
814 const boost::winapi::DWORD_ low_size = boost::winapi::DWORD_(file_offset);
815 return CreateFileMappingW (handle, psec, access, high_size, low_size, name);
816 }
817
818 inline void * open_file_mapping (unsigned long access, const char *name)
819 { return OpenFileMappingA (access, 0, name); }
820
821 inline void * open_file_mapping (unsigned long access, const wchar_t *name)
822 { return OpenFileMappingW (access, 0, name); }
823
824 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)
825 {
826 const boost::winapi::DWORD_ offset_low = boost::winapi::DWORD_(offset & ((::boost::ulong_long_type)0xFFFFFFFF));
827 const boost::winapi::DWORD_ offset_high = boost::winapi::DWORD_(offset >> 32);
828 return MapViewOfFileEx(handle, file_access, offset_high, offset_low, numbytes, base_addr);
829 }
830
831 template<class CharT>
832 inline void *create_file(const CharT *name, unsigned long access, unsigned long creation_flags, unsigned long attributes, interprocess_security_attributes *psec)
833 {
834 for (unsigned int attempt(0); attempt < error_sharing_violation_tries; ++attempt){
835 void * const handle = boost::winapi::create_file(name, access,
836 file_share_read | file_share_write | file_share_delete,
837 psec, creation_flags, attributes, 0);
838 bool const invalid(invalid_handle_value == handle);
839 if (!invalid){
840 return handle;
841 }
842 if (error_sharing_violation != get_last_error()){
843 return handle;
844 }
845 sleep(error_sharing_violation_sleep_ms);
846 }
847 return invalid_handle_value;
848 }
849
850 inline void get_system_info(interprocess_system_info *info)
851 { boost::ipwinapiext::GetSystemInfo(info); }
852
853 inline bool flush_view_of_file(void *base_addr, std::size_t numbytes)
854 { return 0 != boost::winapi::FlushViewOfFile(base_addr, numbytes); }
855
856 inline bool virtual_unlock(void *base_addr, std::size_t numbytes)
857 { return 0 != boost::ipwinapiext::VirtualUnlock(base_addr, numbytes); }
858
859 inline bool virtual_protect(void *base_addr, std::size_t numbytes, unsigned long flNewProtect, unsigned long &lpflOldProtect)
860 { return 0 != boost::ipwinapiext::VirtualProtect(base_addr, numbytes, flNewProtect, &lpflOldProtect); }
861
862 inline bool flush_file_buffers(void *handle)
863 { return 0 != boost::ipwinapiext::FlushFileBuffers(handle); }
864
865 inline bool get_file_size(void *handle, __int64 &size)
866 { return 0 != boost::winapi::GetFileSizeEx(handle, (boost::winapi::LARGE_INTEGER_*)&size); }
867
868 template<class CharT>
869 inline bool create_directory(const CharT *name)
870 {
871 interprocess_all_access_security sec;
872 return 0 != boost::winapi::create_directory(name, sec.get_attributes());
873 }
874
875 template<class CharT>
876 inline bool remove_directory(const CharT *lpPathName)
877 { return 0 != boost::winapi::remove_directory(lpPathName); }
878
879 template<class CharT>
880 inline unsigned long get_temp_path(unsigned long length, CharT *buffer)
881 { return boost::winapi::get_temp_path(length, buffer); }
882
883 inline int set_end_of_file(void *handle)
884 { return 0 != boost::winapi::SetEndOfFile(handle); }
885
886 inline bool set_file_pointer(void *handle, __int64 distance, __int64 *new_file_pointer, unsigned long move_method)
887 {
888 boost::winapi::LONG_ highPart = boost::winapi::LONG_(distance >> 32u);
889 boost::winapi::DWORD_ r = boost::winapi::SetFilePointer(handle, (long)distance, &highPart, move_method);
890 bool br = r != boost::winapi::INVALID_SET_FILE_POINTER_ || boost::winapi::GetLastError() != 0;
891 if (br && new_file_pointer){
892 *new_file_pointer = (__int64)r + ((__int64)highPart << 32);
893 }
894
895 return br;
896 }
897
898 inline bool lock_file_ex(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
899 { return 0 != boost::winapi::LockFileEx(hnd, flags, reserved, size_low, size_high, overlapped); }
900
901 inline bool unlock_file_ex(void *hnd, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
902 { return 0 != boost::winapi::UnlockFileEx(hnd, reserved, size_low, size_high, overlapped); }
903
904 inline bool write_file(void *hnd, const void *buffer, unsigned long bytes_to_write, unsigned long *bytes_written, interprocess_overlapped* overlapped)
905 { return 0 != boost::winapi::WriteFile(hnd, buffer, bytes_to_write, bytes_written, overlapped); }
906
907 inline bool read_file(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped)
908 { return 0 != boost::winapi::ReadFile(hnd, buffer, bytes_to_read, bytes_read, overlapped); }
909
910 inline bool get_file_information_by_handle(void *hnd, interprocess_by_handle_file_information *info)
911 { return 0 != boost::winapi::GetFileInformationByHandle(hnd, info); }
912
913 inline long interlocked_increment(long volatile *addr)
914 { return BOOST_INTERLOCKED_INCREMENT(const_cast<long*>(addr)); }
915
916 inline long interlocked_decrement(long volatile *addr)
917 { return BOOST_INTERLOCKED_DECREMENT(const_cast<long*>(addr)); }
918
919 inline long interlocked_compare_exchange(long volatile *addr, long val1, long val2)
920 { return BOOST_INTERLOCKED_COMPARE_EXCHANGE(const_cast<long*>(addr), val1, val2); }
921
922 inline long interlocked_exchange_add(long volatile* addend, long value)
923 { return BOOST_INTERLOCKED_EXCHANGE_ADD(const_cast<long*>(addend), value); }
924
925 inline long interlocked_exchange(long volatile* addend, long value)
926 { return BOOST_INTERLOCKED_EXCHANGE(const_cast<long*>(addend), value); }
927
928 //Forward functions
929 inline hmodule load_library(const char *name)
930 { return boost::winapi::LoadLibraryA(name); }
931
932 inline bool free_library(hmodule module)
933 { return 0 != boost::winapi::FreeLibrary(module); }
934
935 inline farproc_t get_proc_address(hmodule module, const char *name)
936 { return boost::winapi::GetProcAddress(module, name); }
937
938 inline void *get_current_process()
939 { return boost::winapi::GetCurrentProcess(); }
940
941 inline hmodule get_module_handle(const char *name)
942 { return boost::winapi::GetModuleHandleA(name); }
943
944 inline long reg_open_key_ex(hkey hKey, const char *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
945 { return boost::ipwinapiext::RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
946
947 inline long reg_open_key_ex(hkey hKey, const wchar_t *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
948 { return boost::ipwinapiext::RegOpenKeyExW(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
949
950
951 inline long reg_query_value_ex(hkey hKey, const char *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
952 { return boost::ipwinapiext::RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
953
954 inline long reg_query_value_ex(hkey hKey, const wchar_t *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
955 { return boost::ipwinapiext::RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
956
957 inline long reg_close_key(hkey hKey)
958 { return boost::ipwinapiext::RegCloseKey(hKey); }
959
960 inline void initialize_object_attributes
961 ( object_attributes_t *pobject_attr, unicode_string_t *name
962 , unsigned long attr, void *rootdir, void *security_descr)
963
964 {
965 pobject_attr->Length = sizeof(object_attributes_t);
966 pobject_attr->RootDirectory = rootdir;
967 pobject_attr->Attributes = attr;
968 pobject_attr->ObjectName = name;
969 pobject_attr->SecurityDescriptor = security_descr;
970 pobject_attr->SecurityQualityOfService = 0;
971 }
972
973 inline void rtl_init_empty_unicode_string(unicode_string_t *ucStr, wchar_t *buf, unsigned short bufSize)
974 {
975 ucStr->Buffer = buf;
976 ucStr->Length = 0;
977 ucStr->MaximumLength = bufSize;
978 }
979
980 //A class that locates and caches loaded DLL function addresses.
981 template<int Dummy>
982 struct function_address_holder
983 {
984 enum { NtSetInformationFile
985 , NtQuerySystemInformation
986 , NtQueryObject
987 , NtQuerySemaphore
988 , NtQuerySection
989 , NtOpenFile
990 , NtClose
991 , NtQueryTimerResolution
992 , NumFunction
993 };
994 enum { NtDll_dll, Kernel32_dll, NumModule };
995
996 private:
997 static const char *FunctionNames[NumFunction];
998 static const char *ModuleNames[NumModule];
999 static farproc_t FunctionAddresses[NumFunction];
1000 static unsigned int FunctionModules[NumFunction];
1001 static volatile long FunctionStates[NumFunction];
1002 static hmodule ModuleAddresses[NumModule];
1003 static volatile long ModuleStates[NumModule];
1004
1005 static hmodule get_module_from_id(unsigned int id)
1006 {
1007 BOOST_ASSERT(id < (unsigned int)NumModule);
1008 hmodule addr = get_module_handle(ModuleNames[id]);
1009 BOOST_ASSERT(addr);
1010 return addr;
1011 }
1012
1013 static hmodule get_module(const unsigned int id)
1014 {
1015 BOOST_ASSERT(id < (unsigned int)NumModule);
1016 for(unsigned i = 0; ModuleStates[id] < 2; ++i){
1017 if(interlocked_compare_exchange(&ModuleStates[id], 1, 0) == 0){
1018 ModuleAddresses[id] = get_module_from_id(id);
1019 interlocked_increment(&ModuleStates[id]);
1020 break;
1021 }
1022 else if(i & 1){
1023 sched_yield();
1024 }
1025 else{
1026 sleep_tick();
1027 }
1028 }
1029 return ModuleAddresses[id];
1030 }
1031
1032 static farproc_t get_address_from_dll(const unsigned int id)
1033 {
1034 BOOST_ASSERT(id < (unsigned int)NumFunction);
1035 farproc_t addr = get_proc_address(get_module(FunctionModules[id]), FunctionNames[id]);
1036 BOOST_ASSERT(addr);
1037 return addr;
1038 }
1039
1040 public:
1041 static farproc_t get(const unsigned int id)
1042 {
1043 BOOST_ASSERT(id < (unsigned int)NumFunction);
1044 for(unsigned i = 0; FunctionStates[id] < 2; ++i){
1045 if(interlocked_compare_exchange(&FunctionStates[id], 1, 0) == 0){
1046 FunctionAddresses[id] = get_address_from_dll(id);
1047 interlocked_increment(&FunctionStates[id]);
1048 break;
1049 }
1050 else if(i & 1){
1051 sched_yield();
1052 }
1053 else{
1054 sleep_tick();
1055 }
1056 }
1057 return FunctionAddresses[id];
1058 }
1059 };
1060
1061 template<int Dummy>
1062 const char *function_address_holder<Dummy>::FunctionNames[function_address_holder<Dummy>::NumFunction] =
1063 {
1064 "NtSetInformationFile",
1065 "NtQuerySystemInformation",
1066 "NtQueryObject",
1067 "NtQuerySemaphore",
1068 "NtQuerySection",
1069 "NtOpenFile",
1070 "NtClose",
1071 "NtQueryTimerResolution",
1072 };
1073
1074 template<int Dummy>
1075 unsigned int function_address_holder<Dummy>::FunctionModules[function_address_holder<Dummy>::NumFunction] =
1076 {
1077 NtDll_dll,
1078 NtDll_dll,
1079 NtDll_dll,
1080 NtDll_dll,
1081 NtDll_dll,
1082 NtDll_dll,
1083 NtDll_dll,
1084 NtDll_dll,
1085 };
1086
1087 template<int Dummy>
1088 const char *function_address_holder<Dummy>::ModuleNames[function_address_holder<Dummy>::NumModule] =
1089 {
1090 "ntdll.dll"//, "kernel32.dll"
1091 };
1092
1093
1094 template<int Dummy>
1095 farproc_t function_address_holder<Dummy>::FunctionAddresses[function_address_holder<Dummy>::NumFunction];
1096
1097 template<int Dummy>
1098 volatile long function_address_holder<Dummy>::FunctionStates[function_address_holder<Dummy>::NumFunction];
1099
1100 template<int Dummy>
1101 hmodule function_address_holder<Dummy>::ModuleAddresses[function_address_holder<Dummy>::NumModule];
1102
1103 template<int Dummy>
1104 volatile long function_address_holder<Dummy>::ModuleStates[function_address_holder<Dummy>::NumModule];
1105
1106
1107 struct dll_func
1108 : public function_address_holder<0>
1109 {};
1110
1111 //Complex winapi based functions...
1112 struct library_unloader
1113 {
1114 hmodule lib_;
1115 library_unloader(hmodule module) : lib_(module){}
1116 ~library_unloader(){ free_library(lib_); }
1117 };
1118
1119
1120 inline bool get_system_time_of_day_information(system_timeofday_information &info)
1121 {
1122 NtQuerySystemInformation_t pNtQuerySystemInformation = reinterpret_cast<NtQuerySystemInformation_t>
1123 (dll_func::get(dll_func::NtQuerySystemInformation));
1124 unsigned long res;
1125 long status = pNtQuerySystemInformation(system_time_of_day_information, &info, sizeof(info), &res);
1126 if(status){
1127 return false;
1128 }
1129 return true;
1130 }
1131
1132 inline bool get_boot_time(unsigned char (&bootstamp) [BootstampLength])
1133 {
1134 system_timeofday_information info;
1135 bool ret = get_system_time_of_day_information(info);
1136 if(!ret){
1137 return false;
1138 }
1139 std::memcpy(&bootstamp[0], &info.Reserved1, sizeof(bootstamp));
1140 return true;
1141 }
1142
1143 inline bool get_boot_and_system_time(unsigned char (&bootsystemstamp) [BootAndSystemstampLength])
1144 {
1145 system_timeofday_information info;
1146 bool ret = get_system_time_of_day_information(info);
1147 if(!ret){
1148 return false;
1149 }
1150 std::memcpy(&bootsystemstamp[0], &info.Reserved1, sizeof(bootsystemstamp));
1151 return true;
1152 }
1153
1154 //Writes the hexadecimal value of the buffer, in the wide character string.
1155 //str must be twice length
1156 inline void buffer_to_wide_str(const void *buf, std::size_t length, wchar_t *str)
1157 {
1158 const wchar_t Characters [] =
1159 { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7'
1160 , L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
1161 std::size_t char_counter = 0;
1162 const char *chbuf = static_cast<const char *>(buf);
1163 for(std::size_t i = 0; i != length; ++i){
1164 str[char_counter++] = Characters[(chbuf[i]&0xF0)>>4];
1165 str[char_counter++] = Characters[(chbuf[i]&0x0F)];
1166 }
1167 }
1168
1169 //Writes the hexadecimal value of the buffer, in the narrow character string.
1170 //str must be twice length
1171 inline void buffer_to_narrow_str(const void *buf, std::size_t length, char *str)
1172 {
1173 const char Characters [] =
1174 { '0', '1', '2', '3', '4', '5', '6', '7'
1175 , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1176 std::size_t char_counter = 0;
1177 const char *chbuf = static_cast<const char *>(buf);
1178 for(std::size_t i = 0; i != length; ++i){
1179 str[char_counter++] = Characters[(chbuf[i]&0xF0)>>4];
1180 str[char_counter++] = Characters[(chbuf[i]&0x0F)];
1181 }
1182 }
1183
1184 inline bool get_boot_time_str(char *bootstamp_str, std::size_t &s)
1185 //will write BootstampLength chars
1186 {
1187 if(s < (BootstampLength*2))
1188 return false;
1189 system_timeofday_information info;
1190 bool ret = get_system_time_of_day_information(info);
1191 if(!ret){
1192 return false;
1193 }
1194
1195 buffer_to_narrow_str(info.Reserved1, BootstampLength, bootstamp_str);
1196 s = BootstampLength*2;
1197 return true;
1198 }
1199
1200 inline bool get_boot_and_system_time_wstr(wchar_t *bootsystemstamp, std::size_t &s)
1201 //will write BootAndSystemstampLength chars
1202 {
1203 if(s < (BootAndSystemstampLength*2))
1204 return false;
1205 system_timeofday_information info;
1206 bool ret = get_system_time_of_day_information(info);
1207 if(!ret){
1208 return false;
1209 }
1210
1211 buffer_to_wide_str(&info.Reserved1[0], BootAndSystemstampLength, bootsystemstamp);
1212 s = BootAndSystemstampLength*2;
1213 return true;
1214 }
1215
1216 class handle_closer
1217 {
1218 void *handle_;
1219 handle_closer(const handle_closer &);
1220 handle_closer& operator=(const handle_closer &);
1221 public:
1222 explicit handle_closer(void *handle) : handle_(handle){}
1223 ~handle_closer()
1224 { close_handle(handle_); }
1225 };
1226
1227 union ntquery_mem_t
1228 {
1229 object_name_information_t name;
1230 struct ren_t
1231 {
1232 file_rename_information_t info;
1233 wchar_t buf[1];
1234 } ren;
1235 };
1236
1237 class nt_query_mem_deleter
1238 {
1239 static const std::size_t rename_offset = offsetof(ntquery_mem_t, ren.info.FileName) -
1240 offsetof(ntquery_mem_t, name.Name.Buffer);
1241 // Timestamp process id atomic count
1242 static const std::size_t rename_suffix =
1243 (SystemTimeOfDayInfoLength + sizeof(unsigned long) + sizeof(boost::winapi::DWORD_))*2;
1244
1245 public:
1246 explicit nt_query_mem_deleter(std::size_t object_name_info_size)
1247 : m_size(object_name_info_size + rename_offset + rename_suffix)
1248 , m_buf(new char [m_size])
1249 {}
1250
1251 ~nt_query_mem_deleter()
1252 {
1253 delete[]m_buf;
1254 }
1255
1256 void realloc_mem(std::size_t num_bytes)
1257 {
1258 num_bytes += rename_suffix + rename_offset;
1259 char *buf = m_buf;
1260 m_buf = new char[num_bytes];
1261 delete[]buf;
1262 m_size = num_bytes;
1263 }
1264
1265 ntquery_mem_t *query_mem() const
1266 { return static_cast<ntquery_mem_t *>(static_cast<void*>(m_buf)); }
1267
1268 unsigned long object_name_information_size() const
1269 {
1270 return static_cast<unsigned long>(m_size - rename_offset - SystemTimeOfDayInfoLength*2);
1271 }
1272
1273 std::size_t file_rename_information_size() const
1274 { return static_cast<unsigned long>(m_size); }
1275
1276 private:
1277 std::size_t m_size;
1278 char *m_buf;
1279 };
1280
1281 class c_heap_deleter
1282 {
1283 public:
1284 explicit c_heap_deleter(std::size_t size)
1285 : m_buf(::malloc(size))
1286 {}
1287
1288 ~c_heap_deleter()
1289 {
1290 if(m_buf) ::free(m_buf);
1291 }
1292
1293 void realloc_mem(std::size_t num_bytes)
1294 {
1295 void *oldBuf = m_buf;
1296 m_buf = ::realloc(m_buf, num_bytes);
1297 if (!m_buf){
1298 free(oldBuf);
1299 }
1300 }
1301
1302 void *get() const
1303 { return m_buf; }
1304
1305 private:
1306 void *m_buf;
1307 };
1308
1309 template<class CharT>
1310 inline bool unlink_file(const CharT *filename)
1311 {
1312 //Don't try to optimize doing a DeleteFile first
1313 //as there are interactions with permissions and
1314 //in-use files.
1315 //
1316 //if(!delete_file(filename)){
1317 // (...)
1318 //
1319
1320 //This functions tries to emulate UNIX unlink semantics in windows.
1321 //
1322 //- Open the file and mark the handle as delete-on-close
1323 //- Rename the file to an arbitrary name based on a random number
1324 //- Close the handle. If there are no file users, it will be deleted.
1325 // Otherwise it will be used by already connected handles but the
1326 // file name can't be used to open this file again
1327 BOOST_TRY{
1328 NtSetInformationFile_t pNtSetInformationFile =
1329 reinterpret_cast<NtSetInformationFile_t>(dll_func::get(dll_func::NtSetInformationFile));
1330
1331 NtQueryObject_t pNtQueryObject = reinterpret_cast<NtQueryObject_t>(dll_func::get(dll_func::NtQueryObject));
1332
1333 //First step: Obtain a handle to the file using Win32 rules. This resolves relative paths
1334 void *fh = create_file(filename, generic_read | delete_access, open_existing, 0, 0);
1335 if(fh == invalid_handle_value){
1336 return false;
1337 }
1338
1339 handle_closer h_closer(fh);
1340 {
1341 //Obtain name length
1342 unsigned long size;
1343 const std::size_t initial_string_mem = 512u;
1344
1345 nt_query_mem_deleter nt_query_mem(sizeof(ntquery_mem_t)+initial_string_mem);
1346 //Obtain file name with guessed length
1347 if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
1348 //Obtain file name with exact length buffer
1349 nt_query_mem.realloc_mem(size);
1350 if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
1351 return false;
1352 }
1353 }
1354 ntquery_mem_t *pmem = nt_query_mem.query_mem();
1355 file_rename_information_t *pfri = &pmem->ren.info;
1356 const std::size_t RenMaxNumChars =
1357 std::size_t(((char*)(pmem) + nt_query_mem.file_rename_information_size()) - (char*)&pmem->ren.info.FileName[0])/sizeof(wchar_t);
1358
1359 //Copy filename to the rename member
1360 std::memmove(pmem->ren.info.FileName, pmem->name.Name.Buffer, pmem->name.Name.Length);
1361 std::size_t filename_string_length = pmem->name.Name.Length/sizeof(wchar_t);
1362
1363 //Search '\\' character to replace from it
1364 for(std::size_t i = filename_string_length; i != 0; --filename_string_length){
1365 if(pmem->ren.info.FileName[--i] == L'\\')
1366 break;
1367 }
1368
1369 //Add random number
1370 std::size_t s = RenMaxNumChars - filename_string_length;
1371 if(!get_boot_and_system_time_wstr(&pfri->FileName[filename_string_length], s)){
1372 return false;
1373 }
1374 filename_string_length += s;
1375
1376 //Sometimes the precission of the timestamp is not enough and we need to add another random number.
1377 //The process id (to exclude concurrent processes) and an atomic count (to exclude concurrent threads).
1378 //should be enough
1379 const unsigned long pid = get_current_process_id();
1380 buffer_to_wide_str(&pid, sizeof(pid), &pfri->FileName[filename_string_length]);
1381 filename_string_length += sizeof(pid)*2;
1382
1383 static volatile boost::uint32_t u32_count = 0;
1384 interlocked_decrement(reinterpret_cast<volatile long*>(&u32_count));
1385 buffer_to_wide_str(const_cast<const boost::uint32_t *>(&u32_count), sizeof(boost::uint32_t), &pfri->FileName[filename_string_length]);
1386 filename_string_length += sizeof(boost::uint32_t)*2;
1387
1388 //Fill rename information (FileNameLength is in bytes)
1389 pfri->FileNameLength = static_cast<unsigned long>(sizeof(wchar_t)*(filename_string_length));
1390 pfri->Replace = 1;
1391 pfri->RootDir = 0;
1392
1393 //Cange the name of the in-use file...
1394 io_status_block_t io;
1395 if(0 != pNtSetInformationFile(fh, &io, pfri, nt_query_mem.file_rename_information_size(), file_rename_information)){
1396 return false;
1397 }
1398 }
1399 //...and mark it as delete-on-close
1400 {
1401 //Don't use pNtSetInformationFile with file_disposition_information as it can return STATUS_CANNOT_DELETE
1402 //if the file is still mapped. Reopen it with NtOpenFile and file_delete_on_close
1403 NtOpenFile_t pNtOpenFile = reinterpret_cast<NtOpenFile_t>(dll_func::get(dll_func::NtOpenFile));
1404 NtClose_t pNtClose = reinterpret_cast<NtClose_t>(dll_func::get(dll_func::NtClose));
1405 const wchar_t empty_str [] = L"";
1406 unicode_string_t ustring = { sizeof(empty_str) - sizeof (wchar_t) //length in bytes without null
1407 , sizeof(empty_str) //total size in bytes of memory allocated for Buffer.
1408 , const_cast<wchar_t*>(empty_str)
1409 };
1410 object_attributes_t object_attr;
1411 initialize_object_attributes(&object_attr, &ustring, 0, fh, 0);
1412 void* fh2 = 0;
1413 io_status_block_t io;
1414 pNtOpenFile( &fh2, delete_flag, &object_attr, &io
1415 , file_share_read | file_share_write | file_share_delete, file_delete_on_close);
1416 pNtClose(fh2);
1417 //Even if NtOpenFile fails, the file was renamed and the original no longer exists, so return a success status
1418 return true;
1419 }
1420 }
1421 BOOST_CATCH(...){
1422 return false;
1423 } BOOST_CATCH_END
1424 return true;
1425 }
1426
1427 struct reg_closer
1428 {
1429 hkey key_;
1430 reg_closer(hkey key) : key_(key){}
1431 ~reg_closer(){ reg_close_key(key_); }
1432 };
1433
1434 template <class CharT>
1435 inline bool get_registry_value_buffer(hkey key_type, const CharT *subkey_name, const CharT *value_name, void *buf, std::size_t &buflen)
1436 {
1437 bool bret = false;
1438 hkey key;
1439 if (reg_open_key_ex( key_type
1440 , subkey_name
1441 , 0
1442 , key_query_value
1443 , &key) == 0){
1444 reg_closer key_closer(key);
1445
1446 //Obtain the value
1447 unsigned long size = buflen;
1448 unsigned long type;
1449 buflen = 0;
1450 bret = 0 == reg_query_value_ex( key, value_name, 0, &type, (unsigned char*)buf, &size);
1451 if(bret)
1452 buflen = (std::size_t)size;
1453 }
1454 return bret;
1455 }
1456
1457 template<class CharT>
1458 inline bool get_registry_value_string(hkey key_type, const CharT *subkey_name, const CharT *value_name, std::basic_string<CharT> &s)
1459 {
1460 bool bret = false;
1461 s.clear();
1462 hkey key;
1463 if (reg_open_key_ex( key_type
1464 , subkey_name
1465 , 0
1466 , key_query_value
1467 , &key) == 0){
1468 reg_closer key_closer(key);
1469
1470 //Obtain the value
1471 unsigned long size;
1472 unsigned long type;
1473 long err = reg_query_value_ex( key, value_name, 0, &type, 0, &size);
1474 if((reg_sz == type || reg_expand_sz == type) && !err){
1475 //Size includes terminating NULL
1476 s.resize(size/sizeof(CharT));
1477 err = reg_query_value_ex( key, value_name, 0, &type, (unsigned char*)(&s[0]), &size);
1478 if(!err){
1479 s.erase(s.end()-1);
1480 bret = true;
1481 }
1482 (void)err;
1483 }
1484 }
1485 return bret;
1486 }
1487
1488 template<class CharT>
1489 inline void get_shared_documents_folder(std::basic_string<CharT> &s)
1490 {
1491 get_registry_value_string( hkey_local_machine
1492 , "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
1493 , "Common AppData"
1494 , s);
1495 }
1496
1497 inline void get_shared_documents_folder(std::wstring &s)
1498 {
1499 get_registry_value_string( hkey_local_machine
1500 , L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
1501 , L"Common AppData"
1502 , s);
1503 }
1504
1505 template<class CharT>
1506 inline void get_registry_value(const CharT *folder, const CharT *value_key, std::vector<unsigned char> &s)
1507 {
1508 s.clear();
1509 hkey key;
1510 if (reg_open_key_ex( hkey_local_machine
1511 , folder
1512 , 0
1513 , key_query_value
1514 , &key) == 0){
1515 reg_closer key_closer(key);
1516
1517 //Obtain the value
1518 unsigned long size;
1519 unsigned long type;
1520 const char *const reg_value = value_key;
1521 //long err = (*pRegQueryValue)( key, reg_value, 0, &type, 0, &size);
1522 long err = reg_query_value_ex( key, reg_value, 0, &type, 0, &size);
1523 if(!err){
1524 //Size includes terminating NULL
1525 s.resize(size);
1526 //err = (*pRegQueryValue)( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
1527 err = reg_query_value_ex( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
1528 if(!err)
1529 s.erase(s.end()-1);
1530 (void)err;
1531 }
1532 }
1533 }
1534
1535 inline bool is_directory(const char *path)
1536 {
1537 unsigned long attrib = GetFileAttributesA(path);
1538
1539 return (attrib != invalid_file_attributes &&
1540 (attrib & file_attribute_directory));
1541 }
1542
1543 inline bool get_file_mapping_size(void *file_mapping_hnd, __int64 &size)
1544 {
1545 NtQuerySection_t pNtQuerySection =
1546 reinterpret_cast<NtQuerySection_t>(dll_func::get(dll_func::NtQuerySection));
1547 //Obtain file name
1548 interprocess_section_basic_information info;
1549 long ntstatus =
1550 pNtQuerySection(file_mapping_hnd, section_basic_information, &info, sizeof(info), 0);
1551 size = info.section_size;
1552 return !ntstatus;
1553 }
1554
1555 inline bool get_semaphore_info(void *handle, long &count, long &limit)
1556 {
1557 winapi::interprocess_semaphore_basic_information info;
1558 winapi::NtQuerySemaphore_t pNtQuerySemaphore =
1559 reinterpret_cast<winapi::NtQuerySemaphore_t>(dll_func::get(winapi::dll_func::NtQuerySemaphore));
1560 unsigned int ret_len;
1561 long status = pNtQuerySemaphore(handle, winapi::semaphore_basic_information, &info, sizeof(info), &ret_len);
1562 count = (long)info.count;
1563 limit = (long)info.limit;
1564 return !status;
1565 }
1566
1567 inline bool query_timer_resolution(unsigned long *lowres, unsigned long *highres, unsigned long *curres)
1568 {
1569 winapi::NtQueryTimerResolution_t pNtQueryTimerResolution =
1570 reinterpret_cast<winapi::NtQueryTimerResolution_t>(dll_func::get(winapi::dll_func::NtQueryTimerResolution));
1571 return !pNtQueryTimerResolution(lowres, highres, curres);
1572 }
1573
1574 inline bool query_performance_counter(__int64 *lpPerformanceCount)
1575 {
1576 return 0 != boost::winapi::QueryPerformanceCounter(reinterpret_cast<boost::winapi::LARGE_INTEGER_*>(lpPerformanceCount));
1577 }
1578
1579 inline bool query_performance_frequency(__int64 *lpFrequency)
1580 {
1581 return 0 != boost::winapi::QueryPerformanceFrequency(reinterpret_cast<boost::winapi::LARGE_INTEGER_*>(lpFrequency));
1582 }
1583
1584 inline unsigned long get_tick_count()
1585 { return GetTickCount(); }
1586
1587
1588 template<class CharT>
1589 struct winapi_traits;
1590
1591 template<>
1592 struct winapi_traits<char>
1593 {
1594 static int cmp(const char *a, const char *b)
1595 { return std::strcmp(a, b); }
1596 };
1597
1598 template<>
1599 struct winapi_traits<wchar_t>
1600 {
1601 static int cmp(const wchar_t *a, const wchar_t *b)
1602 { return std::wcscmp(a, b); }
1603 };
1604
1605
1606 #if defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED)
1607
1608
1609 inline bool get_last_bootup_time(std::string &stamp)
1610 {
1611 unsigned dword_val = 0;
1612 std::size_t dword_size = sizeof(dword_val);
1613 bool b_ret = get_registry_value_buffer( hkey_local_machine
1614 , "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management\\PrefetchParameters"
1615 , "BootId", &dword_val, dword_size);
1616 if (b_ret)
1617 {
1618 char dword_str[sizeof(dword_val)*2u+1];
1619 buffer_to_narrow_str(&dword_val, dword_size, dword_str);
1620 dword_str[sizeof(dword_val)*2] = '\0';
1621 stamp = dword_str;
1622
1623 b_ret = get_registry_value_buffer( hkey_local_machine
1624 , "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Power"
1625 , "HybridBootAnimationTime", &dword_val, dword_size);
1626 //Old Windows versions have no HybridBootAnimationTime
1627 if(b_ret)
1628 {
1629 buffer_to_narrow_str(&dword_val, dword_size, dword_str);
1630 dword_str[sizeof(dword_val)*2] = '\0';
1631 stamp += "_";
1632 stamp += dword_str;
1633 }
1634 b_ret = true;
1635 }
1636 return b_ret;
1637 }
1638
1639 inline bool get_last_bootup_time(std::wstring &stamp)
1640 {
1641 unsigned dword_val = 0;
1642 std::size_t dword_size = sizeof(dword_val);
1643 bool b_ret = get_registry_value_buffer( hkey_local_machine
1644 , L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management\\PrefetchParameters"
1645 , L"BootId", &dword_val, dword_size);
1646 if (b_ret)
1647 {
1648 wchar_t dword_str[sizeof(dword_val)*2u+1];
1649 buffer_to_wide_str(&dword_val, dword_size, dword_str);
1650 dword_str[sizeof(dword_val)*2] = L'\0';
1651 stamp = dword_str;
1652
1653 b_ret = get_registry_value_buffer( hkey_local_machine
1654 , L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Power"
1655 , L"HybridBootAnimationTime", &dword_val, dword_size);
1656 //Old Windows versions have no HybridBootAnimationTime
1657 if(b_ret)
1658 {
1659 buffer_to_wide_str(&dword_val, dword_size, dword_str);
1660 dword_str[sizeof(dword_val)*2] = L'\0';
1661 stamp += L"_";
1662 stamp += dword_str;
1663 }
1664 b_ret = true;
1665 }
1666 return b_ret;
1667 }
1668
1669 #elif defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED)
1670
1671 static const unsigned long eventlog_sequential_read = 0x0001;
1672 static const unsigned long eventlog_backwards_read = 0x0008;
1673
1674 struct interprocess_eventlogrecord
1675 {
1676 unsigned long Length; // Length of full record
1677 unsigned long Reserved; // Used by the service
1678 unsigned long RecordNumber; // Absolute record number
1679 unsigned long TimeGenerated; // Seconds since 1-1-1970
1680 unsigned long TimeWritten; // Seconds since 1-1-1970
1681 unsigned long EventID;
1682 unsigned short EventType;
1683 unsigned short NumStrings;
1684 unsigned short EventCategory;
1685 unsigned short ReservedFlags; // For use with paired events (auditing)
1686 unsigned long ClosingRecordNumber; // For use with paired events (auditing)
1687 unsigned long StringOffset; // Offset from beginning of record
1688 unsigned long UserSidLength;
1689 unsigned long UserSidOffset;
1690 unsigned long DataLength;
1691 unsigned long DataOffset; // Offset from beginning of record
1692 //
1693 // Then follow:
1694 //
1695 // wchar_t SourceName[]
1696 // wchar_t Computername[]
1697 // SID UserSid
1698 // wchar_t Strings[]
1699 // BYTE Data[]
1700 // CHAR Pad[]
1701 // unsigned long Length;
1702 //
1703 };
1704
1705 class eventlog_handle_closer
1706 {
1707 void *handle_;
1708 eventlog_handle_closer(const handle_closer &);
1709 eventlog_handle_closer& operator=(const eventlog_handle_closer &);
1710 public:
1711 explicit eventlog_handle_closer(void *handle) : handle_(handle){}
1712 ~eventlog_handle_closer()
1713 { CloseEventLog(handle_); }
1714 };
1715
1716 // Loop through the buffer and obtain the contents of the
1717 // requested record in the buffer.
1718 template<class CharT>
1719 inline bool find_record_in_buffer( const void* pBuffer, unsigned long dwBytesRead, const CharT *provider_name
1720 , unsigned int id_to_find, interprocess_eventlogrecord *&pevent_log_record)
1721 {
1722 const unsigned char * pRecord = static_cast<const unsigned char*>(pBuffer);
1723 const unsigned char * pEndOfRecords = pRecord + dwBytesRead;
1724
1725 while (pRecord < pEndOfRecords){
1726 interprocess_eventlogrecord *pTypedRecord = (interprocess_eventlogrecord*)(void*)pRecord;
1727 // Check provider, written at the end of the fixed-part of the record
1728
1729 if (0 == winapi_traits<CharT>::cmp(provider_name, (CharT*)(void*)(pRecord + sizeof(interprocess_eventlogrecord))))
1730 {
1731 // Check event id
1732 if(id_to_find == (pTypedRecord->EventID & 0xFFFF)){
1733 pevent_log_record = pTypedRecord;
1734 return true;
1735 }
1736 }
1737
1738 pRecord += pTypedRecord->Length;
1739 }
1740 pevent_log_record = 0;
1741 return false;
1742 }
1743
1744 //Obtains the bootup time from the System Event Log,
1745 //event ID == 6005 (event log started).
1746 //Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/bb427356.aspx
1747 inline bool get_last_bootup_time(std::string &stamp)
1748 {
1749 const char *source_name = "System";
1750 const char *provider_name = "EventLog";
1751 const unsigned short event_id = 6005u;
1752
1753 unsigned long status = 0;
1754 unsigned long dwBytesToRead = 0;
1755 unsigned long dwBytesRead = 0;
1756 unsigned long dwMinimumBytesToRead = 0;
1757
1758 // The source name (provider) must exist as a subkey of Application.
1759 void *hEventLog = OpenEventLogA(0, source_name);
1760 if (hEventLog){
1761 eventlog_handle_closer hnd_closer(hEventLog); (void)hnd_closer;
1762 // Allocate an initial block of memory used to read event records. The number
1763 // of records read into the buffer will vary depending on the size of each event.
1764 // The size of each event will vary based on the size of the user-defined
1765 // data included with each event, the number and length of insertion
1766 // strings, and other data appended to the end of the event record.
1767 dwBytesToRead = max_record_buffer_size;
1768 c_heap_deleter heap_deleter(dwBytesToRead);
1769
1770 // Read blocks of records until you reach the end of the log or an
1771 // error occurs. The records are read from newest to oldest. If the buffer
1772 // is not big enough to hold a complete event record, reallocate the buffer.
1773 if (heap_deleter.get() != 0){
1774 while (0 == status){
1775 if (!ReadEventLogA(hEventLog,
1776 eventlog_sequential_read | eventlog_backwards_read,
1777 0,
1778 heap_deleter.get(),
1779 dwBytesToRead,
1780 &dwBytesRead,
1781 &dwMinimumBytesToRead)) {
1782 status = get_last_error();
1783 if (error_insufficient_buffer == status) {
1784 status = 0;
1785 dwBytesToRead = dwMinimumBytesToRead;
1786 heap_deleter.realloc_mem(dwMinimumBytesToRead);
1787 if (!heap_deleter.get()){
1788 return false;
1789 }
1790 }
1791 else{ //Not found or EOF
1792 return false;
1793 }
1794 }
1795 else
1796 {
1797 interprocess_eventlogrecord *pTypedRecord;
1798 // Print the contents of each record in the buffer.
1799 if(find_record_in_buffer(heap_deleter.get(), dwBytesRead, provider_name, event_id, pTypedRecord)){
1800 char stamp_str[sizeof(unsigned long)*3+1];
1801 std::sprintf(&stamp_str[0], "%u", ((unsigned int)pTypedRecord->TimeGenerated));
1802 stamp = stamp_str;
1803 break;
1804 }
1805 }
1806 }
1807 }
1808 }
1809 return true;
1810 }
1811
1812
1813 inline bool get_last_bootup_time(std::wstring &stamp)
1814 {
1815 const wchar_t *source_name = L"System";
1816 const wchar_t *provider_name = L"EventLog";
1817 const unsigned short event_id = 6005u;
1818
1819 unsigned long status = 0;
1820 unsigned long dwBytesToRead = 0;
1821 unsigned long dwBytesRead = 0;
1822 unsigned long dwMinimumBytesToRead = 0;
1823
1824 // The source name (provider) must exist as a subkey of Application.
1825 void *hEventLog = OpenEventLogW(0, source_name);
1826 if (hEventLog){
1827 eventlog_handle_closer hnd_closer(hEventLog); (void)hnd_closer;
1828 // Allocate an initial block of memory used to read event records. The number
1829 // of records read into the buffer will vary depending on the size of each event.
1830 // The size of each event will vary based on the size of the user-defined
1831 // data included with each event, the number and length of insertion
1832 // strings, and other data appended to the end of the event record.
1833 dwBytesToRead = max_record_buffer_size;
1834 c_heap_deleter heap_deleter(dwBytesToRead);
1835
1836 // Read blocks of records until you reach the end of the log or an
1837 // error occurs. The records are read from newest to oldest. If the buffer
1838 // is not big enough to hold a complete event record, reallocate the buffer.
1839 if (heap_deleter.get() != 0){
1840 while (0 == status){
1841 if (!ReadEventLogW(hEventLog,
1842 eventlog_sequential_read | eventlog_backwards_read,
1843 0,
1844 heap_deleter.get(),
1845 dwBytesToRead,
1846 &dwBytesRead,
1847 &dwMinimumBytesToRead)) {
1848 status = get_last_error();
1849 if (error_insufficient_buffer == status) {
1850 status = 0;
1851 dwBytesToRead = dwMinimumBytesToRead;
1852 heap_deleter.realloc_mem(dwMinimumBytesToRead);
1853 if (!heap_deleter.get()){
1854 return false;
1855 }
1856 }
1857 else{ //Not found or EOF
1858 return false;
1859 }
1860 }
1861 else
1862 {
1863 interprocess_eventlogrecord *pTypedRecord;
1864 // Print the contents of each record in the buffer.
1865 if(find_record_in_buffer(heap_deleter.get(), dwBytesRead, provider_name, event_id, pTypedRecord)){
1866 wchar_t stamp_str[sizeof(unsigned long)*3+1];
1867 std::swprintf(&stamp_str[0], L"%u", ((unsigned int)pTypedRecord->TimeGenerated));
1868 stamp = stamp_str;
1869 break;
1870 }
1871 }
1872 }
1873 }
1874 }
1875 return true;
1876 }
1877
1878 #endif //BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
1879
1880
1881 } //namespace winapi
1882 } //namespace interprocess
1883 } //namespace boost
1884
1885 #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
1886 # pragma GCC diagnostic pop
1887 #endif
1888
1889 #include <boost/interprocess/detail/config_end.hpp>
1890
1891 #endif //#ifdef BOOST_INTERPROCESS_WIN32_API_HPP