Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:28

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_WIN32_API_HPP
0012 #define BOOST_INTERPROCESS_WIN32_API_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 #include <boost/cstdint.hpp>
0025 #include <cstddef>
0026 #include <cstring>
0027 #include <cstdlib>
0028 #include <cstdio>
0029 
0030 #include <boost/assert.hpp>
0031 #include <string>
0032 #include <vector>
0033 
0034 #ifdef BOOST_USE_WINDOWS_H
0035 #include <windows.h>
0036 #endif
0037 
0038 #if defined(_MSC_VER)
0039 #  pragma once
0040 #  pragma comment( lib, "Advapi32.lib" )
0041 #  pragma comment( lib, "oleaut32.lib" )
0042 #  pragma comment( lib, "Ole32.lib" )
0043 #endif
0044 
0045 #if defined (BOOST_INTERPROCESS_WINDOWS)
0046 #  include <cstdarg>
0047 #  include <boost/detail/interlocked.hpp>
0048 #else
0049 # error "This file can only be included in Windows OS"
0050 #endif
0051 
0052 //////////////////////////////////////////////////////////////////////////////
0053 //
0054 // Declaration of Windows structures or typedefs if BOOST_USE_WINDOWS_H is used
0055 //
0056 //////////////////////////////////////////////////////////////////////////////
0057 
0058 
0059 #if defined(BOOST_GCC)
0060 //Ignore -pedantic errors here (anonymous structs, etc.)
0061 #  if (BOOST_GCC >= 40600)
0062 #     pragma GCC diagnostic push
0063 #     if (BOOST_GCC >= 40800)
0064 #        pragma GCC diagnostic ignored "-Wpedantic"
0065 #     else
0066 #        pragma GCC diagnostic ignored "-pedantic"
0067 #     endif
0068 #     pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
0069 #  else
0070 #     pragma GCC system_header
0071 #  endif
0072 //When loading DLLs we have no option but reinterpret casting function types  
0073 #  if (BOOST_GCC >= 80000)
0074 #        pragma GCC diagnostic ignored "-Wcast-function-type"
0075 #  endif
0076 #endif
0077 
0078 
0079 //#define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
0080 //#define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
0081 
0082 #ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
0083 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE 1 
0084 #else
0085 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE 0
0086 #endif
0087 
0088 #ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
0089 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE 1 
0090 #else
0091 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE 0
0092 #endif
0093 
0094 #define BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM \
0095    (BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE + \
0096     BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE)
0097 
0098 #if 1 < BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM
0099 #  error "Only one of \
0100           BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED and \
0101           BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED can be defined"
0102 #endif
0103 
0104 #if 0 == BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM
0105 #  define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
0106 #endif
0107 
0108 
0109 namespace boost  {
0110 namespace interprocess  {
0111 namespace winapi {
0112 
0113 //Own defines
0114 static const unsigned long MaxPath           = 260;
0115 
0116 //////////////////////////////////////////////////////////////////////////////
0117 //
0118 // Nt native structures
0119 //
0120 //////////////////////////////////////////////////////////////////////////////
0121 
0122 struct interprocess_semaphore_basic_information
0123 {
0124    unsigned long count;      // current semaphore count
0125    unsigned long limit;      // max semaphore count
0126 };
0127 
0128 struct interprocess_section_basic_information
0129 {
0130   void *          base_address;
0131   unsigned long   section_attributes;
0132   __int64         section_size;
0133 };
0134 
0135 struct file_rename_information_t {
0136    int Replace;
0137    void *RootDir;
0138    unsigned long FileNameLength;
0139    wchar_t FileName[1];
0140 };
0141 
0142 struct unicode_string_t {
0143    unsigned short Length;
0144    unsigned short MaximumLength;
0145    wchar_t *Buffer;
0146 };
0147 
0148 struct object_attributes_t {
0149    unsigned long Length;
0150    void * RootDirectory;
0151    unicode_string_t *ObjectName;
0152    unsigned long Attributes;
0153    void *SecurityDescriptor;
0154    void *SecurityQualityOfService;
0155 };
0156 
0157 struct io_status_block_t {
0158    union {
0159       long Status;
0160       void *Pointer;
0161    };
0162 
0163    unsigned long *Information;
0164 };
0165 
0166 union system_timeofday_information
0167 {
0168    struct data_t
0169    {
0170       __int64 liKeBootTime;
0171       __int64 liKeSystemTime;
0172       __int64 liExpTimeZoneBias;
0173       unsigned long uCurrentTimeZoneId;
0174       unsigned long dwReserved;
0175       ::boost::ulong_long_type ullBootTimeBias;
0176       ::boost::ulong_long_type ullSleepTimeBias;
0177    } data;
0178    unsigned char Reserved1[sizeof(data_t)];
0179 };
0180 
0181 static const long BootstampLength            = sizeof(__int64);
0182 static const long BootAndSystemstampLength   = sizeof(__int64)*2;
0183 static const long SystemTimeOfDayInfoLength  = sizeof(system_timeofday_information::data_t);
0184 
0185 struct object_name_information_t
0186 {
0187    unicode_string_t Name;
0188    wchar_t NameBuffer[1];
0189 };
0190 
0191 enum file_information_class_t {
0192    file_directory_information = 1,
0193    file_full_directory_information,
0194    file_both_directory_information,
0195    file_basic_information,
0196    file_standard_information,
0197    file_internal_information,
0198    file_ea_information,
0199    file_access_information,
0200    file_name_information,
0201    file_rename_information,
0202    file_link_information,
0203    file_names_information,
0204    file_disposition_information,
0205    file_position_information,
0206    file_full_ea_information,
0207    file_mode_information,
0208    file_alignment_information,
0209    file_all_information,
0210    file_allocation_information,
0211    file_end_of_file_information,
0212    file_alternate_name_information,
0213    file_stream_information,
0214    file_pipe_information,
0215    file_pipe_local_information,
0216    file_pipe_remote_information,
0217    file_mailslot_query_information,
0218    file_mailslot_set_information,
0219    file_compression_information,
0220    file_copy_on_write_information,
0221    file_completion_information,
0222    file_move_cluster_information,
0223    file_quota_information,
0224    file_reparse_point_information,
0225    file_network_open_information,
0226    file_object_id_information,
0227    file_tracking_information,
0228    file_ole_directory_information,
0229    file_content_index_information,
0230    file_inherit_content_index_information,
0231    file_ole_information,
0232    file_maximum_information
0233 };
0234 
0235 enum semaphore_information_class {
0236    semaphore_basic_information = 0
0237 };
0238 
0239 
0240 enum system_information_class {
0241    system_basic_information = 0,
0242    system_performance_information = 2,
0243    system_time_of_day_information = 3,
0244    system_process_information = 5,
0245    system_processor_performance_information = 8,
0246    system_interrupt_information = 23,
0247    system_exception_information = 33,
0248    system_registry_quota_information = 37,
0249    system_lookaside_information = 45
0250 };
0251 
0252 enum object_information_class
0253 {
0254    object_basic_information,
0255    object_name_information,
0256    object_type_information,
0257    object_all_information,
0258    object_data_information
0259 };
0260 
0261 enum section_information_class
0262 {
0263    section_basic_information,
0264    section_image_information
0265 };
0266 
0267 }  //namespace winapi {
0268 }  //namespace interprocess  {
0269 }  //namespace boost  {
0270 
0271 
0272 //////////////////////////////////////////////////////////////////////////////
0273 //
0274 // Forward declaration of winapi
0275 //
0276 //////////////////////////////////////////////////////////////////////////////
0277 
0278 #include <boost/winapi/get_current_process_id.hpp>
0279 #include <boost/winapi/get_current_thread_id.hpp>
0280 #include <boost/winapi/get_current_process.hpp>
0281 #include <boost/winapi/get_process_times.hpp>
0282 #include <boost/winapi/error_codes.hpp>
0283 #include <boost/winapi/thread.hpp>
0284 #include <boost/winapi/system.hpp>
0285 #include <boost/winapi/time.hpp>
0286 #include <boost/winapi/timers.hpp>
0287 #include <boost/winapi/get_last_error.hpp>
0288 #include <boost/winapi/handles.hpp>
0289 #include <boost/winapi/file_management.hpp>
0290 #include <boost/winapi/mutex.hpp>
0291 #include <boost/winapi/wait.hpp>
0292 #include <boost/winapi/file_mapping.hpp>
0293 #include <boost/winapi/semaphore.hpp>
0294 #include <boost/winapi/system.hpp>
0295 #include <boost/winapi/error_handling.hpp>
0296 #include <boost/winapi/local_memory.hpp>
0297 #include <boost/winapi/directory_management.hpp>
0298 #include <boost/winapi/security.hpp>
0299 #include <boost/winapi/dll.hpp>
0300 #include <boost/winapi/basic_types.hpp>
0301 
0302 //This should go in winapi's basic_types.hpp 
0303 namespace boost {
0304 namespace ipwinapiext {
0305 typedef boost::winapi::LONG_ LSTATUS;
0306 
0307 typedef boost::winapi::DWORD_ (__stdcall *LPTHREAD_START_ROUTINE_)
0308    (boost::winapi::LPVOID_ lpThreadParameter);
0309 
0310 
0311 //#ifndef BOOST_USE_WINDOWS_H
0312 //typedef boost::winapi::LARGE_INTEGER_ LARGE_INTEGER_EXT;
0313 //#else
0314 //typedef LARGE_INTEGER LARGE_INTEGER_EXT;
0315 //#endif
0316 
0317 }} //namespace boost::ipwinapiext
0318 
0319 #ifndef BOOST_USE_WINDOWS_H
0320 
0321 extern "C" {
0322 
0323 //Error handling
0324 BOOST_SYMBOL_IMPORT BOOST_WINAPI_DETAIL_VOID BOOST_WINAPI_WINAPI_CC SetLastError(boost::winapi::DWORD_ dwErrCode);
0325 
0326 //File management
0327 BOOST_SYMBOL_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC GetFileType(boost::winapi::HANDLE_ hTemplateFile);
0328 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC FlushFileBuffers(boost::winapi::HANDLE_ hFile);
0329 
0330 //threading
0331 
0332 BOOST_SYMBOL_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC CreateThread
0333    ( ::_SECURITY_ATTRIBUTES* lpThreadAttributes
0334    , boost::winapi::SIZE_T_ dwStackSize
0335    , boost::ipwinapiext::LPTHREAD_START_ROUTINE_ lpStartAddress
0336    , boost::winapi::LPVOID_ lpParameter
0337    , boost::winapi::DWORD_ dwCreationFlags
0338    , boost::winapi::LPDWORD_ lpThreadId
0339    );
0340 
0341 //Virtual Memory
0342 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualLock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize);
0343 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualUnlock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize);
0344 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC VirtualProtect( boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize
0345                                                                               , boost::winapi::DWORD_ flNewProtect, boost::winapi::PDWORD_ lpflOldProtect);
0346 //registry.hpp
0347 BOOST_WINAPI_DETAIL_DECLARE_HANDLE(HKEY);
0348 
0349 
0350 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegOpenKeyExA
0351    (::HKEY hKey, const char *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, ::HKEY *phkResult);
0352 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegOpenKeyExW
0353    (::HKEY hKey, const wchar_t *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, ::HKEY *phkResult);
0354 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegQueryValueExA
0355    (::HKEY hKey, const char *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData);
0356 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegQueryValueExW
0357    (::HKEY hKey, const wchar_t *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData);
0358 BOOST_SYMBOL_IMPORT boost::ipwinapiext::LSTATUS BOOST_WINAPI_WINAPI_CC RegCloseKey(::HKEY hKey);
0359 
0360 
0361 //Event Log
0362 BOOST_SYMBOL_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC OpenEventLogA(const char* lpUNCServerName, const char* lpSourceName);
0363 BOOST_SYMBOL_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC OpenEventLogW(const wchar_t* lpUNCServerName, const wchar_t* lpSourceName);
0364 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_   BOOST_WINAPI_WINAPI_CC CloseEventLog(boost::winapi::HANDLE_ hEventLog);
0365 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_   BOOST_WINAPI_WINAPI_CC ReadEventLogA
0366    ( boost::winapi::HANDLE_ hEventLog, boost::winapi::DWORD_ dwReadFlags, boost::winapi::DWORD_ dwRecordOffset, void* lpBuffer
0367    , boost::winapi::DWORD_ nNumberOfBytesToRead, boost::winapi::DWORD_ *pnBytesRead, boost::winapi::DWORD_ *pnMinNumberOfBytesNeeded);
0368 BOOST_SYMBOL_IMPORT boost::winapi::BOOL_   BOOST_WINAPI_WINAPI_CC ReadEventLogW
0369    ( boost::winapi::HANDLE_ hEventLog, boost::winapi::DWORD_ dwReadFlags, boost::winapi::DWORD_ dwRecordOffset, void* lpBuffer
0370    , boost::winapi::DWORD_ nNumberOfBytesToRead, boost::winapi::DWORD_ *pnBytesRead, boost::winapi::DWORD_ *pnMinNumberOfBytesNeeded); 
0371 
0372 }  //extern "C" {
0373 
0374 #endif   //#ifndef BOOST_USE_WINDOWS_H
0375 
0376 namespace boost {
0377 namespace ipwinapiext {
0378 
0379 typedef ::HKEY HKEY_;
0380 
0381 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
0382 
0383 //Threads
0384 BOOST_FORCEINLINE boost::winapi::HANDLE_ CreateThread
0385    ( boost::winapi::SECURITY_ATTRIBUTES_* lpThreadAttributes
0386    , boost::winapi::SIZE_T_ dwStackSize
0387    , boost::ipwinapiext::LPTHREAD_START_ROUTINE_ lpStartAddress
0388    , boost::winapi::LPVOID_ lpParameter
0389    , boost::winapi::DWORD_ dwCreationFlags
0390    , boost::winapi::LPDWORD_ lpThreadId
0391 )
0392 {
0393    return ::CreateThread( reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpThreadAttributes)
0394                         , dwStackSize, lpStartAddress
0395                         , lpParameter, dwCreationFlags, lpThreadId);
0396 }
0397 
0398 //Error handling
0399 BOOST_FORCEINLINE BOOST_WINAPI_DETAIL_VOID SetLastError(boost::winapi::DWORD_ dwErrCode)
0400 {  ::SetLastError(dwErrCode); }
0401 
0402 //File management
0403 BOOST_FORCEINLINE boost::winapi::DWORD_ GetFileType(boost::winapi::HANDLE_ hTemplateFile)
0404 {  return ::GetFileType(hTemplateFile);   }
0405 
0406 BOOST_FORCEINLINE boost::winapi::BOOL_ FlushFileBuffers(boost::winapi::HANDLE_ hFile)
0407 {  return ::FlushFileBuffers(hFile);   }
0408 
0409 //Virtual Memory
0410 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualLock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize)
0411 {  return ::VirtualLock(lpAddress, dwSize);  }
0412 
0413 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualUnlock(boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize)
0414 {  return ::VirtualUnlock(lpAddress, dwSize);   }
0415 
0416 BOOST_FORCEINLINE boost::winapi::BOOL_ VirtualProtect( boost::winapi::LPVOID_ lpAddress, boost::winapi::SIZE_T_ dwSize
0417                                                      , boost::winapi::DWORD_ flNewProtect, boost::winapi::PDWORD_ lpflOldProtect)
0418 {  return ::VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect);  }
0419 
0420 //registry.hpp
0421 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegOpenKeyExA
0422    (boost::ipwinapiext::HKEY_ hKey, const char *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, boost::ipwinapiext::HKEY_ *phkResult)
0423 {
0424    return ::RegOpenKeyExA(reinterpret_cast< ::HKEY >(hKey), lpSubKey, ulOptions, samDesired, reinterpret_cast< ::HKEY* >(phkResult));
0425 }
0426 
0427 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegOpenKeyExW
0428    (boost::ipwinapiext::HKEY_ hKey, const wchar_t *lpSubKey, boost::winapi::DWORD_ ulOptions, boost::winapi::DWORD_ samDesired, boost::ipwinapiext::HKEY_ *phkResult)
0429 {
0430    return ::RegOpenKeyExW(reinterpret_cast< ::HKEY >(hKey), lpSubKey, ulOptions, samDesired, reinterpret_cast< ::HKEY* >(phkResult));
0431 }
0432 
0433 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegQueryValueExA
0434    (boost::ipwinapiext::HKEY_ hKey, const char *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData)
0435 {
0436    return ::RegQueryValueExA(reinterpret_cast< ::HKEY >(hKey), lpValueName, lpReserved, lpType, lpData, lpcbData);
0437 }
0438 
0439 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegQueryValueExW
0440    (boost::ipwinapiext::HKEY_ hKey, const wchar_t *lpValueName, boost::winapi::DWORD_ *lpReserved, boost::winapi::DWORD_ *lpType, boost::winapi::BYTE_ *lpData, boost::winapi::DWORD_ *lpcbData)
0441 {
0442    return ::RegQueryValueExW(reinterpret_cast< ::HKEY >(hKey), lpValueName, lpReserved, lpType, lpData, lpcbData);
0443 }
0444 
0445 BOOST_FORCEINLINE boost::ipwinapiext::LSTATUS RegCloseKey(boost::ipwinapiext::HKEY_ hKey)
0446 {
0447    return ::RegCloseKey(reinterpret_cast< ::HKEY >(hKey));
0448 }
0449 
0450 BOOST_FORCEINLINE void GetSystemInfo(boost::winapi::LPSYSTEM_INFO_ lpSystemInfo)
0451 {  return ::GetSystemInfo(reinterpret_cast< ::_SYSTEM_INFO* >(lpSystemInfo));   }
0452 
0453 #endif   //BOOST_WINAPI_PARTITION_APP_SYSTEM
0454 
0455 }  //namespace ipwinapiext {
0456 }  //namespace boost {
0457 
0458 namespace boost  {
0459 namespace interprocess  {
0460 namespace winapi {
0461 
0462 typedef boost::winapi::SYSTEM_INFO_ interprocess_system_info;
0463 typedef boost::winapi::OVERLAPPED_ interprocess_overlapped;
0464 typedef boost::winapi::FILETIME_ interprocess_filetime;
0465 typedef boost::winapi::WIN32_FIND_DATAA_ win32_find_data_a;
0466 typedef boost::winapi::WIN32_FIND_DATAW_ win32_find_data_w;
0467 typedef boost::winapi::SECURITY_ATTRIBUTES_ interprocess_security_attributes;
0468 typedef boost::winapi::SECURITY_DESCRIPTOR_ interprocess_security_descriptor;
0469 typedef boost::winapi::BY_HANDLE_FILE_INFORMATION_ interprocess_by_handle_file_information;
0470 typedef boost::winapi::HMODULE_ hmodule;
0471 typedef boost::ipwinapiext::HKEY_ hkey;
0472 typedef boost::winapi::FARPROC_ farproc_t;
0473 
0474 //ntdll.dll
0475 typedef long (__stdcall *NtDeleteFile_t)(object_attributes_t *ObjectAttributes);
0476 typedef long (__stdcall *NtSetInformationFile_t)(void *FileHandle, io_status_block_t *IoStatusBlock, void *FileInformation, unsigned long Length, int FileInformationClass );
0477 typedef long (__stdcall *NtOpenFile)(void **FileHandle, unsigned long DesiredAccess, object_attributes_t *ObjectAttributes
0478                                     , io_status_block_t *IoStatusBlock, unsigned long ShareAccess, unsigned long Length, unsigned long OpenOptions);
0479 typedef long (__stdcall *NtQuerySystemInformation_t)(int, void*, unsigned long, unsigned long *);
0480 typedef long (__stdcall *NtQueryObject_t)(void*, object_information_class, void *, unsigned long, unsigned long *);
0481 typedef long (__stdcall *NtQuerySemaphore_t)(void*, unsigned int info_class, interprocess_semaphore_basic_information *pinfo, unsigned int info_size, unsigned int *ret_len);
0482 typedef long (__stdcall *NtQuerySection_t)(void*, section_information_class, interprocess_section_basic_information *pinfo, unsigned long info_size, unsigned long *ret_len);
0483 typedef long (__stdcall *NtQueryInformationFile_t)(void *,io_status_block_t *,void *, long, int);
0484 typedef long (__stdcall *NtOpenFile_t)(void*,unsigned long ,object_attributes_t*,io_status_block_t*,unsigned long,unsigned long);
0485 typedef long (__stdcall *NtClose_t) (void*);
0486 typedef long (__stdcall *NtQueryTimerResolution_t) (unsigned long* LowestResolution, unsigned long* HighestResolution, unsigned long* CurrentResolution);
0487 typedef long (__stdcall *NtSetTimerResolution_t) (unsigned long RequestedResolution, int Set, unsigned long* ActualResolution);
0488 
0489 }  //namespace winapi {
0490 }  //namespace interprocess  {
0491 }  //namespace boost  {
0492 
0493 //////////////////////////////////////////////////////////////////////////////
0494 //
0495 // Forward declaration of constants
0496 //
0497 //////////////////////////////////////////////////////////////////////////////
0498 
0499 namespace boost {
0500 namespace interprocess {
0501 namespace winapi {
0502 
0503 //Some used constants
0504 static const unsigned long infinite_time        = 0xFFFFFFFF;
0505 static const unsigned long error_already_exists = 183L;
0506 static const unsigned long error_invalid_handle = 6L;
0507 static const unsigned long error_sharing_violation = 32L;
0508 static const unsigned long error_file_not_found = 2u;
0509 static const unsigned long error_no_more_files  = 18u;
0510 static const unsigned long error_not_locked     = 158L;
0511 //Retries in CreateFile, see http://support.microsoft.com/kb/316609
0512 static const unsigned long error_sharing_violation_tries = 3L;
0513 static const unsigned long error_sharing_violation_sleep_ms = 250L;
0514 static const unsigned long error_file_too_large = 223L;
0515 static const unsigned long error_insufficient_buffer = 122L;
0516 static const unsigned long error_handle_eof = 38L;
0517 static const unsigned long semaphore_all_access = (0x000F0000L)|(0x00100000L)|0x3;
0518 static const unsigned long mutex_all_access     = (0x000F0000L)|(0x00100000L)|0x0001;
0519 
0520 static const unsigned long page_readonly        = 0x02;
0521 static const unsigned long page_readwrite       = 0x04;
0522 static const unsigned long page_writecopy       = 0x08;
0523 static const unsigned long page_noaccess        = 0x01;
0524 
0525 static const unsigned long standard_rights_required   = 0x000F0000L;
0526 static const unsigned long section_query              = 0x0001;
0527 static const unsigned long section_map_write          = 0x0002;
0528 static const unsigned long section_map_read           = 0x0004;
0529 static const unsigned long section_map_execute        = 0x0008;
0530 static const unsigned long section_extend_size        = 0x0010;
0531 static const unsigned long section_all_access         = standard_rights_required |
0532                                                         section_query            |
0533                                                         section_map_write        |
0534                                                         section_map_read         |
0535                                                         section_map_execute      |
0536                                                         section_extend_size;
0537 
0538 static const unsigned long file_map_copy        = section_query;
0539 static const unsigned long file_map_write       = section_map_write;
0540 static const unsigned long file_map_read        = section_map_read;
0541 static const unsigned long file_map_all_access  = section_all_access;
0542 static const unsigned long delete_access = 0x00010000L;
0543 static const unsigned long file_flag_backup_semantics = 0x02000000;
0544 static const long file_flag_delete_on_close = 0x04000000;
0545 
0546 //Native API constants
0547 static const unsigned long file_open_for_backup_intent = 0x00004000;
0548 static const int file_share_valid_flags = 0x00000007;
0549 static const long file_delete_on_close = 0x00001000L;
0550 static const long obj_case_insensitive = 0x00000040L;
0551 static const long delete_flag = 0x00010000L;
0552 
0553 static const unsigned long movefile_copy_allowed            = 0x02;
0554 static const unsigned long movefile_delay_until_reboot      = 0x04;
0555 static const unsigned long movefile_replace_existing        = 0x01;
0556 static const unsigned long movefile_write_through           = 0x08;
0557 static const unsigned long movefile_create_hardlink         = 0x10;
0558 static const unsigned long movefile_fail_if_not_trackable   = 0x20;
0559 
0560 static const unsigned long file_share_read      = 0x00000001;
0561 static const unsigned long file_share_write     = 0x00000002;
0562 static const unsigned long file_share_delete    = 0x00000004;
0563 
0564 static const unsigned long file_attribute_readonly    = 0x00000001;
0565 static const unsigned long file_attribute_hidden      = 0x00000002;
0566 static const unsigned long file_attribute_system      = 0x00000004;
0567 static const unsigned long file_attribute_directory   = 0x00000010;
0568 static const unsigned long file_attribute_archive     = 0x00000020;
0569 static const unsigned long file_attribute_device      = 0x00000040;
0570 static const unsigned long file_attribute_normal      = 0x00000080;
0571 static const unsigned long file_attribute_temporary   = 0x00000100;
0572 
0573 static const unsigned long generic_read         = 0x80000000L;
0574 static const unsigned long generic_write        = 0x40000000L;
0575 
0576 static const unsigned long wait_object_0        = 0;
0577 static const unsigned long wait_abandoned       = 0x00000080L;
0578 static const unsigned long wait_timeout         = 258L;
0579 static const unsigned long wait_failed          = (unsigned long)0xFFFFFFFF;
0580 
0581 static const unsigned long duplicate_close_source  = (unsigned long)0x00000001;
0582 static const unsigned long duplicate_same_access   = (unsigned long)0x00000002;
0583 
0584 static const unsigned long format_message_allocate_buffer
0585    = (unsigned long)0x00000100;
0586 static const unsigned long format_message_ignore_inserts
0587    = (unsigned long)0x00000200;
0588 static const unsigned long format_message_from_string
0589    = (unsigned long)0x00000400;
0590 static const unsigned long format_message_from_hmodule
0591    = (unsigned long)0x00000800;
0592 static const unsigned long format_message_from_system
0593    = (unsigned long)0x00001000;
0594 static const unsigned long format_message_argument_array
0595    = (unsigned long)0x00002000;
0596 static const unsigned long format_message_max_width_mask
0597    = (unsigned long)0x000000FF;
0598 static const unsigned long lang_neutral         = (unsigned long)0x00;
0599 static const unsigned long sublang_default      = (unsigned long)0x01;
0600 static const unsigned long invalid_file_size    = (unsigned long)0xFFFFFFFF;
0601 static const unsigned long invalid_file_attributes =  ((unsigned long)-1);
0602 static       void * const  invalid_handle_value = ((void*)(long)(-1));
0603 
0604 static const unsigned long file_type_char    =  0x0002L;
0605 static const unsigned long file_type_disk    =  0x0001L;
0606 static const unsigned long file_type_pipe    =  0x0003L;
0607 static const unsigned long file_type_remote  =  0x8000L;
0608 static const unsigned long file_type_unknown =  0x0000L;
0609 
0610 static const unsigned long create_new        = 1;
0611 static const unsigned long create_always     = 2;
0612 static const unsigned long open_existing     = 3;
0613 static const unsigned long open_always       = 4;
0614 static const unsigned long truncate_existing = 5;
0615 
0616 static const unsigned long file_begin     = 0;
0617 static const unsigned long file_current   = 1;
0618 static const unsigned long file_end       = 2;
0619 
0620 static const unsigned long lockfile_fail_immediately  = 1;
0621 static const unsigned long lockfile_exclusive_lock    = 2;
0622 static const unsigned long error_lock_violation       = 33;
0623 static const unsigned long security_descriptor_revision = 1;
0624 
0625 const unsigned long max_record_buffer_size = 0x10000L;   // 64K
0626 const unsigned long max_path = 260;
0627 
0628 //Keys
0629 static const  hkey hkey_local_machine = (hkey)(unsigned long*)(long)(0x80000002);
0630 static unsigned long key_query_value    = 0x0001;
0631 
0632 // Registry types
0633 #define reg_none                       ( 0 )   // No value type
0634 #define reg_sz                         ( 1 )   // Unicode nul terminated string
0635 #define reg_expand_sz                  ( 2 )   // Unicode nul terminated string
0636                                                // (with environment variable references)
0637 #define reg_binary                     ( 3 )   // Free form binary
0638 #define reg_dword                      ( 4 )   // 32-bit number
0639 #define reg_dword_little_endian        ( 4 )   // 32-bit number (same as REG_DWORD)
0640 #define reg_dword_big_endian           ( 5 )   // 32-bit number
0641 #define reg_link                       ( 6 )   // Symbolic Link (unicode)
0642 #define reg_multi_sz                   ( 7 )   // Multiple Unicode strings
0643 #define reg_resource_list              ( 8 )   // Resource list in the resource map
0644 #define reg_full_resource_descriptor   ( 9 )  // Resource list in the hardware description
0645 #define reg_resource_requirements_list ( 10 )
0646 #define reg_qword                      ( 11 )  // 64-bit number
0647 #define reg_qword_little_endian        ( 11 )  // 64-bit number (same as reg_qword)
0648 
0649 
0650 }  //namespace winapi {
0651 }  //namespace interprocess  {
0652 }  //namespace boost  {
0653 
0654 
0655 namespace boost {
0656 namespace interprocess {
0657 namespace winapi {
0658 
0659 inline unsigned long get_last_error()
0660 {  return GetLastError();  }
0661 
0662 inline void set_last_error(unsigned long err)
0663 {  return SetLastError(err);  }
0664 
0665 inline unsigned long format_message
0666    (unsigned long dwFlags, const void *lpSource,
0667     unsigned long dwMessageId, unsigned long dwLanguageId,
0668     char *lpBuffer, unsigned long nSize, std::va_list *Arguments)
0669 {
0670    return FormatMessageA
0671       (dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments);
0672 }
0673 
0674 //And now, wrapper functions
0675 inline void * local_free(void *hmem)
0676 {  return LocalFree(hmem); }
0677 
0678 inline unsigned long make_lang_id(unsigned long p, unsigned long s)
0679 {  
0680    const unsigned short s_us = (unsigned short)s;
0681    const unsigned short p_us = (unsigned short)p;
0682    return (unsigned long)((s_us << 10) | p_us);
0683 }
0684 
0685 inline void sched_yield()
0686 {
0687    if(!SwitchToThread()){
0688       Sleep(0);
0689    }
0690 }
0691 
0692 inline void sleep_tick()
0693 {  Sleep(1);   }
0694 
0695 inline void sleep(unsigned long ms)
0696 {  Sleep(ms);  }
0697 
0698 inline unsigned long get_current_thread_id()
0699 {  return GetCurrentThreadId();  }
0700 
0701 inline bool get_process_times
0702    ( void *hProcess, interprocess_filetime* lpCreationTime
0703    , interprocess_filetime *lpExitTime, interprocess_filetime *lpKernelTime
0704    , interprocess_filetime *lpUserTime )
0705 {  return 0 != GetProcessTimes(hProcess, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime); }
0706 
0707 inline unsigned long get_current_process_id()
0708 {  return GetCurrentProcessId();  }
0709 
0710 inline bool close_handle(void* handle)
0711 {  return CloseHandle(handle) != 0;   }
0712 
0713 inline void * find_first_file(const char *lpFileName, win32_find_data_a *lpFindFileData)
0714 {  return FindFirstFileA(lpFileName, lpFindFileData);   }
0715 
0716 inline void * find_first_file(const wchar_t *lpFileName, win32_find_data_w *lpFindFileData)
0717 {  return FindFirstFileW(lpFileName, lpFindFileData);   }
0718 
0719 inline bool find_next_file(void *hFindFile, win32_find_data_a *lpFindFileData)
0720 {  return FindNextFileA(hFindFile, lpFindFileData) != 0;   }
0721 
0722 inline bool find_next_file(void *hFindFile, win32_find_data_w *lpFindFileData)
0723 {  return FindNextFileW(hFindFile, lpFindFileData) != 0;   }
0724 
0725 inline bool find_close(void *handle)
0726 {  return FindClose(handle) != 0;   }
0727 
0728 inline bool duplicate_current_process_handle
0729    (void *hSourceHandle, void **lpTargetHandle)
0730 {
0731    return 0 != DuplicateHandle
0732       ( GetCurrentProcess(),  hSourceHandle,    GetCurrentProcess()
0733       , lpTargetHandle,       0,                0
0734       , duplicate_same_access);
0735 }
0736 
0737 inline unsigned long get_file_type(void *hFile)
0738 {
0739    return GetFileType(hFile);
0740 }
0741 
0742 /*
0743 inline void get_system_time_as_file_time(interprocess_filetime *filetime)
0744 {  GetSystemTimeAsFileTime(filetime);  }
0745 
0746 inline bool file_time_to_local_file_time
0747    (const interprocess_filetime *in, const interprocess_filetime *out)
0748 {  return 0 != FileTimeToLocalFileTime(in, out);  }
0749 */
0750 inline void *open_or_create_mutex(const char *name, bool initial_owner, interprocess_security_attributes *attr)
0751 {  return CreateMutexA(attr, (int)initial_owner, name);  }
0752 
0753 inline void *open_or_create_mutex(const wchar_t *name, bool initial_owner, interprocess_security_attributes *attr)
0754 {  return CreateMutexW(attr, (int)initial_owner, name);  }
0755 
0756 inline unsigned long wait_for_single_object(void *handle, unsigned long time)
0757 {  return WaitForSingleObject(handle, time); }
0758 
0759 inline int release_mutex(void *handle)
0760 {  return ReleaseMutex(handle);  }
0761 
0762 inline int unmap_view_of_file(void *address)
0763 {  return UnmapViewOfFile(address); }
0764 
0765 inline void *open_or_create_semaphore(const char *name, long initial_count, long maximum_count, interprocess_security_attributes *attr)
0766 {  return CreateSemaphoreA(attr, initial_count, maximum_count, name);  }
0767 
0768 inline void *open_or_create_semaphore(const wchar_t *name, long initial_count, long maximum_count, interprocess_security_attributes *attr)
0769 {  return CreateSemaphoreW(attr, initial_count, maximum_count, name);  }
0770 
0771 inline void *open_semaphore(const char *name)
0772 {  return OpenSemaphoreA(semaphore_all_access, 0, name);  }
0773 
0774 inline void *open_semaphore(const wchar_t *name)
0775 {  return OpenSemaphoreW(semaphore_all_access, 0, name);  }
0776 
0777 inline int release_semaphore(void *handle, long release_count, long *prev_count)
0778 {  return ReleaseSemaphore(handle, release_count, prev_count); }
0779 
0780 class interprocess_all_access_security
0781 {
0782    interprocess_security_attributes sa;
0783    interprocess_security_descriptor sd;
0784    bool initialized;
0785 
0786    public:
0787    interprocess_all_access_security()
0788       : initialized(false)
0789    {
0790       if(!boost::winapi::InitializeSecurityDescriptor(&sd, security_descriptor_revision))
0791          return;
0792       if(!boost::winapi::SetSecurityDescriptorDacl(&sd, true, 0, false))
0793          return;
0794       sa.lpSecurityDescriptor = &sd;
0795       sa.nLength = sizeof(interprocess_security_attributes);
0796       sa.bInheritHandle = false;
0797       initialized = true;
0798    }
0799 
0800    interprocess_security_attributes *get_attributes()
0801    {  return &sa; }
0802 };
0803 
0804 inline void * create_file_mapping (void * handle, unsigned long access, ::boost::ulong_long_type file_offset, const char * name, interprocess_security_attributes *psec)
0805 {
0806    const boost::winapi::DWORD_ high_size = boost::winapi::DWORD_(file_offset >> 32);
0807    const boost::winapi::DWORD_ low_size  = boost::winapi::DWORD_(file_offset);
0808    return CreateFileMappingA (handle, psec, access, high_size, low_size, name);
0809 }
0810 
0811 inline void * create_file_mapping (void * handle, unsigned long access, ::boost::ulong_long_type file_offset, const wchar_t * name, interprocess_security_attributes *psec)
0812 {
0813    const boost::winapi::DWORD_ high_size = boost::winapi::DWORD_(file_offset >> 32);
0814    const boost::winapi::DWORD_ low_size  = boost::winapi::DWORD_(file_offset);
0815    return CreateFileMappingW (handle, psec, access, high_size, low_size, name);
0816 }
0817 
0818 inline void * open_file_mapping (unsigned long access, const char *name)
0819 {  return OpenFileMappingA (access, 0, name);   }
0820 
0821 inline void * open_file_mapping (unsigned long access, const wchar_t *name)
0822 {  return OpenFileMappingW (access, 0, name);   }
0823 
0824 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)
0825 {
0826    const boost::winapi::DWORD_ offset_low  = boost::winapi::DWORD_(offset & ((::boost::ulong_long_type)0xFFFFFFFF));
0827    const boost::winapi::DWORD_ offset_high = boost::winapi::DWORD_(offset >> 32);
0828    return MapViewOfFileEx(handle, file_access, offset_high, offset_low, numbytes, base_addr);
0829 }
0830 
0831 template<class CharT>
0832 inline void *create_file(const CharT *name, unsigned long access, unsigned long creation_flags, unsigned long attributes, interprocess_security_attributes *psec)
0833 {
0834    for (unsigned int attempt(0); attempt < error_sharing_violation_tries; ++attempt){
0835       void * const handle = boost::winapi::create_file(name, access,
0836                                         file_share_read | file_share_write | file_share_delete,
0837                                         psec, creation_flags, attributes, 0);
0838       bool const invalid(invalid_handle_value == handle);
0839       if (!invalid){
0840          return handle;
0841       }
0842       if (error_sharing_violation != get_last_error()){
0843          return handle;
0844       }
0845       sleep(error_sharing_violation_sleep_ms);
0846    }
0847    return invalid_handle_value;
0848 }
0849 
0850 inline void get_system_info(interprocess_system_info *info)
0851 {  boost::ipwinapiext::GetSystemInfo(info); }
0852 
0853 inline bool flush_view_of_file(void *base_addr, std::size_t numbytes)
0854 {  return 0 != boost::winapi::FlushViewOfFile(base_addr, numbytes); }
0855 
0856 inline bool virtual_unlock(void *base_addr, std::size_t numbytes)
0857 {  return 0 != boost::ipwinapiext::VirtualUnlock(base_addr, numbytes); }
0858 
0859 inline bool virtual_protect(void *base_addr, std::size_t numbytes, unsigned long flNewProtect, unsigned long &lpflOldProtect)
0860 {  return 0 != boost::ipwinapiext::VirtualProtect(base_addr, numbytes, flNewProtect, &lpflOldProtect); }
0861 
0862 inline bool flush_file_buffers(void *handle)
0863 {  return 0 != boost::ipwinapiext::FlushFileBuffers(handle); }
0864 
0865 inline bool get_file_size(void *handle, __int64 &size)
0866 {  return 0 != boost::winapi::GetFileSizeEx(handle, (boost::winapi::LARGE_INTEGER_*)&size);  }
0867 
0868 template<class CharT>
0869 inline bool create_directory(const CharT *name)
0870 {
0871    interprocess_all_access_security sec;
0872    return 0 != boost::winapi::create_directory(name, sec.get_attributes());
0873 }
0874 
0875 template<class CharT>
0876 inline bool remove_directory(const CharT *lpPathName)
0877 {  return 0 != boost::winapi::remove_directory(lpPathName);   }
0878 
0879 template<class CharT>
0880 inline unsigned long get_temp_path(unsigned long length, CharT *buffer)
0881 {  return boost::winapi::get_temp_path(length, buffer);   }
0882 
0883 inline int set_end_of_file(void *handle)
0884 {  return 0 != boost::winapi::SetEndOfFile(handle);   }
0885 
0886 inline bool set_file_pointer(void *handle, __int64 distance, __int64 *new_file_pointer, unsigned long move_method)
0887 {
0888    boost::winapi::LONG_ highPart = boost::winapi::LONG_(distance >> 32u);
0889    boost::winapi::DWORD_ r = boost::winapi::SetFilePointer(handle, (long)distance, &highPart, move_method);
0890    bool br = r != boost::winapi::INVALID_SET_FILE_POINTER_ || boost::winapi::GetLastError() != 0;
0891    if (br && new_file_pointer){
0892       *new_file_pointer = (__int64)r + ((__int64)highPart << 32);
0893    }
0894 
0895    return br;
0896 }
0897 
0898 inline bool lock_file_ex(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
0899 {  return 0 != boost::winapi::LockFileEx(hnd, flags, reserved, size_low, size_high, overlapped); }
0900 
0901 inline bool unlock_file_ex(void *hnd, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
0902 {  return 0 != boost::winapi::UnlockFileEx(hnd, reserved, size_low, size_high, overlapped);  }
0903 
0904 inline bool write_file(void *hnd, const void *buffer, unsigned long bytes_to_write, unsigned long *bytes_written, interprocess_overlapped* overlapped)
0905 {  return 0 != boost::winapi::WriteFile(hnd, buffer, bytes_to_write, bytes_written, overlapped);  }
0906 
0907 inline bool read_file(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped)
0908 {  return 0 != boost::winapi::ReadFile(hnd, buffer, bytes_to_read, bytes_read, overlapped);  }
0909 
0910 inline bool get_file_information_by_handle(void *hnd, interprocess_by_handle_file_information *info)
0911 {  return 0 != boost::winapi::GetFileInformationByHandle(hnd, info);  }
0912 
0913 inline long interlocked_increment(long volatile *addr)
0914 {  return BOOST_INTERLOCKED_INCREMENT(const_cast<long*>(addr));  }
0915 
0916 inline long interlocked_decrement(long volatile *addr)
0917 {  return BOOST_INTERLOCKED_DECREMENT(const_cast<long*>(addr));  }
0918 
0919 inline long interlocked_compare_exchange(long volatile *addr, long val1, long val2)
0920 {  return BOOST_INTERLOCKED_COMPARE_EXCHANGE(const_cast<long*>(addr), val1, val2);  }
0921 
0922 inline long interlocked_exchange_add(long volatile* addend, long value)
0923 {  return BOOST_INTERLOCKED_EXCHANGE_ADD(const_cast<long*>(addend), value);  }
0924 
0925 inline long interlocked_exchange(long volatile* addend, long value)
0926 {  return BOOST_INTERLOCKED_EXCHANGE(const_cast<long*>(addend), value);  }
0927 
0928 //Forward functions
0929 inline hmodule load_library(const char *name)
0930 {  return boost::winapi::LoadLibraryA(name); }
0931 
0932 inline bool free_library(hmodule module)
0933 {  return 0 != boost::winapi::FreeLibrary(module); }
0934 
0935 inline farproc_t get_proc_address(hmodule module, const char *name)
0936 {  return boost::winapi::GetProcAddress(module, name); }
0937 
0938 inline void *get_current_process()
0939 {  return boost::winapi::GetCurrentProcess();  }
0940 
0941 inline hmodule get_module_handle(const char *name)
0942 {  return boost::winapi::GetModuleHandleA(name); }
0943 
0944 inline long reg_open_key_ex(hkey hKey, const char *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
0945 {  return boost::ipwinapiext::RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
0946 
0947 inline long reg_open_key_ex(hkey hKey, const wchar_t *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
0948 {  return boost::ipwinapiext::RegOpenKeyExW(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
0949 
0950 
0951 inline long reg_query_value_ex(hkey hKey, const char *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
0952 {  return boost::ipwinapiext::RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
0953 
0954 inline long reg_query_value_ex(hkey hKey, const wchar_t *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
0955 {  return boost::ipwinapiext::RegQueryValueExW(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
0956 
0957 inline long reg_close_key(hkey hKey)
0958 {  return boost::ipwinapiext::RegCloseKey(hKey); }
0959 
0960 inline void initialize_object_attributes
0961 ( object_attributes_t *pobject_attr, unicode_string_t *name
0962  , unsigned long attr, void *rootdir, void *security_descr)
0963 
0964 {
0965    pobject_attr->Length = sizeof(object_attributes_t);
0966    pobject_attr->RootDirectory = rootdir;
0967    pobject_attr->Attributes = attr;
0968    pobject_attr->ObjectName = name;
0969    pobject_attr->SecurityDescriptor = security_descr;
0970    pobject_attr->SecurityQualityOfService = 0;
0971 }
0972 
0973 inline void rtl_init_empty_unicode_string(unicode_string_t *ucStr, wchar_t *buf, unsigned short bufSize)
0974 {
0975    ucStr->Buffer = buf;
0976    ucStr->Length = 0;
0977    ucStr->MaximumLength = bufSize;
0978 }
0979 
0980 //A class that locates and caches loaded DLL function addresses.
0981 template<int Dummy>
0982 struct function_address_holder
0983 {
0984    enum  { NtSetInformationFile
0985          , NtQuerySystemInformation
0986          , NtQueryObject
0987          , NtQuerySemaphore
0988          , NtQuerySection
0989          , NtOpenFile
0990          , NtClose
0991          , NtQueryTimerResolution
0992          , NumFunction
0993          };
0994    enum { NtDll_dll, Kernel32_dll, NumModule };
0995 
0996    private:
0997    static const char *FunctionNames[NumFunction];
0998    static const char *ModuleNames[NumModule];
0999    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