summaryrefslogtreecommitdiff
path: root/boost/interprocess/detail/win32_api.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/interprocess/detail/win32_api.hpp')
-rw-r--r--boost/interprocess/detail/win32_api.hpp1587
1 files changed, 1039 insertions, 548 deletions
diff --git a/boost/interprocess/detail/win32_api.hpp b/boost/interprocess/detail/win32_api.hpp
index c53725ca3b..d9d3e70330 100644
--- a/boost/interprocess/detail/win32_api.hpp
+++ b/boost/interprocess/detail/win32_api.hpp
@@ -1,6 +1,6 @@
//////////////////////////////////////////////////////////////////////////////
//
-// (C) Copyright Ion Gaztanaga 2005-2011. Distributed under the Boost
+// (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@@ -8,248 +8,154 @@
//
//////////////////////////////////////////////////////////////////////////////
-#ifndef BOOST_INTERPROCESS_WIN32_PRIMITIVES_HPP
-#define BOOST_INTERPROCESS_WIN32_PRIMITIVES_HPP
+#ifndef BOOST_INTERPROCESS_WIN32_API_HPP
+#define BOOST_INTERPROCESS_WIN32_API_HPP
+
+#if defined(_MSC_VER)
+# pragma once
+#endif
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/date_time/filetime_functions.hpp>
#include <cstddef>
#include <cstring>
-#include <cassert>
+#include <cstdlib>
+#include <cstdio>
+
+#include <boost/assert.hpp>
#include <string>
#include <vector>
#include <memory>
-#if (defined _MSC_VER) && (_MSC_VER >= 1200)
+#ifdef BOOST_USE_WINDOWS_H
+#include <windows.h>
+#include <Wbemidl.h>
+#include <Objbase.h>
+#include <Shlobj.h>
+#endif
+
+#if defined(_MSC_VER)
# pragma once
-# pragma comment( lib, "advapi32.lib" )
+# pragma comment( lib, "Advapi32.lib" )
# pragma comment( lib, "oleaut32.lib" )
# pragma comment( lib, "Ole32.lib" )
# pragma comment( lib, "Psapi.lib" )
+# pragma comment( lib, "Shell32.lib" ) //SHGetSpecialFolderPathA
#endif
-#if (defined BOOST_INTERPROCESS_WINDOWS)
+#if defined (BOOST_INTERPROCESS_WINDOWS)
# include <cstdarg>
# include <boost/detail/interlocked.hpp>
#else
# error "This file can only be included in Windows OS"
#endif
+//////////////////////////////////////////////////////////////////////////////
+//
+// Declaration of Windows structures or typedefs if BOOST_USE_WINDOWS_H is used
+//
+//////////////////////////////////////////////////////////////////////////////
-//The structures used in Interprocess with the
-//same binary interface as windows ones
-namespace boost {
-namespace interprocess {
+namespace boost {
+namespace interprocess {
namespace winapi {
-//Some used constants
-static const unsigned long infinite_time = 0xFFFFFFFF;
-static const unsigned long error_already_exists = 183L;
-static const unsigned long error_invalid_handle = 6L;
-static const unsigned long error_sharing_violation = 32L;
-static const unsigned long error_file_not_found = 2u;
-static const unsigned long error_no_more_files = 18u;
-//Retries in CreateFile, see http://support.microsoft.com/kb/316609
-static const unsigned int error_sharing_violation_tries = 3u;
-static const unsigned int error_sharing_violation_sleep_ms = 250u;
-static const unsigned int error_file_too_large = 223u;
+//Own defines
+static const unsigned long MaxPath = 260;
-static const unsigned long semaphore_all_access = (0x000F0000L)|(0x00100000L)|0x3;
-static const unsigned long mutex_all_access = (0x000F0000L)|(0x00100000L)|0x0001;
+#ifndef BOOST_USE_WINDOWS_H
-static const unsigned long page_readonly = 0x02;
-static const unsigned long page_readwrite = 0x04;
-static const unsigned long page_writecopy = 0x08;
+struct GUID_BIPC
+{
+ unsigned long Data1;
+ unsigned short Data2;
+ unsigned short Data3;
+ unsigned char Data4[8];
+};
-static const unsigned long standard_rights_required = 0x000F0000L;
-static const unsigned long section_query = 0x0001;
-static const unsigned long section_map_write = 0x0002;
-static const unsigned long section_map_read = 0x0004;
-static const unsigned long section_map_execute = 0x0008;
-static const unsigned long section_extend_size = 0x0010;
-static const unsigned long section_all_access = standard_rights_required |
- section_query |
- section_map_write |
- section_map_read |
- section_map_execute |
- section_extend_size;
+#if defined(_MSC_VER)
+#pragma warning (push)
+#pragma warning (disable : 4201) // nonstandard extension used
+#endif
-static const unsigned long file_map_copy = section_query;
-static const unsigned long file_map_write = section_map_write;
-static const unsigned long file_map_read = section_map_read;
-static const unsigned long file_map_all_access = section_all_access;
-static const unsigned long delete_access = 0x00010000L;
-static const unsigned long file_flag_backup_semantics = 0x02000000;
-static const long file_flag_delete_on_close = 0x04000000;
+struct decimal
+{
+ unsigned short wReserved;
+ union {
+ struct {
+ unsigned char scale;
+ unsigned char sign;
+ };
+ unsigned short signscale;
+ };
+ unsigned long Hi32;
+ union {
+ struct {
+ unsigned long Lo32;
+ unsigned long Mid32;
+ };
+ unsigned __int64 Lo64;
+ };
+};
-//Native API constants
-static const unsigned long file_open_for_backup_intent = 0x00004000;
-static const int file_share_valid_flags = 0x00000007;
-static const long file_delete_on_close = 0x00001000L;
-static const long obj_case_insensitive = 0x00000040L;
+typedef unsigned short *bstr;
-static const unsigned long movefile_copy_allowed = 0x02;
-static const unsigned long movefile_delay_until_reboot = 0x04;
-static const unsigned long movefile_replace_existing = 0x01;
-static const unsigned long movefile_write_through = 0x08;
-static const unsigned long movefile_create_hardlink = 0x10;
-static const unsigned long movefile_fail_if_not_trackable = 0x20;
-static const unsigned long file_share_read = 0x00000001;
-static const unsigned long file_share_write = 0x00000002;
-static const unsigned long file_share_delete = 0x00000004;
+struct wchar_variant
+{
+ union
+ {
+ struct
+ {
+ unsigned short vt;
+ unsigned short wReserved1;
+ unsigned short wReserved2;
+ unsigned short wReserved3;
+ union
+ {
+ bstr bstrVal;
+ struct
+ {
+ void* pvRecord;
+ void* pRecInfo;
+ };
+ };
+ };
+ decimal decVal;
+ };
+};
-static const unsigned long file_attribute_readonly = 0x00000001;
-static const unsigned long file_attribute_hidden = 0x00000002;
-static const unsigned long file_attribute_system = 0x00000004;
-static const unsigned long file_attribute_directory = 0x00000010;
-static const unsigned long file_attribute_archive = 0x00000020;
-static const unsigned long file_attribute_device = 0x00000040;
-static const unsigned long file_attribute_normal = 0x00000080;
-static const unsigned long file_attribute_temporary = 0x00000100;
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
-static const unsigned long generic_read = 0x80000000L;
-static const unsigned long generic_write = 0x40000000L;
-static const unsigned long wait_object_0 = 0;
-static const unsigned long wait_abandoned = 0x00000080L;
-static const unsigned long wait_timeout = 258L;
-static const unsigned long wait_failed = (unsigned long)0xFFFFFFFF;
-static const unsigned long duplicate_close_source = (unsigned long)0x00000001;
-static const unsigned long duplicate_same_access = (unsigned long)0x00000002;
-static const unsigned long format_message_allocate_buffer
- = (unsigned long)0x00000100;
-static const unsigned long format_message_ignore_inserts
- = (unsigned long)0x00000200;
-static const unsigned long format_message_from_string
- = (unsigned long)0x00000400;
-static const unsigned long format_message_from_hmodule
- = (unsigned long)0x00000800;
-static const unsigned long format_message_from_system
- = (unsigned long)0x00001000;
-static const unsigned long format_message_argument_array
- = (unsigned long)0x00002000;
-static const unsigned long format_message_max_width_mask
- = (unsigned long)0x000000FF;
-static const unsigned long lang_neutral = (unsigned long)0x00;
-static const unsigned long sublang_default = (unsigned long)0x01;
-static const unsigned long invalid_file_size = (unsigned long)0xFFFFFFFF;
-static const unsigned long invalid_file_attributes = ((unsigned long)-1);
-static void * const invalid_handle_value = ((void*)(long)(-1));
-static const unsigned long file_type_char = 0x0002L;
-static const unsigned long file_type_disk = 0x0001L;
-static const unsigned long file_type_pipe = 0x0003L;
-static const unsigned long file_type_remote = 0x8000L;
-static const unsigned long file_type_unknown = 0x0000L;
-static const unsigned long create_new = 1;
-static const unsigned long create_always = 2;
-static const unsigned long open_existing = 3;
-static const unsigned long open_always = 4;
-static const unsigned long truncate_existing = 5;
-static const unsigned long file_begin = 0;
-static const unsigned long file_current = 1;
-static const unsigned long file_end = 2;
-static const unsigned long lockfile_fail_immediately = 1;
-static const unsigned long lockfile_exclusive_lock = 2;
-static const unsigned long error_lock_violation = 33;
-static const unsigned long security_descriptor_revision = 1;
-//Own defines
-static const long SystemTimeOfDayInfoLength = 48;
-static const long BootAndSystemstampLength = 16;
-static const long BootstampLength = 8;
-static const unsigned long MaxPath = 260;
-//Keys
-static void * const hkey_local_machine = (void*)(unsigned long*)(long)(0x80000002);
-static unsigned long key_query_value = 0x0001;
-//COM API
-const unsigned long RPC_C_AUTHN_LEVEL_PKT_BIPC = 4;
-const unsigned long RPC_C_AUTHN_DEFAULT_BIPC = 0xffffffffL;
-const unsigned long RPC_C_AUTHZ_DEFAULT_BIPC = 0xffffffffL;
-const unsigned long RPC_C_IMP_LEVEL_IMPERSONATE_BIPC = 3;
-const signed long EOAC_NONE_BIPC = 0;
-const signed long CLSCTX_INPROC_SERVER_BIPC = 0x1;
-const signed long CLSCTX_LOCAL_SERVER_BIPC = 0x4;
-const signed long WBEM_FLAG_RETURN_IMMEDIATELY_BIPC = 0x10;
-const signed long WBEM_FLAG_RETURN_WHEN_COMPLETE_BIPC = 0x0;
-const signed long WBEM_FLAG_FORWARD_ONLY_BIPC = 0x20;
-const signed long WBEM_INFINITE_BIPC = 0xffffffffL;
-const signed long RPC_E_TOO_LATE_BIPC = 0x80010119L;
-const signed long S_OK_BIPC = 0L;
-const signed long S_FALSE_BIPC = 1;
-const signed long RPC_E_CHANGED_MODE_BIPC = 0x80010106L;
-const unsigned long COINIT_APARTMENTTHREADED_BIPC = 0x2;
-const unsigned long COINIT_MULTITHREADED_BIPC = 0x0;
-const unsigned long COINIT_DISABLE_OLE1DDE_BIPC = 0x4;
-const unsigned long COINIT_SPEED_OVER_MEMORY_BIPC = 0x4;
-//If the user needs to change default COM initialization model,
-//it can define BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL to one of these:
-//
-// COINIT_APARTMENTTHREADED_BIPC
-// COINIT_MULTITHREADED_BIPC
-// COINIT_DISABLE_OLE1DDE_BIPC
-// COINIT_SPEED_OVER_MEMORY_BIPC
-#if !defined(BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL)
- #define BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL COINIT_APARTMENTTHREADED_BIPC
-#elif (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_APARTMENTTHREADED_BIPC) &&\
- (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_MULTITHREADED_BIPC) &&\
- (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_DISABLE_OLE1DDE_BIPC) &&\
- (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_SPEED_OVER_MEMORY_BIPC)
- #error "Wrong value for BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL macro"
-#endif
-} //namespace winapi {
-} //namespace interprocess {
-} //namespace boost {
-namespace boost {
-namespace interprocess {
-namespace winapi {
-struct GUID_BIPC
-{
- unsigned long Data1;
- unsigned short Data2;
- unsigned short Data3;
- unsigned char Data4[8];
-};
-
-const GUID_BIPC CLSID_WbemAdministrativeLocator =
- { 0xcb8555cc, 0x9128, 0x11d1, {0xad, 0x9b, 0x00, 0xc0, 0x4f, 0xd8, 0xfd, 0xff}};
-
-const GUID_BIPC IID_IUnknown = { 0x00000000, 0x0000, 0x0000, {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}};
-struct wchar_variant
+struct IUnknown_BIPC
{
- unsigned long long dummy;
- union value_t{
- wchar_t *pbstrVal;
- unsigned long long dummy;
- } value;
-};
-
- struct IUnknown_BIPC
- {
- public:
- virtual long __stdcall QueryInterface(
- /* [in] */ const GUID_BIPC &riid,
- /* [iid_is][out] */ void **ppvObject) = 0;
-
- virtual unsigned long __stdcall AddRef( void) = 0;
+ public:
+ virtual long __stdcall QueryInterface(
+ const GUID_BIPC &riid, // [in]
+ void **ppvObject) = 0; // [iid_is][out]
- virtual unsigned long __stdcall Release( void) = 0;
- };
+ virtual unsigned long __stdcall AddRef (void) = 0;
+ virtual unsigned long __stdcall Release(void) = 0;
+};
struct IWbemClassObject_BIPC : public IUnknown_BIPC
{
@@ -258,23 +164,23 @@ struct IWbemClassObject_BIPC : public IUnknown_BIPC
/* [out] */ void **ppQualSet) = 0;
virtual long __stdcall Get(
- /* [string][in] */ const wchar_t * wszName,
+ /* [string][in] */ const bstr wszName,
/* [in] */ long lFlags,
/* [unique][in][out] */ wchar_variant *pVal,
/* [unique][in][out] */ long *pType,
/* [unique][in][out] */ long *plFlavor) = 0;
virtual long __stdcall Put(
- /* [string][in] */ const wchar_t * wszName,
+ /* [string][in] */ const bstr wszName,
/* [in] */ long lFlags,
/* [in] */ wchar_variant *pVal,
/* [in] */ long Type) = 0;
virtual long __stdcall Delete(
- /* [string][in] */ const wchar_t * wszName) = 0;
+ /* [string][in] */ const bstr wszName) = 0;
virtual long __stdcall GetNames(
- /* [string][in] */ const wchar_t * wszQualifierName,
+ /* [string][in] */ const bstr wszQualifierName,
/* [in] */ long lFlags,
/* [in] */ wchar_variant *pQualifierVal,
/* [out] */ void * *pNames) = 0;
@@ -284,7 +190,7 @@ struct IWbemClassObject_BIPC : public IUnknown_BIPC
virtual long __stdcall Next(
/* [in] */ long lFlags,
- /* [unique][in][out] */ wchar_t * *strName,
+ /* [unique][in][out] */ bstr *strName,
/* [unique][in][out] */ wchar_variant *pVal,
/* [unique][in][out] */ long *pType,
/* [unique][in][out] */ long *plFlavor) = 0;
@@ -292,7 +198,7 @@ struct IWbemClassObject_BIPC : public IUnknown_BIPC
virtual long __stdcall EndEnumeration( void) = 0;
virtual long __stdcall GetPropertyQualifierSet(
- /* [string][in] */ const wchar_t * wszProperty,
+ /* [string][in] */ const bstr wszProperty,
/* [out] */ void **ppQualSet) = 0;
virtual long __stdcall Clone(
@@ -300,7 +206,7 @@ struct IWbemClassObject_BIPC : public IUnknown_BIPC
virtual long __stdcall GetObjectText(
/* [in] */ long lFlags,
- /* [out] */ wchar_t * *pstrObjectText) = 0;
+ /* [out] */ bstr *pstrObjectText) = 0;
virtual long __stdcall SpawnDerivedClass(
/* [in] */ long lFlags,
@@ -315,49 +221,48 @@ struct IWbemClassObject_BIPC : public IUnknown_BIPC
/* [in] */ IWbemClassObject_BIPC *pCompareTo) = 0;
virtual long __stdcall GetPropertyOrigin(
- /* [string][in] */ const wchar_t * wszName,
- /* [out] */ wchar_t * *pstrClassName) = 0;
+ /* [string][in] */ const bstr wszName,
+ /* [out] */ bstr *pstrClassName) = 0;
virtual long __stdcall InheritsFrom(
- /* [in] */ const wchar_t * strAncestor) = 0;
+ /* [in] */ const bstr strAncestor) = 0;
virtual long __stdcall GetMethod(
- /* [string][in] */ const wchar_t * wszName,
+ /* [string][in] */ const bstr wszName,
/* [in] */ long lFlags,
/* [out] */ IWbemClassObject_BIPC **ppInSignature,
/* [out] */ IWbemClassObject_BIPC **ppOutSignature) = 0;
virtual long __stdcall PutMethod(
- /* [string][in] */ const wchar_t * wszName,
+ /* [string][in] */ const bstr wszName,
/* [in] */ long lFlags,
/* [in] */ IWbemClassObject_BIPC *pInSignature,
/* [in] */ IWbemClassObject_BIPC *pOutSignature) = 0;
virtual long __stdcall DeleteMethod(
- /* [string][in] */ const wchar_t * wszName) = 0;
+ /* [string][in] */ const bstr wszName) = 0;
virtual long __stdcall BeginMethodEnumeration(
/* [in] */ long lEnumFlags) = 0;
virtual long __stdcall NextMethod(
/* [in] */ long lFlags,
- /* [unique][in][out] */ wchar_t * *pstrName,
+ /* [unique][in][out] */ bstr *pstrName,
/* [unique][in][out] */ IWbemClassObject_BIPC **ppInSignature,
/* [unique][in][out] */ IWbemClassObject_BIPC **ppOutSignature) = 0;
virtual long __stdcall EndMethodEnumeration( void) = 0;
virtual long __stdcall GetMethodQualifierSet(
- /* [string][in] */ const wchar_t * wszMethod,
+ /* [string][in] */ const bstr wszMethod,
/* [out] */ void **ppQualSet) = 0;
virtual long __stdcall GetMethodOrigin(
- /* [string][in] */ const wchar_t * wszMethodName,
- /* [out] */ wchar_t * *pstrClassName) = 0;
+ /* [string][in] */ const bstr wszMethodName,
+ /* [out] */ bstr *pstrClassName) = 0;
};
-
struct IWbemContext_BIPC : public IUnknown_BIPC
{
public:
@@ -373,23 +278,23 @@ public:
virtual long __stdcall Next(
/* [in] */ long lFlags,
- /* [out] */ wchar_t * *pstrName,
+ /* [out] */ bstr *pstrName,
/* [out] */ wchar_variant *pValue) = 0;
virtual long __stdcall EndEnumeration( void) = 0;
virtual long __stdcall SetValue(
- /* [string][in] */ const wchar_t * wszName,
+ /* [string][in] */ const bstr wszName,
/* [in] */ long lFlags,
/* [in] */ wchar_variant *pValue) = 0;
virtual long __stdcall GetValue(
- /* [string][in] */ const wchar_t * wszName,
+ /* [string][in] */ const bstr wszName,
/* [in] */ long lFlags,
/* [out] */ wchar_variant *pValue) = 0;
virtual long __stdcall DeleteValue(
- /* [string][in] */ const wchar_t * wszName,
+ /* [string][in] */ const bstr wszName,
/* [in] */ long lFlags) = 0;
virtual long __stdcall DeleteAll( void) = 0;
@@ -425,7 +330,7 @@ struct IWbemServices_BIPC : public IUnknown_BIPC
{
public:
virtual long __stdcall OpenNamespace(
- /* [in] */ const wchar_t * strNamespace,
+ /* [in] */ const bstr strNamespace,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [unique][in][out] */ void **ppWorkingNamespace,
@@ -439,14 +344,14 @@ public:
/* [out] */ void **ppResponseHandler) = 0;
virtual long __stdcall GetObject(
- /* [in] */ const wchar_t * strObjectPath,
+ /* [in] */ const bstr strObjectPath,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [unique][in][out] */ void **ppObject,
/* [unique][in][out] */ void **ppCallResult) = 0;
virtual long __stdcall GetObjectAsync(
- /* [in] */ const wchar_t * strObjectPath,
+ /* [in] */ const bstr strObjectPath,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [in] */ void *pResponseHandler) = 0;
@@ -464,25 +369,25 @@ public:
/* [in] */ void *pResponseHandler) = 0;
virtual long __stdcall DeleteClass(
- /* [in] */ const wchar_t * strClass,
+ /* [in] */ const bstr strClass,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [unique][in][out] */ void **ppCallResult) = 0;
virtual long __stdcall DeleteClassAsync(
- /* [in] */ const wchar_t * strClass,
+ /* [in] */ const bstr strClass,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [in] */ void *pResponseHandler) = 0;
virtual long __stdcall CreateClassEnum(
- /* [in] */ const wchar_t * strSuperclass,
+ /* [in] */ const bstr strSuperclass,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [out] */ void **ppEnum) = 0;
virtual long __stdcall CreateClassEnumAsync(
- /* [in] */ const wchar_t * strSuperclass,
+ /* [in] */ const bstr strSuperclass,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [in] */ void *pResponseHandler) = 0;
@@ -500,60 +405,60 @@ public:
/* [in] */ void *pResponseHandler) = 0;
virtual long __stdcall DeleteInstance(
- /* [in] */ const wchar_t * strObjectPath,
+ /* [in] */ const bstr strObjectPath,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [unique][in][out] */ void **ppCallResult) = 0;
virtual long __stdcall DeleteInstanceAsync(
- /* [in] */ const wchar_t * strObjectPath,
+ /* [in] */ const bstr strObjectPath,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [in] */ void *pResponseHandler) = 0;
virtual long __stdcall CreateInstanceEnum(
- /* [in] */ const wchar_t * strFilter,
+ /* [in] */ const bstr strFilter,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [out] */ void **ppEnum) = 0;
virtual long __stdcall CreateInstanceEnumAsync(
- /* [in] */ const wchar_t * strFilter,
+ /* [in] */ const bstr strFilter,
/* [in] */ long lFlags,
/* [in] */ void *pCtx,
/* [in] */ void *pResponseHandler) = 0;
virtual long __stdcall ExecQuery(
- /* [in] */ const wchar_t * strQueryLanguage,
- /* [in] */ const wchar_t * strQuery,
+ /* [in] */ const bstr strQueryLanguage,
+ /* [in] */ const bstr strQuery,
/* [in] */ long lFlags,
/* [in] */ IWbemContext_BIPC *pCtx,
/* [out] */ IEnumWbemClassObject_BIPC **ppEnum) = 0;
virtual long __stdcall ExecQueryAsync(
- /* [in] */ const wchar_t * strQueryLanguage,
- /* [in] */ const wchar_t * strQuery,
+ /* [in] */ const bstr strQueryLanguage,
+ /* [in] */ const bstr strQuery,
/* [in] */ long lFlags,
/* [in] */ IWbemContext_BIPC *pCtx,
/* [in] */ void *pResponseHandler) = 0;
virtual long __stdcall ExecNotificationQuery(
- /* [in] */ const wchar_t * strQueryLanguage,
- /* [in] */ const wchar_t * strQuery,
+ /* [in] */ const bstr strQueryLanguage,
+ /* [in] */ const bstr strQuery,
/* [in] */ long lFlags,
/* [in] */ IWbemContext_BIPC *pCtx,
/* [out] */ void **ppEnum) = 0;
virtual long __stdcall ExecNotificationQueryAsync(
- /* [in] */ const wchar_t * strQueryLanguage,
- /* [in] */ const wchar_t * strQuery,
+ /* [in] */ const bstr strQueryLanguage,
+ /* [in] */ const bstr strQuery,
/* [in] */ long lFlags,
/* [in] */ IWbemContext_BIPC *pCtx,
/* [in] */ void *pResponseHandler) = 0;
virtual long __stdcall ExecMethod(
- /* [in] */ const wchar_t * strObjectPath,
- /* [in] */ const wchar_t * strMethodName,
+ /* [in] */ const bstr strObjectPath,
+ /* [in] */ const bstr strMethodName,
/* [in] */ long lFlags,
/* [in] */ IWbemContext_BIPC *pCtx,
/* [in] */ IWbemClassObject_BIPC *pInParams,
@@ -561,8 +466,8 @@ public:
/* [unique][in][out] */ void **ppCallResult) = 0;
virtual long __stdcall ExecMethodAsync(
- /* [in] */ const wchar_t * strObjectPath,
- /* [in] */ const wchar_t * strMethodName,
+ /* [in] */ const bstr strObjectPath,
+ /* [in] */ const bstr strMethodName,
/* [in] */ long lFlags,
/* [in] */ IWbemContext_BIPC *pCtx,
/* [in] */ IWbemClassObject_BIPC *pInParams,
@@ -574,19 +479,17 @@ struct IWbemLocator_BIPC : public IUnknown_BIPC
{
public:
virtual long __stdcall ConnectServer(
- /* [in] */ const wchar_t * strNetworkResource,
- /* [in] */ const wchar_t * strUser,
- /* [in] */ const wchar_t * strPassword,
- /* [in] */ const wchar_t * strLocale,
+ /* [in] */ const bstr strNetworkResource,
+ /* [in] */ const bstr strUser,
+ /* [in] */ const bstr strPassword,
+ /* [in] */ const bstr strLocale,
/* [in] */ long lSecurityFlags,
- /* [in] */ const wchar_t * strAuthority,
+ /* [in] */ const bstr strAuthority,
/* [in] */ void *pCtx,
/* [out] */ IWbemServices_BIPC **ppNamespace) = 0;
};
-
-
struct interprocess_overlapped
{
unsigned long *internal;
@@ -602,18 +505,6 @@ struct interprocess_overlapped
void *h_event;
};
-struct interprocess_semaphore_basic_information
-{
- unsigned int count; // current semaphore count
- unsigned int limit; // max semaphore count
-};
-
-struct interprocess_section_basic_information
-{
- void * base_address;
- unsigned long section_attributes;
- __int64 section_size;
-};
struct interprocess_filetime
{
@@ -621,7 +512,7 @@ struct interprocess_filetime
unsigned long dwHighDateTime;
};
-struct win32_find_data_t
+struct win32_find_data
{
unsigned long dwFileAttributes;
interprocess_filetime ftCreationTime;
@@ -661,27 +552,16 @@ struct system_info {
unsigned short wProcessorRevision;
};
-typedef struct _interprocess_memory_basic_information
-{
- void * BaseAddress;
- void * AllocationBase;
- unsigned long AllocationProtect;
- unsigned long RegionSize;
- unsigned long State;
- unsigned long Protect;
- unsigned long Type;
-} interprocess_memory_basic_information;
-
-typedef struct _interprocess_acl
+struct interprocess_acl
{
unsigned char AclRevision;
unsigned char Sbz1;
unsigned short AclSize;
unsigned short AceCount;
unsigned short Sbz2;
-} interprocess_acl;
+};
-typedef struct _interprocess_security_descriptor
+struct interprocess_security_descriptor
{
unsigned char Revision;
unsigned char Sbz1;
@@ -690,59 +570,134 @@ typedef struct _interprocess_security_descriptor
void *Group;
interprocess_acl *Sacl;
interprocess_acl *Dacl;
-} interprocess_security_descriptor;
+};
-enum file_information_class_t {
- file_directory_information = 1,
- file_full_directory_information,
- file_both_directory_information,
- file_basic_information,
- file_standard_information,
- file_internal_information,
- file_ea_information,
- file_access_information,
- file_name_information,
- file_rename_information,
- file_link_information,
- file_names_information,
- file_disposition_information,
- file_position_information,
- file_full_ea_information,
- file_mode_information,
- file_alignment_information,
- file_all_information,
- file_allocation_information,
- file_end_of_file_information,
- file_alternate_name_information,
- file_stream_information,
- file_pipe_information,
- file_pipe_local_information,
- file_pipe_remote_information,
- file_mailslot_query_information,
- file_mailslot_set_information,
- file_compression_information,
- file_copy_on_write_information,
- file_completion_information,
- file_move_cluster_information,
- file_quota_information,
- file_reparse_point_information,
- file_network_open_information,
- file_object_id_information,
- file_tracking_information,
- file_ole_directory_information,
- file_content_index_information,
- file_inherit_content_index_information,
- file_ole_information,
- file_maximum_information
+struct interprocess_by_handle_file_information
+{
+ unsigned long dwFileAttributes;
+ interprocess_filetime ftCreationTime;
+ interprocess_filetime ftLastAccessTime;
+ interprocess_filetime ftLastWriteTime;
+ unsigned long dwVolumeSerialNumber;
+ unsigned long nFileSizeHigh;
+ unsigned long nFileSizeLow;
+ unsigned long nNumberOfLinks;
+ unsigned long nFileIndexHigh;
+ unsigned long nFileIndexLow;
};
-enum semaphore_information_class {
- semaphore_basic_information = 0
+struct interprocess_eventlogrecord
+{
+ unsigned long Length; // Length of full record
+ unsigned long Reserved; // Used by the service
+ unsigned long RecordNumber; // Absolute record number
+ unsigned long TimeGenerated; // Seconds since 1-1-1970
+ unsigned long TimeWritten; // Seconds since 1-1-1970
+ unsigned long EventID;
+ unsigned short EventType;
+ unsigned short NumStrings;
+ unsigned short EventCategory;
+ unsigned short ReservedFlags; // For use with paired events (auditing)
+ unsigned long ClosingRecordNumber; // For use with paired events (auditing)
+ unsigned long StringOffset; // Offset from beginning of record
+ unsigned long UserSidLength;
+ unsigned long UserSidOffset;
+ unsigned long DataLength;
+ unsigned long DataOffset; // Offset from beginning of record
+ //
+ // Then follow:
+ //
+ // wchar_t SourceName[]
+ // wchar_t Computername[]
+ // SID UserSid
+ // wchar_t Strings[]
+ // BYTE Data[]
+ // CHAR Pad[]
+ // unsigned long Length;
+ //
};
-struct file_name_information_t {
- unsigned long FileNameLength;
- wchar_t FileName[1];
+union large_integer
+{
+ __int64 QuadPart;
+};
+
+struct hinstance_struct { int unused; };
+typedef hinstance_struct *hmodule;
+
+struct hkey_struct;
+typedef hkey_struct *hkey;
+
+#ifdef _WIN64
+typedef __int64 (__stdcall *farproc_t)();
+#else
+typedef int (__stdcall *farproc_t)();
+#endif // _WIN64
+
+#else //#ifndef BOOST_USE_WINDOWS_H
+
+typedef GUID GUID_BIPC;
+typedef VARIANT wchar_variant;
+
+typedef IUnknown IUnknown_BIPC;
+
+typedef IWbemClassObject IWbemClassObject_BIPC;
+
+typedef IWbemContext IWbemContext_BIPC;
+
+typedef IEnumWbemClassObject IEnumWbemClassObject_BIPC;
+
+typedef IWbemServices IWbemServices_BIPC;
+
+typedef IWbemLocator IWbemLocator_BIPC;
+
+typedef OVERLAPPED interprocess_overlapped;
+
+typedef FILETIME interprocess_filetime;
+
+typedef WIN32_FIND_DATAA win32_find_data;
+
+typedef SECURITY_ATTRIBUTES interprocess_security_attributes;
+
+typedef SYSTEM_INFO system_info;
+
+typedef ACL interprocess_acl;
+
+typedef SECURITY_DESCRIPTOR interprocess_security_descriptor;
+
+typedef BY_HANDLE_FILE_INFORMATION interprocess_by_handle_file_information;
+
+typedef EVENTLOGRECORD interprocess_eventlogrecord;
+
+typedef LARGE_INTEGER large_integer;
+
+typedef HMODULE hmodule;
+
+typedef HKEY hkey;
+
+typedef BSTR bstr;
+
+typedef FARPROC farproc_t;
+
+#endif //#ifndef BOOST_USE_WINDOWS_H
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// Nt native structures
+//
+//////////////////////////////////////////////////////////////////////////////
+
+struct interprocess_semaphore_basic_information
+{
+ unsigned int count; // current semaphore count
+ unsigned int limit; // max semaphore count
+};
+
+struct interprocess_section_basic_information
+{
+ void * base_address;
+ unsigned long section_attributes;
+ __int64 section_size;
};
struct file_rename_information_t {
@@ -785,24 +740,71 @@ union system_timeofday_information
__int64 liExpTimeZoneBias;
unsigned long uCurrentTimeZoneId;
unsigned long dwReserved;
+ unsigned __int64 ullBootTimeBias;
+ unsigned __int64 ullSleepTimeBias;
} data;
- unsigned char Reserved1[SystemTimeOfDayInfoLength];
+ unsigned char Reserved1[sizeof(data_t)];
};
-struct interprocess_by_handle_file_information
+static const long BootstampLength = sizeof(__int64);
+static const long BootAndSystemstampLength = sizeof(__int64)*2;
+static const long SystemTimeOfDayInfoLength = sizeof(system_timeofday_information::data_t);
+
+struct object_name_information_t
{
- unsigned long dwFileAttributes;
- interprocess_filetime ftCreationTime;
- interprocess_filetime ftLastAccessTime;
- interprocess_filetime ftLastWriteTime;
- unsigned long dwVolumeSerialNumber;
- unsigned long nFileSizeHigh;
- unsigned long nFileSizeLow;
- unsigned long nNumberOfLinks;
- unsigned long nFileIndexHigh;
- unsigned long nFileIndexLow;
+ unicode_string_t Name;
+ wchar_t NameBuffer[1];
};
+enum file_information_class_t {
+ file_directory_information = 1,
+ file_full_directory_information,
+ file_both_directory_information,
+ file_basic_information,
+ file_standard_information,
+ file_internal_information,
+ file_ea_information,
+ file_access_information,
+ file_name_information,
+ file_rename_information,
+ file_link_information,
+ file_names_information,
+ file_disposition_information,
+ file_position_information,
+ file_full_ea_information,
+ file_mode_information,
+ file_alignment_information,
+ file_all_information,
+ file_allocation_information,
+ file_end_of_file_information,
+ file_alternate_name_information,
+ file_stream_information,
+ file_pipe_information,
+ file_pipe_local_information,
+ file_pipe_remote_information,
+ file_mailslot_query_information,
+ file_mailslot_set_information,
+ file_compression_information,
+ file_copy_on_write_information,
+ file_completion_information,
+ file_move_cluster_information,
+ file_quota_information,
+ file_reparse_point_information,
+ file_network_open_information,
+ file_object_id_information,
+ file_tracking_information,
+ file_ole_directory_information,
+ file_content_index_information,
+ file_inherit_content_index_information,
+ file_ole_information,
+ file_maximum_information
+};
+
+enum semaphore_information_class {
+ semaphore_basic_information = 0
+};
+
+
enum system_information_class {
system_basic_information = 0,
system_performance_information = 2,
@@ -830,11 +832,15 @@ enum section_information_class
section_image_information
};
-struct object_name_information_t
-{
- unicode_string_t Name;
- wchar_t NameBuffer[1];
-};
+//////////////////////////////////////////////////////////////////////////////
+//
+// Forward declaration of winapi
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_USE_WINDOWS_H
+
+//Kernel32.dll
//Some windows API declarations
extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentProcessId();
@@ -844,6 +850,7 @@ extern "C" __declspec(dllimport) int __stdcall GetProcessTimes
, interprocess_filetime *lpExitTime,interprocess_filetime *lpKernelTime
, interprocess_filetime *lpUserTime );
extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long);
+extern "C" __declspec(dllimport) unsigned long __stdcall GetTickCount(void);
extern "C" __declspec(dllimport) int __stdcall SwitchToThread();
extern "C" __declspec(dllimport) unsigned long __stdcall GetLastError();
extern "C" __declspec(dllimport) void __stdcall SetLastError(unsigned long);
@@ -855,8 +862,8 @@ extern "C" __declspec(dllimport) int __stdcall DuplicateHandle
, unsigned long dwDesiredAccess, int bInheritHandle
, unsigned long dwOptions);
extern "C" __declspec(dllimport) long __stdcall GetFileType(void *hFile);
-extern "C" __declspec(dllimport) void *__stdcall FindFirstFileA(const char *lpFileName, win32_find_data_t *lpFindFileData);
-extern "C" __declspec(dllimport) int __stdcall FindNextFileA(void *hFindFile, win32_find_data_t *lpFindFileData);
+extern "C" __declspec(dllimport) void *__stdcall FindFirstFileA(const char *lpFileName, win32_find_data *lpFindFileData);
+extern "C" __declspec(dllimport) int __stdcall FindNextFileA(void *hFindFile, win32_find_data *lpFindFileData);
extern "C" __declspec(dllimport) int __stdcall FindClose(void *hFindFile);
//extern "C" __declspec(dllimport) void __stdcall GetSystemTimeAsFileTime(interprocess_filetime*);
//extern "C" __declspec(dllimport) int __stdcall FileTimeToLocalFileTime(const interprocess_filetime *in, const interprocess_filetime *out);
@@ -872,12 +879,12 @@ extern "C" __declspec(dllimport) void * __stdcall CreateFileMappingA (void *, in
extern "C" __declspec(dllimport) void * __stdcall MapViewOfFileEx (void *, unsigned long, unsigned long, unsigned long, std::size_t, void*);
extern "C" __declspec(dllimport) void * __stdcall OpenFileMappingA (unsigned long, int, const char *);
extern "C" __declspec(dllimport) void * __stdcall CreateFileA (const char *, unsigned long, unsigned long, struct interprocess_security_attributes*, unsigned long, unsigned long, void *);
-extern "C" __declspec(dllimport) int __stdcall DeleteFileA (const char *);
-extern "C" __declspec(dllimport) int __stdcall MoveFileExA (const char *, const char *, unsigned long);
extern "C" __declspec(dllimport) void __stdcall GetSystemInfo (struct system_info *);
extern "C" __declspec(dllimport) int __stdcall FlushViewOfFile (void *, std::size_t);
+extern "C" __declspec(dllimport) int __stdcall VirtualUnlock (void *, std::size_t);
+extern "C" __declspec(dllimport) int __stdcall VirtualProtect (void *, std::size_t, unsigned long, unsigned long *);
extern "C" __declspec(dllimport) int __stdcall FlushFileBuffers (void *);
-extern "C" __declspec(dllimport) int __stdcall GetFileSizeEx (void *, __int64 *size);
+extern "C" __declspec(dllimport) int __stdcall GetFileSizeEx (void *, large_integer *size);
extern "C" __declspec(dllimport) unsigned long __stdcall FormatMessageA
(unsigned long dwFlags, const void *lpSource, unsigned long dwMessageId,
unsigned long dwLanguageId, char *lpBuffer, unsigned long nSize,
@@ -890,7 +897,7 @@ extern "C" __declspec(dllimport) int __stdcall GetTempPathA(unsigned long length
extern "C" __declspec(dllimport) int __stdcall CreateDirectory(const char *, interprocess_security_attributes*);
extern "C" __declspec(dllimport) int __stdcall SetFileValidData(void *, __int64 size);
extern "C" __declspec(dllimport) int __stdcall SetEndOfFile(void *);
-extern "C" __declspec(dllimport) int __stdcall SetFilePointerEx(void *, __int64 distance, __int64 *new_file_pointer, unsigned long move_method);
+extern "C" __declspec(dllimport) int __stdcall SetFilePointerEx(void *, large_integer distance, large_integer *new_file_pointer, unsigned long move_method);
extern "C" __declspec(dllimport) int __stdcall LockFile (void *hnd, unsigned long offset_low, unsigned long offset_high, unsigned long size_low, unsigned long size_high);
extern "C" __declspec(dllimport) int __stdcall UnlockFile(void *hnd, unsigned long offset_low, unsigned long offset_high, unsigned long size_low, unsigned long size_high);
extern "C" __declspec(dllimport) int __stdcall LockFileEx(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped* overlapped);
@@ -899,18 +906,18 @@ extern "C" __declspec(dllimport) int __stdcall WriteFile(void *hnd, const void *
extern "C" __declspec(dllimport) int __stdcall ReadFile(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped);
extern "C" __declspec(dllimport) int __stdcall InitializeSecurityDescriptor(interprocess_security_descriptor *pSecurityDescriptor, unsigned long dwRevision);
extern "C" __declspec(dllimport) int __stdcall SetSecurityDescriptorDacl(interprocess_security_descriptor *pSecurityDescriptor, int bDaclPresent, interprocess_acl *pDacl, int bDaclDefaulted);
-extern "C" __declspec(dllimport) void *__stdcall LoadLibraryA(const char *);
-extern "C" __declspec(dllimport) int __stdcall FreeLibrary(void *);
-extern "C" __declspec(dllimport) void *__stdcall GetProcAddress(void *, const char*);
-extern "C" __declspec(dllimport) void *__stdcall GetModuleHandleA(const char*);
+extern "C" __declspec(dllimport) hmodule __stdcall LoadLibraryA(const char *);
+extern "C" __declspec(dllimport) int __stdcall FreeLibrary(hmodule);
+extern "C" __declspec(dllimport) farproc_t __stdcall GetProcAddress(void *, const char*);
+extern "C" __declspec(dllimport) hmodule __stdcall GetModuleHandleA(const char*);
extern "C" __declspec(dllimport) void *__stdcall GetFileInformationByHandle(void *, interprocess_by_handle_file_information*);
-extern "C" __declspec(dllimport) unsigned long __stdcall GetMappedFileNameW(void *, void *, wchar_t *, unsigned long);
-extern "C" __declspec(dllimport) long __stdcall RegOpenKeyExA(void *, const char *, unsigned long, unsigned long, void **);
-extern "C" __declspec(dllimport) long __stdcall RegQueryValueExA(void *, const char *, unsigned long*, unsigned long*, unsigned char *, unsigned long*);
-extern "C" __declspec(dllimport) long __stdcall RegCloseKey(void *);
-extern "C" __declspec(dllimport) int __stdcall QueryPerformanceCounter(__int64 *lpPerformanceCount);
-//COM API
+//Advapi32.dll
+extern "C" __declspec(dllimport) long __stdcall RegOpenKeyExA(hkey, const char *, unsigned long, unsigned long, hkey*);
+extern "C" __declspec(dllimport) long __stdcall RegQueryValueExA(hkey, const char *, unsigned long*, unsigned long*, unsigned char *, unsigned long*);
+extern "C" __declspec(dllimport) long __stdcall RegCloseKey(hkey);
+
+//Ole32.dll
extern "C" __declspec(dllimport) long __stdcall CoInitializeEx(void *pvReserved, unsigned long dwCoInit);
extern "C" __declspec(dllimport) long __stdcall CoInitializeSecurity(
void* pSecDesc,
@@ -932,18 +939,47 @@ extern "C" __declspec(dllimport) long __stdcall CoInitializeSecurity(
unsigned long dwImpLevel,
void *pAuthInfo,
unsigned long dwCapabilities);
-
-extern "C" __declspec(dllimport) long __stdcall VariantClear(wchar_variant * pvarg);
extern "C" __declspec(dllimport) long __stdcall CoCreateInstance(const GUID_BIPC & rclsid, IUnknown_BIPC *pUnkOuter,
unsigned long dwClsContext, const GUID_BIPC & riid, void** ppv);
extern "C" __declspec(dllimport) void __stdcall CoUninitialize(void);
+//OleAut32.dll
+extern "C" __declspec(dllimport) long __stdcall VariantClear(wchar_variant * pvarg);
+
+//Shell32.dll
+extern "C" __declspec(dllimport) int __stdcall SHGetSpecialFolderPathA
+ (void* hwnd, const char *pszPath, int csidl, int fCreate);
+
+extern "C" __declspec(dllimport) int __stdcall SHGetFolderPathA(void *hwnd, int csidl, void *hToken, unsigned long dwFlags, const char *pszPath);
+
+//EventLog access functions
+extern "C" __declspec(dllimport) void* __stdcall OpenEventLogA
+ (const char* lpUNCServerName, const char* lpSourceName);
-//API function typedefs
-//Pointer to functions
+extern "C" __declspec(dllimport) int __stdcall CloseEventLog(void *hEventLog);
+
+extern "C" __declspec(dllimport) int __stdcall ReadEventLogA
+ (void *hEventLog,
+ unsigned long dwReadFlags,
+ unsigned long dwRecordOffset,
+ void *lpBuffer,
+ unsigned long nNumberOfBytesToRead,
+ unsigned long *pnBytesRead,
+ unsigned long *pnMinNumberOfBytesNeeded
+ );
+
+#endif //#ifndef BOOST_USE_WINDOWS_H
+
+//kernel32.dll
+typedef int (__stdcall *QueryPerformanceCounter_t) (__int64 *lpPerformanceCount);
+typedef int (__stdcall *QueryPerformanceFrequency_t)(__int64 *lpFrequency);
+
+//ntdll.dll
typedef long (__stdcall *NtDeleteFile_t)(object_attributes_t *ObjectAttributes);
typedef long (__stdcall *NtSetInformationFile_t)(void *FileHandle, io_status_block_t *IoStatusBlock, void *FileInformation, unsigned long Length, int FileInformationClass );
+typedef long (__stdcall *NtOpenFile)(void **FileHandle, unsigned long DesiredAccess, object_attributes_t *ObjectAttributes
+ , io_status_block_t *IoStatusBlock, unsigned long ShareAccess, unsigned long Length, unsigned long OpenOptions);
typedef long (__stdcall *NtQuerySystemInformation_t)(int, void*, unsigned long, unsigned long *);
typedef long (__stdcall *NtQueryObject_t)(void*, object_information_class, void *, unsigned long, unsigned long *);
typedef long (__stdcall *NtQuerySemaphore_t)(void*, unsigned int info_class, interprocess_semaphore_basic_information *pinfo, unsigned int info_size, unsigned int *ret_len);
@@ -951,19 +987,203 @@ typedef long (__stdcall *NtQuerySection_t)(void*, section_information_class, int
typedef long (__stdcall *NtQueryInformationFile_t)(void *,io_status_block_t *,void *, long, int);
typedef long (__stdcall *NtOpenFile_t)(void*,unsigned long ,object_attributes_t*,io_status_block_t*,unsigned long,unsigned long);
typedef long (__stdcall *NtClose_t) (void*);
-typedef long (__stdcall *RtlCreateUnicodeStringFromAsciiz_t)(unicode_string_t *, const char *);
-typedef void (__stdcall *RtlFreeUnicodeString_t)(unicode_string_t *);
-typedef void (__stdcall *RtlInitUnicodeString_t)( unicode_string_t *, const wchar_t * );
-typedef long (__stdcall *RtlAppendUnicodeToString_t)(unicode_string_t *Destination, const wchar_t *Source);
-typedef unsigned long (__stdcall * GetMappedFileName_t)(void *, void *, wchar_t *, unsigned long);
-typedef long (__stdcall * RegOpenKeyEx_t)(void *, const char *, unsigned long, unsigned long, void **);
-typedef long (__stdcall * RegQueryValueEx_t)(void *, const char *, unsigned long*, unsigned long*, unsigned char *, unsigned long*);
-typedef long (__stdcall * RegCloseKey_t)(void *);
+typedef long (__stdcall *NtQueryTimerResolution_t) (unsigned long* LowestResolution, unsigned long* HighestResolution, unsigned long* CurrentResolution);
+typedef long (__stdcall *NtSetTimerResolution_t) (unsigned long RequestedResolution, int Set, unsigned long* ActualResolution);
+
+} //namespace winapi {
+} //namespace interprocess {
+} //namespace boost {
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// Forward declaration of constants
+//
+//////////////////////////////////////////////////////////////////////////////
+
+namespace boost {
+namespace interprocess {
+namespace winapi {
+
+//Some used constants
+static const unsigned long infinite_time = 0xFFFFFFFF;
+static const unsigned long error_already_exists = 183L;
+static const unsigned long error_invalid_handle = 6L;
+static const unsigned long error_sharing_violation = 32L;
+static const unsigned long error_file_not_found = 2u;
+static const unsigned long error_no_more_files = 18u;
+static const unsigned long error_not_locked = 158L;
+//Retries in CreateFile, see http://support.microsoft.com/kb/316609
+static const unsigned long error_sharing_violation_tries = 3L;
+static const unsigned long error_sharing_violation_sleep_ms = 250L;
+static const unsigned long error_file_too_large = 223L;
+static const unsigned long error_insufficient_buffer = 122L;
+static const unsigned long error_handle_eof = 38L;
+static const unsigned long semaphore_all_access = (0x000F0000L)|(0x00100000L)|0x3;
+static const unsigned long mutex_all_access = (0x000F0000L)|(0x00100000L)|0x0001;
+
+static const unsigned long page_readonly = 0x02;
+static const unsigned long page_readwrite = 0x04;
+static const unsigned long page_writecopy = 0x08;
+static const unsigned long page_noaccess = 0x01;
+
+static const unsigned long standard_rights_required = 0x000F0000L;
+static const unsigned long section_query = 0x0001;
+static const unsigned long section_map_write = 0x0002;
+static const unsigned long section_map_read = 0x0004;
+static const unsigned long section_map_execute = 0x0008;
+static const unsigned long section_extend_size = 0x0010;
+static const unsigned long section_all_access = standard_rights_required |
+ section_query |
+ section_map_write |
+ section_map_read |
+ section_map_execute |
+ section_extend_size;
+
+static const unsigned long file_map_copy = section_query;
+static const unsigned long file_map_write = section_map_write;
+static const unsigned long file_map_read = section_map_read;
+static const unsigned long file_map_all_access = section_all_access;
+static const unsigned long delete_access = 0x00010000L;
+static const unsigned long file_flag_backup_semantics = 0x02000000;
+static const long file_flag_delete_on_close = 0x04000000;
+
+//Native API constants
+static const unsigned long file_open_for_backup_intent = 0x00004000;
+static const int file_share_valid_flags = 0x00000007;
+static const long file_delete_on_close = 0x00001000L;
+static const long obj_case_insensitive = 0x00000040L;
+static const long delete_flag = 0x00010000L;
+
+static const unsigned long movefile_copy_allowed = 0x02;
+static const unsigned long movefile_delay_until_reboot = 0x04;
+static const unsigned long movefile_replace_existing = 0x01;
+static const unsigned long movefile_write_through = 0x08;
+static const unsigned long movefile_create_hardlink = 0x10;
+static const unsigned long movefile_fail_if_not_trackable = 0x20;
+
+static const unsigned long file_share_read = 0x00000001;
+static const unsigned long file_share_write = 0x00000002;
+static const unsigned long file_share_delete = 0x00000004;
+
+static const unsigned long file_attribute_readonly = 0x00000001;
+static const unsigned long file_attribute_hidden = 0x00000002;
+static const unsigned long file_attribute_system = 0x00000004;
+static const unsigned long file_attribute_directory = 0x00000010;
+static const unsigned long file_attribute_archive = 0x00000020;
+static const unsigned long file_attribute_device = 0x00000040;
+static const unsigned long file_attribute_normal = 0x00000080;
+static const unsigned long file_attribute_temporary = 0x00000100;
+
+static const unsigned long generic_read = 0x80000000L;
+static const unsigned long generic_write = 0x40000000L;
+
+static const unsigned long wait_object_0 = 0;
+static const unsigned long wait_abandoned = 0x00000080L;
+static const unsigned long wait_timeout = 258L;
+static const unsigned long wait_failed = (unsigned long)0xFFFFFFFF;
+
+static const unsigned long duplicate_close_source = (unsigned long)0x00000001;
+static const unsigned long duplicate_same_access = (unsigned long)0x00000002;
+
+static const unsigned long format_message_allocate_buffer
+ = (unsigned long)0x00000100;
+static const unsigned long format_message_ignore_inserts
+ = (unsigned long)0x00000200;
+static const unsigned long format_message_from_string
+ = (unsigned long)0x00000400;
+static const unsigned long format_message_from_hmodule
+ = (unsigned long)0x00000800;
+static const unsigned long format_message_from_system
+ = (unsigned long)0x00001000;
+static const unsigned long format_message_argument_array
+ = (unsigned long)0x00002000;
+static const unsigned long format_message_max_width_mask
+ = (unsigned long)0x000000FF;
+static const unsigned long lang_neutral = (unsigned long)0x00;
+static const unsigned long sublang_default = (unsigned long)0x01;
+static const unsigned long invalid_file_size = (unsigned long)0xFFFFFFFF;
+static const unsigned long invalid_file_attributes = ((unsigned long)-1);
+static void * const invalid_handle_value = ((void*)(long)(-1));
+
+static const unsigned long file_type_char = 0x0002L;
+static const unsigned long file_type_disk = 0x0001L;
+static const unsigned long file_type_pipe = 0x0003L;
+static const unsigned long file_type_remote = 0x8000L;
+static const unsigned long file_type_unknown = 0x0000L;
+
+static const unsigned long create_new = 1;
+static const unsigned long create_always = 2;
+static const unsigned long open_existing = 3;
+static const unsigned long open_always = 4;
+static const unsigned long truncate_existing = 5;
+
+static const unsigned long file_begin = 0;
+static const unsigned long file_current = 1;
+static const unsigned long file_end = 2;
+
+static const unsigned long lockfile_fail_immediately = 1;
+static const unsigned long lockfile_exclusive_lock = 2;
+static const unsigned long error_lock_violation = 33;
+static const unsigned long security_descriptor_revision = 1;
+
+const unsigned long max_record_buffer_size = 0x10000L; // 64K
+const unsigned long max_path = 260;
+
+//Keys
+static const hkey hkey_local_machine = (hkey)(unsigned long*)(long)(0x80000002);
+static unsigned long key_query_value = 0x0001;
+
+//COM API
+const unsigned long RPC_C_AUTHN_LEVEL_PKT_BIPC = 4;
+const unsigned long RPC_C_AUTHN_DEFAULT_BIPC = 0xffffffffL;
+const unsigned long RPC_C_AUTHZ_DEFAULT_BIPC = 0xffffffffL;
+const unsigned long RPC_C_IMP_LEVEL_IMPERSONATE_BIPC = 3;
+const signed long EOAC_NONE_BIPC = 0;
+const signed long CLSCTX_INPROC_SERVER_BIPC = 0x1;
+const signed long CLSCTX_LOCAL_SERVER_BIPC = 0x4;
+const signed long WBEM_FLAG_RETURN_IMMEDIATELY_BIPC = 0x10;
+const signed long WBEM_FLAG_RETURN_WHEN_COMPLETE_BIPC = 0x0;
+const signed long WBEM_FLAG_FORWARD_ONLY_BIPC = 0x20;
+const signed long WBEM_INFINITE_BIPC = 0xffffffffL;
+const signed long RPC_E_TOO_LATE_BIPC = 0x80010119L;
+const signed long S_OK_BIPC = 0L;
+const signed long S_FALSE_BIPC = 1;
+const signed long RPC_E_CHANGED_MODE_BIPC = 0x80010106L;
+const unsigned long COINIT_APARTMENTTHREADED_BIPC = 0x2;
+const unsigned long COINIT_MULTITHREADED_BIPC = 0x0;
+const unsigned long COINIT_DISABLE_OLE1DDE_BIPC = 0x4;
+const unsigned long COINIT_SPEED_OVER_MEMORY_BIPC = 0x4;
+
+
+//If the user needs to change default COM initialization model,
+//it can define BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL to one of these:
+//
+// COINIT_APARTMENTTHREADED_BIPC
+// COINIT_MULTITHREADED_BIPC
+// COINIT_DISABLE_OLE1DDE_BIPC
+// COINIT_SPEED_OVER_MEMORY_BIPC
+#if !defined(BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL)
+ #define BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL COINIT_APARTMENTTHREADED_BIPC
+#elif (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_APARTMENTTHREADED_BIPC) &&\
+ (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_MULTITHREADED_BIPC) &&\
+ (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_DISABLE_OLE1DDE_BIPC) &&\
+ (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_SPEED_OVER_MEMORY_BIPC)
+ #error "Wrong value for BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL macro"
+#endif
+
+const GUID_BIPC CLSID_WbemAdministrativeLocator =
+ { 0xcb8555cc, 0x9128, 0x11d1, {0xad, 0x9b, 0x00, 0xc0, 0x4f, 0xd8, 0xfd, 0xff}};
+
+const GUID_BIPC IID_IUnknown = { 0x00000000, 0x0000, 0x0000, {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}};
+
+static const unsigned long eventlog_sequential_read = 0x0001;
+static const unsigned long eventlog_backwards_read = 0x0008;
} //namespace winapi {
} //namespace interprocess {
} //namespace boost {
+
namespace boost {
namespace interprocess {
namespace winapi {
@@ -993,10 +1213,13 @@ inline unsigned long make_lang_id(unsigned long p, unsigned long s)
inline void sched_yield()
{
if(!SwitchToThread()){
- Sleep(1);
+ Sleep(0);
}
}
+inline void sleep_tick()
+{ Sleep(1); }
+
inline void sleep(unsigned long ms)
{ Sleep(ms); }
@@ -1015,10 +1238,10 @@ inline unsigned long get_current_process_id()
inline unsigned int close_handle(void* handle)
{ return CloseHandle(handle); }
-inline void * find_first_file(const char *lpFileName, win32_find_data_t *lpFindFileData)
+inline void * find_first_file(const char *lpFileName, win32_find_data *lpFindFileData)
{ return FindFirstFileA(lpFileName, lpFindFileData); }
-inline bool find_next_file(void *hFindFile, win32_find_data_t *lpFindFileData)
+inline bool find_next_file(void *hFindFile, win32_find_data *lpFindFileData)
{ return FindNextFileA(hFindFile, lpFindFileData) != 0; }
inline bool find_close(void *handle)
@@ -1033,7 +1256,7 @@ inline bool duplicate_current_process_handle
, duplicate_same_access);
}
-inline long get_file_type(void *hFile)
+inline unsigned long get_file_type(void *hFile)
{
return GetFileType(hFile);
}
@@ -1125,23 +1348,23 @@ inline void *create_file(const char *name, unsigned long access, unsigned long c
return invalid_handle_value;
}
-inline bool delete_file(const char *name)
-{ return 0 != DeleteFileA(name); }
-
-inline bool move_file_ex(const char *source_filename, const char *destination_filename, unsigned long flags)
-{ return 0 != MoveFileExA(source_filename, destination_filename, flags); }
-
inline void get_system_info(system_info *info)
{ GetSystemInfo(info); }
inline bool flush_view_of_file(void *base_addr, std::size_t numbytes)
{ return 0 != FlushViewOfFile(base_addr, numbytes); }
+inline bool virtual_unlock(void *base_addr, std::size_t numbytes)
+{ return 0 != VirtualUnlock(base_addr, numbytes); }
+
+inline bool virtual_protect(void *base_addr, std::size_t numbytes, unsigned long flNewProtect, unsigned long &lpflOldProtect)
+{ return 0 != VirtualProtect(base_addr, numbytes, flNewProtect, &lpflOldProtect); }
+
inline bool flush_file_buffers(void *handle)
{ return 0 != FlushFileBuffers(handle); }
inline bool get_file_size(void *handle, __int64 &size)
-{ return 0 != GetFileSizeEx(handle, &size); }
+{ return 0 != GetFileSizeEx(handle, (large_integer*)&size); }
inline bool create_directory(const char *name)
{
@@ -1159,7 +1382,10 @@ inline int set_end_of_file(void *handle)
{ return 0 != SetEndOfFile(handle); }
inline bool set_file_pointer_ex(void *handle, __int64 distance, __int64 *new_file_pointer, unsigned long move_method)
-{ return 0 != SetFilePointerEx(handle, distance, new_file_pointer, move_method); }
+{
+ large_integer d; d.QuadPart = distance;
+ return 0 != SetFilePointerEx(handle, d, (large_integer*)new_file_pointer, move_method);
+}
inline bool lock_file_ex(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
{ return 0 != LockFileEx(hnd, flags, reserved, size_low, size_high, overlapped); }
@@ -1192,38 +1418,30 @@ inline long interlocked_exchange(long volatile* addend, long value)
{ return BOOST_INTERLOCKED_EXCHANGE(const_cast<long*>(addend), value); }
//Forward functions
-inline void *load_library(const char *name)
+inline hmodule load_library(const char *name)
{ return LoadLibraryA(name); }
-inline bool free_library(void *module)
+inline bool free_library(hmodule module)
{ return 0 != FreeLibrary(module); }
-inline void *get_proc_address(void *module, const char *name)
+inline farproc_t get_proc_address(hmodule module, const char *name)
{ return GetProcAddress(module, name); }
inline void *get_current_process()
{ return GetCurrentProcess(); }
-inline void *get_module_handle(const char *name)
+inline hmodule get_module_handle(const char *name)
{ return GetModuleHandleA(name); }
-inline unsigned long get_mapped_file_name(void *process, void *lpv, wchar_t *lpfilename, unsigned long nSize)
-{ return GetMappedFileNameW(process, lpv, lpfilename, nSize); }
-
-inline long reg_open_key_ex(void *hKey, const char *lpSubKey, unsigned long ulOptions, unsigned long samDesired, void **phkResult)
+inline long reg_open_key_ex(hkey hKey, const char *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
{ return RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
-inline long reg_query_value_ex(void *hKey, const char *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
+inline long reg_query_value_ex(hkey hKey, const char *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
{ return RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
-inline long reg_close_key(void *hKey)
+inline long reg_close_key(hkey hKey)
{ return RegCloseKey(hKey); }
-inline bool query_performance_counter(__int64 *lpPerformanceCount)
-{
- return 0 != QueryPerformanceCounter(lpPerformanceCount);
-}
-
inline void initialize_object_attributes
( object_attributes_t *pobject_attr, unicode_string_t *name
, unsigned long attr, void *rootdir, void *security_descr)
@@ -1248,75 +1466,134 @@ inline void rtl_init_empty_unicode_string(unicode_string_t *ucStr, wchar_t *buf,
template<int Dummy>
struct function_address_holder
{
- enum { NtSetInformationFile, NtQuerySystemInformation, NtQueryObject, NtQuerySemaphore, NtQuerySection, NumFunction };
- enum { NtDll_dll, NumModule };
+ enum { NtSetInformationFile
+ , NtQuerySystemInformation
+ , NtQueryObject
+ , NtQuerySemaphore
+ , NtQuerySection
+ , NtOpenFile
+ , NtClose
+ , NtQueryTimerResolution
+ , NtSetTimerResolution
+ , QueryPerformanceCounter
+ , QueryPerformanceFrequency
+ , NumFunction
+ };
+ enum { NtDll_dll, Kernel32_dll, NumModule };
private:
- static void *FunctionAddresses[NumFunction];
+ static const char *FunctionNames[NumFunction];
+ static const char *ModuleNames[NumModule];
+ static farproc_t FunctionAddresses[NumFunction];
+ static unsigned int FunctionModules[NumFunction];
static volatile long FunctionStates[NumFunction];
- static void *ModuleAddresses[NumModule];
+ static hmodule ModuleAddresses[NumModule];
static volatile long ModuleStates[NumModule];
- static void *get_module_from_id(unsigned int id)
+ static hmodule get_module_from_id(unsigned int id)
{
- assert(id < (unsigned int)NumModule);
- const char *module[] = { "ntdll.dll" };
- bool compile_check[sizeof(module)/sizeof(module[0]) == NumModule];
- (void)compile_check;
- return get_module_handle(module[id]);
+ BOOST_ASSERT(id < (unsigned int)NumModule);
+ hmodule addr = get_module_handle(ModuleNames[id]);
+ BOOST_ASSERT(addr);
+ return addr;
}
- static void *get_module(const unsigned int id)
+ static hmodule get_module(const unsigned int id)
{
- assert(id < (unsigned int)NumModule);
- while(ModuleStates[id] < 2){
+ BOOST_ASSERT(id < (unsigned int)NumModule);
+ for(unsigned i = 0; ModuleStates[id] < 2; ++i){
if(interlocked_compare_exchange(&ModuleStates[id], 1, 0) == 0){
ModuleAddresses[id] = get_module_from_id(id);
interlocked_increment(&ModuleStates[id]);
break;
}
- else{
+ else if(i & 1){
sched_yield();
}
+ else{
+ sleep_tick();
+ }
}
return ModuleAddresses[id];
}
- static void *get_address_from_dll(const unsigned int id)
+ static farproc_t get_address_from_dll(const unsigned int id)
{
- assert(id < (unsigned int)NumFunction);
- const char *function[] = { "NtSetInformationFile", "NtQuerySystemInformation", "NtQueryObject", "NtQuerySemaphore", "NtQuerySection" };
- bool compile_check[sizeof(function)/sizeof(function[0]) == NumFunction];
- (void)compile_check;
- return get_proc_address(get_module(NtDll_dll), function[id]);
+ BOOST_ASSERT(id < (unsigned int)NumFunction);
+ farproc_t addr = get_proc_address(get_module(FunctionModules[id]), FunctionNames[id]);
+ BOOST_ASSERT(addr);
+ return addr;
}
public:
- static void *get(const unsigned int id)
+ static farproc_t get(const unsigned int id)
{
- assert(id < (unsigned int)NumFunction);
- while(FunctionStates[id] < 2){
+ BOOST_ASSERT(id < (unsigned int)NumFunction);
+ for(unsigned i = 0; FunctionStates[id] < 2; ++i){
if(interlocked_compare_exchange(&FunctionStates[id], 1, 0) == 0){
FunctionAddresses[id] = get_address_from_dll(id);
interlocked_increment(&FunctionStates[id]);
break;
}
- else{
+ else if(i & 1){
sched_yield();
}
+ else{
+ sleep_tick();
+ }
}
return FunctionAddresses[id];
}
};
template<int Dummy>
-void *function_address_holder<Dummy>::FunctionAddresses[function_address_holder<Dummy>::NumFunction];
+const char *function_address_holder<Dummy>::FunctionNames[function_address_holder<Dummy>::NumFunction] =
+{
+ "NtSetInformationFile",
+ "NtQuerySystemInformation",
+ "NtQueryObject",
+ "NtQuerySemaphore",
+ "NtQuerySection",
+ "NtOpenFile",
+ "NtClose",
+ "NtQueryTimerResolution",
+ "NtSetTimerResolution",
+ "QueryPerformanceCounter",
+ "QueryPerformanceFrequency"
+};
+
+template<int Dummy>
+unsigned int function_address_holder<Dummy>::FunctionModules[function_address_holder<Dummy>::NumFunction] =
+{
+ NtDll_dll,
+ NtDll_dll,
+ NtDll_dll,
+ NtDll_dll,
+ NtDll_dll,
+ NtDll_dll,
+ NtDll_dll,
+ NtDll_dll,
+ NtDll_dll,
+ Kernel32_dll,
+ Kernel32_dll
+};
+
+template<int Dummy>
+const char *function_address_holder<Dummy>::ModuleNames[function_address_holder<Dummy>::NumModule] =
+{
+ "ntdll.dll",
+ "kernel32.dll"
+};
+
+
+template<int Dummy>
+farproc_t function_address_holder<Dummy>::FunctionAddresses[function_address_holder<Dummy>::NumFunction];
template<int Dummy>
volatile long function_address_holder<Dummy>::FunctionStates[function_address_holder<Dummy>::NumFunction];
template<int Dummy>
-void *function_address_holder<Dummy>::ModuleAddresses[function_address_holder<Dummy>::NumModule];
+hmodule function_address_holder<Dummy>::ModuleAddresses[function_address_holder<Dummy>::NumModule];
template<int Dummy>
volatile long function_address_holder<Dummy>::ModuleStates[function_address_holder<Dummy>::NumModule];
@@ -1329,52 +1606,11 @@ struct dll_func
//Complex winapi based functions...
struct library_unloader
{
- void *lib_;
- library_unloader(void *module) : lib_(module){}
+ hmodule lib_;
+ library_unloader(hmodule module) : lib_(module){}
~library_unloader(){ free_library(lib_); }
};
-//pszFilename must have room for at least MaxPath+1 characters
-inline bool get_file_name_from_handle_function
- (void * hFile, wchar_t *pszFilename, std::size_t length, std::size_t &out_length)
-{
- if(length <= MaxPath){
- return false;
- }
-
-// void *hiPSAPI = load_library("PSAPI.DLL");
-// if (0 == hiPSAPI)
-// return 0;
-// library_unloader unloader(hiPSAPI);
-
-// Pointer to function getMappedFileName() in PSAPI.DLL
-// GetMappedFileName_t pfGMFN =
-// (GetMappedFileName_t)get_proc_address(hiPSAPI, "GetMappedFileNameW");
-// if (! pfGMFN){
-// return 0; // Failed: unexpected error
-// }
-
- bool bSuccess = false;
-
- // Create a file mapping object.
- void * hFileMap = create_file_mapping(hFile, page_readonly, 1, 0, 0);
- if(hFileMap){
- // Create a file mapping to get the file name.
- void* pMem = map_view_of_file_ex(hFileMap, file_map_read, 0, 1, 0);
-
- if (pMem){
- //out_length = pfGMFN(get_current_process(), pMem, pszFilename, MaxPath);
- out_length = get_mapped_file_name(get_current_process(), pMem, pszFilename, MaxPath);
- if(out_length){
- bSuccess = true;
- }
- unmap_view_of_file(pMem);
- }
- close_handle(hFileMap);
- }
-
- return(bSuccess);
-}
inline bool get_system_time_of_day_information(system_timeofday_information &info)
{
@@ -1410,7 +1646,8 @@ inline bool get_boot_and_system_time(unsigned char (&bootsystemstamp) [BootAndSy
return true;
}
-inline bool get_boot_time_str(char *bootstamp_str, std::size_t &s) //will write BootstampLength chars
+inline bool get_boot_time_str(char *bootstamp_str, std::size_t &s)
+ //will write BootstampLength chars
{
if(s < (BootstampLength*2))
return false;
@@ -1431,7 +1668,23 @@ inline bool get_boot_time_str(char *bootstamp_str, std::size_t &s) //will write
return true;
}
-inline bool get_boot_and_system_time_wstr(wchar_t *bootsystemstamp, std::size_t &s) //will write BootAndSystemstampLength chars
+//Writes the hexadecimal value of the buffer, in the wide character string.
+//str must be twice length
+inline void buffer_to_wide_str(const void *buf, std::size_t length, wchar_t *str)
+{
+ const wchar_t Characters [] =
+ { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7'
+ , L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
+ std::size_t char_counter = 0;
+ const char *chbuf = static_cast<const char *>(buf);
+ for(std::size_t i = 0; i != length; ++i){
+ str[char_counter++] = Characters[(chbuf[i]&0xF0)>>4];
+ str[char_counter++] = Characters[(chbuf[i]&0x0F)];
+ }
+}
+
+inline bool get_boot_and_system_time_wstr(wchar_t *bootsystemstamp, std::size_t &s)
+ //will write BootAndSystemstampLength chars
{
if(s < (BootAndSystemstampLength*2))
return false;
@@ -1440,14 +1693,8 @@ inline bool get_boot_and_system_time_wstr(wchar_t *bootsystemstamp, std::size_t
if(!ret){
return false;
}
- const wchar_t Characters [] =
- { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7'
- , L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
- std::size_t char_counter = 0;
- for(std::size_t i = 0; i != static_cast<std::size_t>(BootAndSystemstampLength); ++i){
- bootsystemstamp[char_counter++] = Characters[(info.Reserved1[i]&0xF0)>>4];
- bootsystemstamp[char_counter++] = Characters[(info.Reserved1[i]&0x0F)];
- }
+
+ buffer_to_wide_str(&info.Reserved1[0], BootAndSystemstampLength, bootsystemstamp);
s = BootAndSystemstampLength*2;
return true;
}
@@ -1463,16 +1710,99 @@ class handle_closer
{ close_handle(handle_); }
};
+class eventlog_handle_closer
+{
+ void *handle_;
+ eventlog_handle_closer(const handle_closer &);
+ eventlog_handle_closer& operator=(const eventlog_handle_closer &);
+ public:
+ explicit eventlog_handle_closer(void *handle) : handle_(handle){}
+ ~eventlog_handle_closer()
+ { CloseEventLog(handle_); }
+};
+
union ntquery_mem_t
{
object_name_information_t name;
struct ren_t
{
file_rename_information_t info;
- wchar_t buf[32767];
+ wchar_t buf[1];
} ren;
};
+class nt_query_mem_deleter
+{
+ static const std::size_t rename_offset = offsetof(ntquery_mem_t, ren.info.FileName) -
+ offsetof(ntquery_mem_t, name.Name.Buffer);
+ // Timestamp process id atomic count
+ static const std::size_t rename_suffix =
+ (SystemTimeOfDayInfoLength + sizeof(unsigned long) + sizeof(boost::uint32_t))*2;
+
+ public:
+ nt_query_mem_deleter(std::size_t object_name_information_size)
+ : m_size(object_name_information_size + rename_offset + rename_suffix)
+ , m_buf(new char [m_size])
+ {}
+
+ ~nt_query_mem_deleter()
+ {
+ delete[]m_buf;
+ }
+
+ void realloc_mem(std::size_t num_bytes)
+ {
+ num_bytes += rename_suffix + rename_offset;
+ char *buf = m_buf;
+ m_buf = new char[num_bytes];
+ delete[]buf;
+ m_size = num_bytes;
+ }
+
+ ntquery_mem_t *query_mem() const
+ { return static_cast<ntquery_mem_t *>(static_cast<void*>(m_buf)); }
+
+ unsigned long object_name_information_size() const
+ {
+ return static_cast<unsigned long>(m_size - rename_offset - SystemTimeOfDayInfoLength*2);
+ }
+
+ std::size_t file_rename_information_size() const
+ { return static_cast<unsigned long>(m_size); }
+
+ private:
+ std::size_t m_size;
+ char *m_buf;
+};
+
+class c_heap_deleter
+{
+ public:
+ c_heap_deleter(std::size_t size)
+ : m_buf(::malloc(size))
+ {}
+
+ ~c_heap_deleter()
+ {
+ if(m_buf) ::free(m_buf);
+ }
+
+ void realloc_mem(std::size_t num_bytes)
+ {
+ void *buf = ::realloc(m_buf, num_bytes);
+ if(!buf){
+ free(m_buf);
+ m_buf = 0;
+ }
+ }
+
+ void *get() const
+ { return m_buf; }
+
+ private:
+ void *m_buf;
+};
+
inline bool unlink_file(const char *filename)
{
//Don't try to optimize doing a DeleteFile first
@@ -1493,71 +1823,96 @@ inline bool unlink_file(const char *filename)
try{
NtSetInformationFile_t pNtSetInformationFile =
(NtSetInformationFile_t)dll_func::get(dll_func::NtSetInformationFile);
- if(!pNtSetInformationFile){
- return false;
- }
- NtQueryObject_t pNtQueryObject =
- (NtQueryObject_t)dll_func::get(dll_func::NtQueryObject);
+ NtQueryObject_t pNtQueryObject = (NtQueryObject_t)dll_func::get(dll_func::NtQueryObject);
//First step: Obtain a handle to the file using Win32 rules. This resolves relative paths
- void *fh = create_file(filename, generic_read | delete_access, open_existing,
- file_flag_backup_semantics | file_flag_delete_on_close, 0);
+ void *fh = create_file(filename, generic_read | delete_access, open_existing, 0, 0);
if(fh == invalid_handle_value){
return false;
}
handle_closer h_closer(fh);
+ {
+ //Obtain name length
+ unsigned long size;
+ const std::size_t initial_string_mem = 512u;
+
+ nt_query_mem_deleter nt_query_mem(sizeof(ntquery_mem_t)+initial_string_mem);
+ //Obtain file name with guessed length
+ if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
+ //Obtain file name with exact length buffer
+ nt_query_mem.realloc_mem(size);
+ if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
+ return false;
+ }
+ }
+ ntquery_mem_t *pmem = nt_query_mem.query_mem();
+ file_rename_information_t *pfri = &pmem->ren.info;
+ const std::size_t RenMaxNumChars =
+ (((char*)(pmem) + nt_query_mem.file_rename_information_size()) - (char*)&pmem->ren.info.FileName[0])/sizeof(wchar_t);
+
+ //Copy filename to the rename member
+ std::memmove(pmem->ren.info.FileName, pmem->name.Name.Buffer, pmem->name.Name.Length);
+ std::size_t filename_string_length = pmem->name.Name.Length/sizeof(wchar_t);
+
+ //Search '\\' character to replace from it
+ for(std::size_t i = filename_string_length; i != 0; --filename_string_length){
+ if(pmem->ren.info.FileName[--i] == L'\\')
+ break;
+ }
- std::auto_ptr<ntquery_mem_t> pmem(new ntquery_mem_t);
- file_rename_information_t *pfri = &pmem->ren.info;
- const std::size_t RenMaxNumChars =
- ((char*)pmem.get() - (char*)&pmem->ren.info.FileName[0])/sizeof(wchar_t);
-
- //Obtain file name
- unsigned long size;
- if(pNtQueryObject(fh, object_name_information, pmem.get(), sizeof(ntquery_mem_t), &size)){
- return false;
- }
-
- //Copy filename to the rename member
- std::memmove(pmem->ren.info.FileName, pmem->name.Name.Buffer, pmem->name.Name.Length);
- std::size_t filename_string_length = pmem->name.Name.Length/sizeof(wchar_t);
-
- //Second step: obtain the complete native-nt filename
- //if(!get_file_name_from_handle_function(fh, pfri->FileName, RenMaxNumChars, filename_string_length)){
- //return 0;
- //}
-
- //Add trailing mark
- if((RenMaxNumChars-filename_string_length) < (SystemTimeOfDayInfoLength*2)){
- return false;
- }
-
- //Search '\\' character to replace it
- for(std::size_t i = filename_string_length; i != 0; --filename_string_length){
- if(pmem->ren.info.FileName[--i] == L'\\')
- break;
- }
-
- //Add random number
- std::size_t s = RenMaxNumChars - filename_string_length;
- if(!get_boot_and_system_time_wstr(&pfri->FileName[filename_string_length], s)){
- return false;
+ //Add random number
+ std::size_t s = RenMaxNumChars - filename_string_length;
+ if(!get_boot_and_system_time_wstr(&pfri->FileName[filename_string_length], s)){
+ return false;
+ }
+ filename_string_length += s;
+
+ //Sometimes the precission of the timestamp is not enough and we need to add another random number.
+ //The process id (to exclude concurrent processes) and an atomic count (to exclude concurrent threads).
+ //should be enough
+ const unsigned long pid = get_current_process_id();
+ buffer_to_wide_str(&pid, sizeof(pid), &pfri->FileName[filename_string_length]);
+ filename_string_length += sizeof(pid)*2;
+
+ static volatile boost::uint32_t u32_count = 0;
+ interlocked_decrement(reinterpret_cast<volatile long*>(&u32_count));
+ buffer_to_wide_str(const_cast<const boost::uint32_t *>(&u32_count), sizeof(boost::uint32_t), &pfri->FileName[filename_string_length]);
+ filename_string_length += sizeof(boost::uint32_t)*2;
+
+ //Fill rename information (FileNameLength is in bytes)
+ pfri->FileNameLength = static_cast<unsigned long>(sizeof(wchar_t)*(filename_string_length));
+ pfri->Replace = 1;
+ pfri->RootDir = 0;
+
+ //Cange the name of the in-use file...
+ io_status_block_t io;
+ if(0 != pNtSetInformationFile(fh, &io, pfri, nt_query_mem.file_rename_information_size(), file_rename_information)){
+ return false;
+ }
}
- filename_string_length += s;
-
- //Fill rename information (FileNameLength is in bytes)
- pfri->FileNameLength = static_cast<unsigned long>(sizeof(wchar_t)*(filename_string_length));
- pfri->Replace = 1;
- pfri->RootDir = 0;
-
- //Final step: change the name of the in-use file:
- io_status_block_t io;
- if(0 != pNtSetInformationFile(fh, &io, pfri, sizeof(ntquery_mem_t::ren_t), file_rename_information)){
- return false;
+ //...and mark it as delete-on-close
+ {
+ //Don't use pNtSetInformationFile with file_disposition_information as it can return STATUS_CANNOT_DELETE
+ //if the file is still mapped. Reopen it with NtOpenFile and file_delete_on_close
+ NtOpenFile_t pNtOpenFile = (NtOpenFile_t)dll_func::get(dll_func::NtOpenFile);
+ NtClose_t pNtClose = (NtClose_t)dll_func::get(dll_func::NtClose);
+ const wchar_t empty_str [] = L"";
+ unicode_string_t ustring = { sizeof(empty_str) - sizeof (wchar_t) //length in bytes without null
+ , sizeof(empty_str) //total size in bytes of memory allocated for Buffer.
+ , const_cast<wchar_t*>(empty_str)
+ };
+ object_attributes_t object_attr;
+ initialize_object_attributes(&object_attr, &ustring, 0, fh, 0);
+ void* fh2 = 0;
+ io_status_block_t io;
+ pNtOpenFile( &fh2, delete_flag, &object_attr, &io
+ , file_share_read | file_share_write | file_share_delete, file_delete_on_close);
+ pNtClose(fh2);
+ //Even if NtOpenFile fails, the file was renamed and the original no longer exists, so return a success status
+ return true;
}
- return true;
}
catch(...){
return false;
@@ -1567,18 +1922,16 @@ inline bool unlink_file(const char *filename)
struct reg_closer
{
- //reg_closer(RegCloseKey_t func, void *key) : func_(func), key_(key){}
- //~reg_closer(){ (*func_)(key_); }
- //RegCloseKey_t func_;
- void *key_;
- reg_closer(void *key) : key_(key){}
+ hkey key_;
+ reg_closer(hkey key) : key_(key){}
~reg_closer(){ reg_close_key(key_); }
};
inline void get_shared_documents_folder(std::string &s)
{
+ #if 1 //Original registry search code
s.clear();
- void *key;
+ hkey key;
if (reg_open_key_ex( hkey_local_machine
, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
, 0
@@ -1602,12 +1955,24 @@ inline void get_shared_documents_folder(std::string &s)
(void)err;
}
}
+ #else //registry alternative: SHGetSpecialFolderPathA
+ const int BIPC_CSIDL_COMMON_APPDATA = 0x0023; // All Users\Application Data
+ const int BIPC_CSIDL_FLAG_CREATE = 0x8000; // new for Win2K, or this in to force creation of folder
+ const int BIPC_SHGFP_TYPE_CURRENT = 0; // current value for user, verify it exists
+
+ s.clear();
+ char szPath[max_path];
+ if(0 == SHGetFolderPathA(0, BIPC_CSIDL_COMMON_APPDATA | BIPC_CSIDL_FLAG_CREATE, 0, BIPC_SHGFP_TYPE_CURRENT, szPath)){
+ s = szPath;
+ }
+
+ #endif
}
inline void get_registry_value(const char *folder, const char *value_key, std::vector<unsigned char> &s)
{
s.clear();
- void *key;
+ hkey key;
if (reg_open_key_ex( hkey_local_machine
, folder
, 0
@@ -1700,7 +2065,7 @@ inline bool get_wmi_class_attribute( std::wstring& strValue, const wchar_t *wmi_
IWbemServices_BIPC *pWbemServices = 0;
if( 0 != pIWbemLocator->ConnectServer(
- bstrNamespace, // Namespace
+ (bstr)bstrNamespace, // Namespace
0, // Userid
0, // PW
0, // Locale
@@ -1738,8 +2103,8 @@ inline bool get_wmi_class_attribute( std::wstring& strValue, const wchar_t *wmi_
IEnumWbemClassObject_BIPC * pEnumObject = 0;
if ( 0 != pWbemServices->ExecQuery(
- L"WQL",
- strValue.c_str(),
+ (bstr)L"WQL",
+ (bstr)strValue.c_str(),
//WBEM_FLAG_RETURN_IMMEDIATELY_BIPC,
WBEM_FLAG_RETURN_WHEN_COMPLETE_BIPC | WBEM_FLAG_FORWARD_ONLY_BIPC,
0,
@@ -1762,9 +2127,9 @@ inline bool get_wmi_class_attribute( std::wstring& strValue, const wchar_t *wmi_
while( 0 == pEnumObject->Next( WBEM_INFINITE_BIPC, uCount, &pClassObject, &uReturned ) )
{
com_releaser<IWbemClassObject_BIPC> IWbemClassObject_releaser(pClassObject);
- if ( 0 == pClassObject->Get( L"LastBootUpTime", 0, &vwchar, 0, 0 ) ){
+ if ( 0 == pClassObject->Get( (bstr)L"LastBootUpTime", 0, &vwchar, 0, 0 ) ){
bRet = true;
- strValue = vwchar.value.pbstrVal;
+ strValue = (wchar_t*)vwchar.bstrVal;
VariantClear(&vwchar );
break;
}
@@ -1773,6 +2138,10 @@ inline bool get_wmi_class_attribute( std::wstring& strValue, const wchar_t *wmi_
return bRet;
}
+#ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME
+
+//Obtains the bootup time from WMI LastBootUpTime.
+//This time seems to change with hibernation and clock synchronization so avoid it.
inline bool get_last_bootup_time( std::wstring& strValue )
{
bool ret = get_wmi_class_attribute(strValue, L"Win32_OperatingSystem", L"LastBootUpTime");
@@ -1798,12 +2167,110 @@ inline bool get_last_bootup_time( std::string& str )
return ret;
}
+#else
+
+// Loop through the buffer and obtain the contents of the
+// requested record in the buffer.
+inline bool find_record_in_buffer( const void* pBuffer, unsigned long dwBytesRead, const char *provider_name
+ , unsigned int id_to_find, interprocess_eventlogrecord *&pevent_log_record)
+{
+ const unsigned char * pRecord = static_cast<const unsigned char*>(pBuffer);
+ const unsigned char * pEndOfRecords = pRecord + dwBytesRead;
+
+ while (pRecord < pEndOfRecords){
+ interprocess_eventlogrecord *pTypedRecord = (interprocess_eventlogrecord*)pRecord;
+ // Check provider, written at the end of the fixed-part of the record
+ if (0 == std::strcmp(provider_name, (char*)(pRecord + sizeof(interprocess_eventlogrecord))))
+ {
+ // Check event id
+ if(id_to_find == (pTypedRecord->EventID & 0xFFFF)){
+ pevent_log_record = pTypedRecord;
+ return true;
+ }
+ }
+
+ pRecord += pTypedRecord->Length;
+ }
+ pevent_log_record = 0;
+ return false;
+}
+
+//Obtains the bootup time from the System Event Log,
+//event ID == 6005 (event log started).
+//Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/bb427356.aspx
+inline bool get_last_bootup_time(std::string &stamp)
+{
+ const char *source_name = "System";
+ const char *provider_name = "EventLog";
+ const unsigned short event_id = 6005u;
+
+ unsigned long status = 0;
+ unsigned long dwBytesToRead = 0;
+ unsigned long dwBytesRead = 0;
+ unsigned long dwMinimumBytesToRead = 0;
+
+ // The source name (provider) must exist as a subkey of Application.
+ void *hEventLog = OpenEventLogA(0, source_name);
+ if (hEventLog){
+ eventlog_handle_closer hnd_closer(hEventLog); (void)hnd_closer;
+ // Allocate an initial block of memory used to read event records. The number
+ // of records read into the buffer will vary depending on the size of each event.
+ // The size of each event will vary based on the size of the user-defined
+ // data included with each event, the number and length of insertion
+ // strings, and other data appended to the end of the event record.
+ dwBytesToRead = max_record_buffer_size;
+ c_heap_deleter heap_deleter(dwBytesToRead);
+
+ // Read blocks of records until you reach the end of the log or an
+ // error occurs. The records are read from newest to oldest. If the buffer
+ // is not big enough to hold a complete event record, reallocate the buffer.
+ if (heap_deleter.get() != 0){
+ while (0 == status){
+ if (!ReadEventLogA(hEventLog,
+ eventlog_sequential_read | eventlog_backwards_read,
+ 0,
+ heap_deleter.get(),
+ dwBytesToRead,
+ &dwBytesRead,
+ &dwMinimumBytesToRead)) {
+ status = get_last_error();
+ if (error_insufficient_buffer == status) {
+ status = 0;
+ dwBytesToRead = dwMinimumBytesToRead;
+ heap_deleter.realloc_mem(dwMinimumBytesToRead);
+ if (!heap_deleter.get()){
+ return false;
+ }
+ }
+ else{ //Not found or EOF
+ return false;
+ }
+ }
+ else
+ {
+ interprocess_eventlogrecord *pTypedRecord;
+ // Print the contents of each record in the buffer.
+ if(find_record_in_buffer(heap_deleter.get(), dwBytesRead, provider_name, event_id, pTypedRecord)){
+ char stamp_str[sizeof(unsigned long)*3+1];
+ std::sprintf(&stamp_str[0], "%u", ((unsigned int)pTypedRecord->TimeGenerated));
+ stamp = stamp_str;
+ break;
+ }
+ }
+ }
+ }
+ }
+ return true;
+}
+
+#endif
+
inline bool is_directory(const char *path)
{
- unsigned long attrib = GetFileAttributesA(path);
+ unsigned long attrib = GetFileAttributesA(path);
- return (attrib != invalid_file_attributes &&
- (attrib & file_attribute_directory));
+ return (attrib != invalid_file_attributes &&
+ (attrib & file_attribute_directory));
}
inline bool get_file_mapping_size(void *file_mapping_hnd, __int64 &size)
@@ -1814,11 +2281,8 @@ inline bool get_file_mapping_size(void *file_mapping_hnd, __int64 &size)
interprocess_section_basic_information info;
unsigned long ntstatus =
pNtQuerySection(file_mapping_hnd, section_basic_information, &info, sizeof(info), 0);
- if(ntstatus){
- return false;
- }
size = info.section_size;
- return true;
+ return !ntstatus;
}
inline bool get_semaphore_info(void *handle, long &count, long &limit)
@@ -1828,14 +2292,41 @@ inline bool get_semaphore_info(void *handle, long &count, long &limit)
(winapi::NtQuerySemaphore_t)dll_func::get(winapi::dll_func::NtQuerySemaphore);
unsigned int ret_len;
long status = pNtQuerySemaphore(handle, winapi::semaphore_basic_information, &info, sizeof(info), &ret_len);
- if(status){
- return false;
- }
count = info.count;
limit = info.limit;
- return true;
+ return !status;
+}
+
+inline bool query_timer_resolution(unsigned long *lowres, unsigned long *highres, unsigned long *curres)
+{
+ winapi::NtQueryTimerResolution_t pNtQueryTimerResolution =
+ (winapi::NtQueryTimerResolution_t)dll_func::get(winapi::dll_func::NtQueryTimerResolution);
+ return !pNtQueryTimerResolution(lowres, highres, curres);
+}
+
+inline bool set_timer_resolution(unsigned long RequestedResolution, int Set, unsigned long* ActualResolution)
+{
+ winapi::NtSetTimerResolution_t pNtSetTimerResolution =
+ (winapi::NtSetTimerResolution_t)dll_func::get(winapi::dll_func::NtSetTimerResolution);
+ return !pNtSetTimerResolution(RequestedResolution, Set, ActualResolution);
+}
+
+inline bool query_performance_counter(__int64 *lpPerformanceCount)
+{
+ QueryPerformanceCounter_t pQueryPerformanceCounter = (QueryPerformanceCounter_t)
+ dll_func::get(dll_func::QueryPerformanceCounter);
+ return 0 != pQueryPerformanceCounter(lpPerformanceCount);
+}
+
+inline bool query_performance_frequency(__int64 *lpFrequency)
+{
+ QueryPerformanceCounter_t pQueryPerformanceFrequency = (QueryPerformanceFrequency_t)
+ dll_func::get(dll_func::QueryPerformanceFrequency);
+ return 0 != pQueryPerformanceFrequency(lpFrequency);
}
+inline unsigned long get_tick_count()
+{ return GetTickCount(); }
} //namespace winapi
} //namespace interprocess
@@ -1843,4 +2334,4 @@ inline bool get_semaphore_info(void *handle, long &count, long &limit)
#include <boost/interprocess/detail/config_end.hpp>
-#endif //#ifdef BOOST_INTERPROCESS_WIN32_PRIMITIVES_HPP
+#endif //#ifdef BOOST_INTERPROCESS_WIN32_API_HPP