win32_api.hpp 69 KB

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