win32_api.hpp 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  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/date_time/filetime_functions.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. //#define BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME
  30. //#define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
  31. //#define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
  32. #ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME
  33. # define BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME_VALUE 1
  34. #else
  35. # define BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME_VALUE 0
  36. #endif
  37. #ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
  38. # define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE 1
  39. #else
  40. # define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE 0
  41. #endif
  42. #ifdef BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
  43. # define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE 1
  44. #else
  45. # define BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE 0
  46. #endif
  47. #define BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM \
  48. (BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED_VALUE + \
  49. BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED_VALUE + \
  50. BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME_VALUE)
  51. #if 1 < BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM
  52. # error "Only one of BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME, \
  53. BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED and \
  54. BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED can be defined"
  55. #endif
  56. #if 0 == BOOST_INTERPROCESS_BOOTSTAMP_VALUE_SUM
  57. # define BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
  58. #endif
  59. #ifdef BOOST_USE_WINDOWS_H
  60. #include <windows.h>
  61. # if defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME)
  62. # include <wbemidl.h>
  63. # include <objbase.h>
  64. # endif
  65. #include <shlobj.h>
  66. #endif
  67. #if defined(_MSC_VER)
  68. # pragma once
  69. # pragma comment( lib, "Advapi32.lib" )
  70. # pragma comment( lib, "oleaut32.lib" )
  71. # pragma comment( lib, "Ole32.lib" )
  72. # pragma comment( lib, "Shell32.lib" ) //SHGetFolderPath
  73. #endif
  74. #if defined (BOOST_INTERPROCESS_WINDOWS)
  75. # include <cstdarg>
  76. # include <boost/detail/interlocked.hpp>
  77. #else
  78. # error "This file can only be included in Windows OS"
  79. #endif
  80. //////////////////////////////////////////////////////////////////////////////
  81. //
  82. // Declaration of Windows structures or typedefs if BOOST_USE_WINDOWS_H is used
  83. //
  84. //////////////////////////////////////////////////////////////////////////////
  85. #if defined(BOOST_GCC)
  86. //Ignore -pedantic errors here (anonymous structs, etc.)
  87. # if (BOOST_GCC >= 40600)
  88. # pragma GCC diagnostic push
  89. # if (BOOST_GCC >= 40800)
  90. # pragma GCC diagnostic ignored "-Wpedantic"
  91. # else
  92. # pragma GCC diagnostic ignored "-pedantic"
  93. # endif
  94. # pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  95. # else
  96. # pragma GCC system_header
  97. # endif
  98. //When loading DLLs we have no option but reinterpret casting function types
  99. # if (BOOST_GCC >= 80000)
  100. # pragma GCC diagnostic ignored "-Wcast-function-type"
  101. # endif
  102. #endif
  103. namespace boost {
  104. namespace interprocess {
  105. namespace winapi {
  106. //Own defines
  107. static const unsigned long MaxPath = 260;
  108. #ifndef BOOST_USE_WINDOWS_H
  109. struct GUID_BIPC
  110. {
  111. unsigned long Data1;
  112. unsigned short Data2;
  113. unsigned short Data3;
  114. unsigned char Data4[8];
  115. };
  116. #if defined(_MSC_VER)
  117. #pragma warning (push)
  118. #pragma warning (disable : 4201) // nonstandard extension used
  119. #endif
  120. struct decimal
  121. {
  122. unsigned short wReserved;
  123. union {
  124. struct {
  125. unsigned char scale;
  126. unsigned char sign;
  127. };
  128. unsigned short signscale;
  129. };
  130. unsigned long Hi32;
  131. union {
  132. struct {
  133. unsigned long Lo32;
  134. unsigned long Mid32;
  135. };
  136. ::boost::ulong_long_type Lo64;
  137. };
  138. };
  139. typedef unsigned short *bstr;
  140. struct wchar_variant
  141. {
  142. union
  143. {
  144. struct
  145. {
  146. unsigned short vt;
  147. unsigned short wReserved1;
  148. unsigned short wReserved2;
  149. unsigned short wReserved3;
  150. union
  151. {
  152. bstr bstrVal;
  153. struct
  154. {
  155. void* pvRecord;
  156. void* pRecInfo;
  157. };
  158. };
  159. };
  160. decimal decVal;
  161. };
  162. };
  163. #if defined(_MSC_VER)
  164. #pragma warning (pop)
  165. #endif
  166. struct IUnknown_BIPC
  167. {
  168. public:
  169. virtual long __stdcall QueryInterface(
  170. const GUID_BIPC &riid, // [in]
  171. void **ppvObject) = 0; // [iid_is][out]
  172. virtual unsigned long __stdcall AddRef (void) = 0;
  173. virtual unsigned long __stdcall Release(void) = 0;
  174. };
  175. struct IWbemClassObject_BIPC : public IUnknown_BIPC
  176. {
  177. public:
  178. virtual long __stdcall GetQualifierSet(
  179. /* [out] */ void **ppQualSet) = 0;
  180. virtual long __stdcall Get(
  181. /* [string][in] */ const bstr wszName,
  182. /* [in] */ long lFlags,
  183. /* [unique][in][out] */ wchar_variant *pVal,
  184. /* [unique][in][out] */ long *pType,
  185. /* [unique][in][out] */ long *plFlavor) = 0;
  186. virtual long __stdcall Put(
  187. /* [string][in] */ const bstr wszName,
  188. /* [in] */ long lFlags,
  189. /* [in] */ wchar_variant *pVal,
  190. /* [in] */ long Type) = 0;
  191. virtual long __stdcall Delete(
  192. /* [string][in] */ const bstr wszName) = 0;
  193. virtual long __stdcall GetNames(
  194. /* [string][in] */ const bstr wszQualifierName,
  195. /* [in] */ long lFlags,
  196. /* [in] */ wchar_variant *pQualifierVal,
  197. /* [out] */ void * *pNames) = 0;
  198. virtual long __stdcall BeginEnumeration(
  199. /* [in] */ long lEnumFlags) = 0;
  200. virtual long __stdcall Next(
  201. /* [in] */ long lFlags,
  202. /* [unique][in][out] */ bstr *strName,
  203. /* [unique][in][out] */ wchar_variant *pVal,
  204. /* [unique][in][out] */ long *pType,
  205. /* [unique][in][out] */ long *plFlavor) = 0;
  206. virtual long __stdcall EndEnumeration( void) = 0;
  207. virtual long __stdcall GetPropertyQualifierSet(
  208. /* [string][in] */ const bstr wszProperty,
  209. /* [out] */ void **ppQualSet) = 0;
  210. virtual long __stdcall Clone(
  211. /* [out] */ IWbemClassObject_BIPC **ppCopy) = 0;
  212. virtual long __stdcall GetObjectText(
  213. /* [in] */ long lFlags,
  214. /* [out] */ bstr *pstrObjectText) = 0;
  215. virtual long __stdcall SpawnDerivedClass(
  216. /* [in] */ long lFlags,
  217. /* [out] */ IWbemClassObject_BIPC **ppNewClass) = 0;
  218. virtual long __stdcall SpawnInstance(
  219. /* [in] */ long lFlags,
  220. /* [out] */ IWbemClassObject_BIPC **ppNewInstance) = 0;
  221. virtual long __stdcall CompareTo(
  222. /* [in] */ long lFlags,
  223. /* [in] */ IWbemClassObject_BIPC *pCompareTo) = 0;
  224. virtual long __stdcall GetPropertyOrigin(
  225. /* [string][in] */ const bstr wszName,
  226. /* [out] */ bstr *pstrClassName) = 0;
  227. virtual long __stdcall InheritsFrom(
  228. /* [in] */ const bstr strAncestor) = 0;
  229. virtual long __stdcall GetMethod(
  230. /* [string][in] */ const bstr wszName,
  231. /* [in] */ long lFlags,
  232. /* [out] */ IWbemClassObject_BIPC **ppInSignature,
  233. /* [out] */ IWbemClassObject_BIPC **ppOutSignature) = 0;
  234. virtual long __stdcall PutMethod(
  235. /* [string][in] */ const bstr wszName,
  236. /* [in] */ long lFlags,
  237. /* [in] */ IWbemClassObject_BIPC *pInSignature,
  238. /* [in] */ IWbemClassObject_BIPC *pOutSignature) = 0;
  239. virtual long __stdcall DeleteMethod(
  240. /* [string][in] */ const bstr wszName) = 0;
  241. virtual long __stdcall BeginMethodEnumeration(
  242. /* [in] */ long lEnumFlags) = 0;
  243. virtual long __stdcall NextMethod(
  244. /* [in] */ long lFlags,
  245. /* [unique][in][out] */ bstr *pstrName,
  246. /* [unique][in][out] */ IWbemClassObject_BIPC **ppInSignature,
  247. /* [unique][in][out] */ IWbemClassObject_BIPC **ppOutSignature) = 0;
  248. virtual long __stdcall EndMethodEnumeration( void) = 0;
  249. virtual long __stdcall GetMethodQualifierSet(
  250. /* [string][in] */ const bstr wszMethod,
  251. /* [out] */ void **ppQualSet) = 0;
  252. virtual long __stdcall GetMethodOrigin(
  253. /* [string][in] */ const bstr wszMethodName,
  254. /* [out] */ bstr *pstrClassName) = 0;
  255. };
  256. struct IWbemContext_BIPC : public IUnknown_BIPC
  257. {
  258. public:
  259. virtual long __stdcall Clone(
  260. /* [out] */ IWbemContext_BIPC **ppNewCopy) = 0;
  261. virtual long __stdcall GetNames(
  262. /* [in] */ long lFlags,
  263. /* [out] */ void * *pNames) = 0;
  264. virtual long __stdcall BeginEnumeration(
  265. /* [in] */ long lFlags) = 0;
  266. virtual long __stdcall Next(
  267. /* [in] */ long lFlags,
  268. /* [out] */ bstr *pstrName,
  269. /* [out] */ wchar_variant *pValue) = 0;
  270. virtual long __stdcall EndEnumeration( void) = 0;
  271. virtual long __stdcall SetValue(
  272. /* [string][in] */ const bstr wszName,
  273. /* [in] */ long lFlags,
  274. /* [in] */ wchar_variant *pValue) = 0;
  275. virtual long __stdcall GetValue(
  276. /* [string][in] */ const bstr wszName,
  277. /* [in] */ long lFlags,
  278. /* [out] */ wchar_variant *pValue) = 0;
  279. virtual long __stdcall DeleteValue(
  280. /* [string][in] */ const bstr wszName,
  281. /* [in] */ long lFlags) = 0;
  282. virtual long __stdcall DeleteAll( void) = 0;
  283. };
  284. struct IEnumWbemClassObject_BIPC : public IUnknown_BIPC
  285. {
  286. public:
  287. virtual long __stdcall Reset( void) = 0;
  288. virtual long __stdcall Next(
  289. /* [in] */ long lTimeout,
  290. /* [in] */ unsigned long uCount,
  291. /* [length_is][size_is][out] */ IWbemClassObject_BIPC **apObjects,
  292. /* [out] */ unsigned long *puReturned) = 0;
  293. virtual long __stdcall NextAsync(
  294. /* [in] */ unsigned long uCount,
  295. /* [in] */ void *pSink) = 0;
  296. virtual long __stdcall Clone(
  297. /* [out] */ void **ppEnum) = 0;
  298. virtual long __stdcall Skip(
  299. /* [in] */ long lTimeout,
  300. /* [in] */ unsigned long nCount) = 0;
  301. };
  302. struct IWbemServices_BIPC : public IUnknown_BIPC
  303. {
  304. public:
  305. virtual long __stdcall OpenNamespace(
  306. /* [in] */ const bstr strNamespace,
  307. /* [in] */ long lFlags,
  308. /* [in] */ void *pCtx,
  309. /* [unique][in][out] */ void **ppWorkingNamespace,
  310. /* [unique][in][out] */ void **ppResult) = 0;
  311. virtual long __stdcall CancelAsyncCall(
  312. /* [in] */ void *pSink) = 0;
  313. virtual long __stdcall QueryObjectSink(
  314. /* [in] */ long lFlags,
  315. /* [out] */ void **ppResponseHandler) = 0;
  316. virtual long __stdcall GetObject(
  317. /* [in] */ const bstr strObjectPath,
  318. /* [in] */ long lFlags,
  319. /* [in] */ void *pCtx,
  320. /* [unique][in][out] */ void **ppObject,
  321. /* [unique][in][out] */ void **ppCallResult) = 0;
  322. virtual long __stdcall GetObjectAsync(
  323. /* [in] */ const bstr strObjectPath,
  324. /* [in] */ long lFlags,
  325. /* [in] */ void *pCtx,
  326. /* [in] */ void *pResponseHandler) = 0;
  327. virtual long __stdcall PutClass(
  328. /* [in] */ IWbemClassObject_BIPC *pObject,
  329. /* [in] */ long lFlags,
  330. /* [in] */ void *pCtx,
  331. /* [unique][in][out] */ void **ppCallResult) = 0;
  332. virtual long __stdcall PutClassAsync(
  333. /* [in] */ IWbemClassObject_BIPC *pObject,
  334. /* [in] */ long lFlags,
  335. /* [in] */ void *pCtx,
  336. /* [in] */ void *pResponseHandler) = 0;
  337. virtual long __stdcall DeleteClass(
  338. /* [in] */ const bstr strClass,
  339. /* [in] */ long lFlags,
  340. /* [in] */ void *pCtx,
  341. /* [unique][in][out] */ void **ppCallResult) = 0;
  342. virtual long __stdcall DeleteClassAsync(
  343. /* [in] */ const bstr strClass,
  344. /* [in] */ long lFlags,
  345. /* [in] */ void *pCtx,
  346. /* [in] */ void *pResponseHandler) = 0;
  347. virtual long __stdcall CreateClassEnum(
  348. /* [in] */ const bstr strSuperclass,
  349. /* [in] */ long lFlags,
  350. /* [in] */ void *pCtx,
  351. /* [out] */ void **ppEnum) = 0;
  352. virtual long __stdcall CreateClassEnumAsync(
  353. /* [in] */ const bstr strSuperclass,
  354. /* [in] */ long lFlags,
  355. /* [in] */ void *pCtx,
  356. /* [in] */ void *pResponseHandler) = 0;
  357. virtual long __stdcall PutInstance(
  358. /* [in] */ void *pInst,
  359. /* [in] */ long lFlags,
  360. /* [in] */ void *pCtx,
  361. /* [unique][in][out] */ void **ppCallResult) = 0;
  362. virtual long __stdcall PutInstanceAsync(
  363. /* [in] */ void *pInst,
  364. /* [in] */ long lFlags,
  365. /* [in] */ void *pCtx,
  366. /* [in] */ void *pResponseHandler) = 0;
  367. virtual long __stdcall DeleteInstance(
  368. /* [in] */ const bstr strObjectPath,
  369. /* [in] */ long lFlags,
  370. /* [in] */ void *pCtx,
  371. /* [unique][in][out] */ void **ppCallResult) = 0;
  372. virtual long __stdcall DeleteInstanceAsync(
  373. /* [in] */ const bstr strObjectPath,
  374. /* [in] */ long lFlags,
  375. /* [in] */ void *pCtx,
  376. /* [in] */ void *pResponseHandler) = 0;
  377. virtual long __stdcall CreateInstanceEnum(
  378. /* [in] */ const bstr strFilter,
  379. /* [in] */ long lFlags,
  380. /* [in] */ void *pCtx,
  381. /* [out] */ void **ppEnum) = 0;
  382. virtual long __stdcall CreateInstanceEnumAsync(
  383. /* [in] */ const bstr strFilter,
  384. /* [in] */ long lFlags,
  385. /* [in] */ void *pCtx,
  386. /* [in] */ void *pResponseHandler) = 0;
  387. virtual long __stdcall ExecQuery(
  388. /* [in] */ const bstr strQueryLanguage,
  389. /* [in] */ const bstr strQuery,
  390. /* [in] */ long lFlags,
  391. /* [in] */ IWbemContext_BIPC *pCtx,
  392. /* [out] */ IEnumWbemClassObject_BIPC **ppEnum) = 0;
  393. virtual long __stdcall ExecQueryAsync(
  394. /* [in] */ const bstr strQueryLanguage,
  395. /* [in] */ const bstr strQuery,
  396. /* [in] */ long lFlags,
  397. /* [in] */ IWbemContext_BIPC *pCtx,
  398. /* [in] */ void *pResponseHandler) = 0;
  399. virtual long __stdcall ExecNotificationQuery(
  400. /* [in] */ const bstr strQueryLanguage,
  401. /* [in] */ const bstr strQuery,
  402. /* [in] */ long lFlags,
  403. /* [in] */ IWbemContext_BIPC *pCtx,
  404. /* [out] */ void **ppEnum) = 0;
  405. virtual long __stdcall ExecNotificationQueryAsync(
  406. /* [in] */ const bstr strQueryLanguage,
  407. /* [in] */ const bstr strQuery,
  408. /* [in] */ long lFlags,
  409. /* [in] */ IWbemContext_BIPC *pCtx,
  410. /* [in] */ void *pResponseHandler) = 0;
  411. virtual long __stdcall ExecMethod(
  412. /* [in] */ const bstr strObjectPath,
  413. /* [in] */ const bstr strMethodName,
  414. /* [in] */ long lFlags,
  415. /* [in] */ IWbemContext_BIPC *pCtx,
  416. /* [in] */ IWbemClassObject_BIPC *pInParams,
  417. /* [unique][in][out] */ IWbemClassObject_BIPC **ppOutParams,
  418. /* [unique][in][out] */ void **ppCallResult) = 0;
  419. virtual long __stdcall ExecMethodAsync(
  420. /* [in] */ const bstr strObjectPath,
  421. /* [in] */ const bstr strMethodName,
  422. /* [in] */ long lFlags,
  423. /* [in] */ IWbemContext_BIPC *pCtx,
  424. /* [in] */ IWbemClassObject_BIPC *pInParams,
  425. /* [in] */ void *pResponseHandler) = 0;
  426. };
  427. struct IWbemLocator_BIPC : public IUnknown_BIPC
  428. {
  429. public:
  430. virtual long __stdcall ConnectServer(
  431. /* [in] */ const bstr strNetworkResource,
  432. /* [in] */ const bstr strUser,
  433. /* [in] */ const bstr strPassword,
  434. /* [in] */ const bstr strLocale,
  435. /* [in] */ long lSecurityFlags,
  436. /* [in] */ const bstr strAuthority,
  437. /* [in] */ void *pCtx,
  438. /* [out] */ IWbemServices_BIPC **ppNamespace) = 0;
  439. };
  440. struct interprocess_overlapped
  441. {
  442. unsigned long *internal;
  443. unsigned long *internal_high;
  444. union {
  445. struct {
  446. unsigned long offset;
  447. unsigned long offset_high;
  448. }dummy;
  449. void *pointer;
  450. };
  451. void *h_event;
  452. };
  453. struct interprocess_filetime
  454. {
  455. unsigned long dwLowDateTime;
  456. unsigned long dwHighDateTime;
  457. };
  458. struct win32_find_data
  459. {
  460. unsigned long dwFileAttributes;
  461. interprocess_filetime ftCreationTime;
  462. interprocess_filetime ftLastAccessTime;
  463. interprocess_filetime ftLastWriteTime;
  464. unsigned long nFileSizeHigh;
  465. unsigned long nFileSizeLow;
  466. unsigned long dwReserved0;
  467. unsigned long dwReserved1;
  468. char cFileName[MaxPath];
  469. char cAlternateFileName[14];
  470. };
  471. struct interprocess_security_attributes
  472. {
  473. unsigned long nLength;
  474. void *lpSecurityDescriptor;
  475. int bInheritHandle;
  476. };
  477. struct system_info {
  478. union {
  479. unsigned long dwOemId; // Obsolete field...do not use
  480. struct {
  481. unsigned short wProcessorArchitecture;
  482. unsigned short wReserved;
  483. } dummy;
  484. };
  485. unsigned long dwPageSize;
  486. void * lpMinimumApplicationAddress;
  487. void * lpMaximumApplicationAddress;
  488. unsigned long * dwActiveProcessorMask;
  489. unsigned long dwNumberOfProcessors;
  490. unsigned long dwProcessorType;
  491. unsigned long dwAllocationGranularity;
  492. unsigned short wProcessorLevel;
  493. unsigned short wProcessorRevision;
  494. };
  495. struct interprocess_acl
  496. {
  497. unsigned char AclRevision;
  498. unsigned char Sbz1;
  499. unsigned short AclSize;
  500. unsigned short AceCount;
  501. unsigned short Sbz2;
  502. };
  503. struct interprocess_security_descriptor
  504. {
  505. unsigned char Revision;
  506. unsigned char Sbz1;
  507. unsigned short Control;
  508. void *Owner;
  509. void *Group;
  510. interprocess_acl *Sacl;
  511. interprocess_acl *Dacl;
  512. };
  513. struct interprocess_by_handle_file_information
  514. {
  515. unsigned long dwFileAttributes;
  516. interprocess_filetime ftCreationTime;
  517. interprocess_filetime ftLastAccessTime;
  518. interprocess_filetime ftLastWriteTime;
  519. unsigned long dwVolumeSerialNumber;
  520. unsigned long nFileSizeHigh;
  521. unsigned long nFileSizeLow;
  522. unsigned long nNumberOfLinks;
  523. unsigned long nFileIndexHigh;
  524. unsigned long nFileIndexLow;
  525. };
  526. struct interprocess_eventlogrecord
  527. {
  528. unsigned long Length; // Length of full record
  529. unsigned long Reserved; // Used by the service
  530. unsigned long RecordNumber; // Absolute record number
  531. unsigned long TimeGenerated; // Seconds since 1-1-1970
  532. unsigned long TimeWritten; // Seconds since 1-1-1970
  533. unsigned long EventID;
  534. unsigned short EventType;
  535. unsigned short NumStrings;
  536. unsigned short EventCategory;
  537. unsigned short ReservedFlags; // For use with paired events (auditing)
  538. unsigned long ClosingRecordNumber; // For use with paired events (auditing)
  539. unsigned long StringOffset; // Offset from beginning of record
  540. unsigned long UserSidLength;
  541. unsigned long UserSidOffset;
  542. unsigned long DataLength;
  543. unsigned long DataOffset; // Offset from beginning of record
  544. //
  545. // Then follow:
  546. //
  547. // wchar_t SourceName[]
  548. // wchar_t Computername[]
  549. // SID UserSid
  550. // wchar_t Strings[]
  551. // BYTE Data[]
  552. // CHAR Pad[]
  553. // unsigned long Length;
  554. //
  555. };
  556. union large_integer
  557. {
  558. __int64 QuadPart;
  559. };
  560. struct hinstance_struct { int unused; };
  561. typedef hinstance_struct *hmodule;
  562. struct hkey_struct;
  563. typedef hkey_struct *hkey;
  564. #ifdef _WIN64
  565. typedef __int64 (__stdcall *farproc_t)();
  566. #else
  567. typedef int (__stdcall *farproc_t)();
  568. #endif // _WIN64
  569. #else //#ifndef BOOST_USE_WINDOWS_H
  570. typedef GUID GUID_BIPC;
  571. typedef VARIANT wchar_variant;
  572. #if defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME)
  573. typedef IUnknown IUnknown_BIPC;
  574. typedef IWbemClassObject IWbemClassObject_BIPC;
  575. typedef IWbemContext IWbemContext_BIPC;
  576. typedef IEnumWbemClassObject IEnumWbemClassObject_BIPC;
  577. typedef IWbemServices IWbemServices_BIPC;
  578. typedef IWbemLocator IWbemLocator_BIPC;
  579. #endif
  580. typedef OVERLAPPED interprocess_overlapped;
  581. typedef FILETIME interprocess_filetime;
  582. typedef WIN32_FIND_DATAA win32_find_data;
  583. typedef SECURITY_ATTRIBUTES interprocess_security_attributes;
  584. typedef SYSTEM_INFO system_info;
  585. typedef ACL interprocess_acl;
  586. typedef SECURITY_DESCRIPTOR interprocess_security_descriptor;
  587. typedef BY_HANDLE_FILE_INFORMATION interprocess_by_handle_file_information;
  588. typedef EVENTLOGRECORD interprocess_eventlogrecord;
  589. typedef LARGE_INTEGER large_integer;
  590. typedef HMODULE hmodule;
  591. typedef HKEY hkey;
  592. typedef BSTR bstr;
  593. typedef FARPROC farproc_t;
  594. #endif //#ifndef BOOST_USE_WINDOWS_H
  595. //////////////////////////////////////////////////////////////////////////////
  596. //
  597. // Nt native structures
  598. //
  599. //////////////////////////////////////////////////////////////////////////////
  600. struct interprocess_semaphore_basic_information
  601. {
  602. unsigned int count; // current semaphore count
  603. unsigned int limit; // max semaphore count
  604. };
  605. struct interprocess_section_basic_information
  606. {
  607. void * base_address;
  608. unsigned long section_attributes;
  609. __int64 section_size;
  610. };
  611. struct file_rename_information_t {
  612. int Replace;
  613. void *RootDir;
  614. unsigned long FileNameLength;
  615. wchar_t FileName[1];
  616. };
  617. struct unicode_string_t {
  618. unsigned short Length;
  619. unsigned short MaximumLength;
  620. wchar_t *Buffer;
  621. };
  622. struct object_attributes_t {
  623. unsigned long Length;
  624. void * RootDirectory;
  625. unicode_string_t *ObjectName;
  626. unsigned long Attributes;
  627. void *SecurityDescriptor;
  628. void *SecurityQualityOfService;
  629. };
  630. struct io_status_block_t {
  631. union {
  632. long Status;
  633. void *Pointer;
  634. };
  635. unsigned long *Information;
  636. };
  637. union system_timeofday_information
  638. {
  639. struct data_t
  640. {
  641. __int64 liKeBootTime;
  642. __int64 liKeSystemTime;
  643. __int64 liExpTimeZoneBias;
  644. unsigned long uCurrentTimeZoneId;
  645. unsigned long dwReserved;
  646. ::boost::ulong_long_type ullBootTimeBias;
  647. ::boost::ulong_long_type ullSleepTimeBias;
  648. } data;
  649. unsigned char Reserved1[sizeof(data_t)];
  650. };
  651. static const long BootstampLength = sizeof(__int64);
  652. static const long BootAndSystemstampLength = sizeof(__int64)*2;
  653. static const long SystemTimeOfDayInfoLength = sizeof(system_timeofday_information::data_t);
  654. struct object_name_information_t
  655. {
  656. unicode_string_t Name;
  657. wchar_t NameBuffer[1];
  658. };
  659. enum file_information_class_t {
  660. file_directory_information = 1,
  661. file_full_directory_information,
  662. file_both_directory_information,
  663. file_basic_information,
  664. file_standard_information,
  665. file_internal_information,
  666. file_ea_information,
  667. file_access_information,
  668. file_name_information,
  669. file_rename_information,
  670. file_link_information,
  671. file_names_information,
  672. file_disposition_information,
  673. file_position_information,
  674. file_full_ea_information,
  675. file_mode_information,
  676. file_alignment_information,
  677. file_all_information,
  678. file_allocation_information,
  679. file_end_of_file_information,
  680. file_alternate_name_information,
  681. file_stream_information,
  682. file_pipe_information,
  683. file_pipe_local_information,
  684. file_pipe_remote_information,
  685. file_mailslot_query_information,
  686. file_mailslot_set_information,
  687. file_compression_information,
  688. file_copy_on_write_information,
  689. file_completion_information,
  690. file_move_cluster_information,
  691. file_quota_information,
  692. file_reparse_point_information,
  693. file_network_open_information,
  694. file_object_id_information,
  695. file_tracking_information,
  696. file_ole_directory_information,
  697. file_content_index_information,
  698. file_inherit_content_index_information,
  699. file_ole_information,
  700. file_maximum_information
  701. };
  702. enum semaphore_information_class {
  703. semaphore_basic_information = 0
  704. };
  705. enum system_information_class {
  706. system_basic_information = 0,
  707. system_performance_information = 2,
  708. system_time_of_day_information = 3,
  709. system_process_information = 5,
  710. system_processor_performance_information = 8,
  711. system_interrupt_information = 23,
  712. system_exception_information = 33,
  713. system_registry_quota_information = 37,
  714. system_lookaside_information = 45
  715. };
  716. enum object_information_class
  717. {
  718. object_basic_information,
  719. object_name_information,
  720. object_type_information,
  721. object_all_information,
  722. object_data_information
  723. };
  724. enum section_information_class
  725. {
  726. section_basic_information,
  727. section_image_information
  728. };
  729. //////////////////////////////////////////////////////////////////////////////
  730. //
  731. // Forward declaration of winapi
  732. //
  733. //////////////////////////////////////////////////////////////////////////////
  734. #ifndef BOOST_USE_WINDOWS_H
  735. //Kernel32.dll
  736. //Some windows API declarations
  737. extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentProcessId();
  738. extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId();
  739. extern "C" __declspec(dllimport) int __stdcall GetProcessTimes
  740. ( void *hProcess, interprocess_filetime* lpCreationTime
  741. , interprocess_filetime *lpExitTime,interprocess_filetime *lpKernelTime
  742. , interprocess_filetime *lpUserTime );
  743. extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long);
  744. extern "C" __declspec(dllimport) unsigned long __stdcall GetTickCount(void);
  745. extern "C" __declspec(dllimport) int __stdcall SwitchToThread();
  746. extern "C" __declspec(dllimport) unsigned long __stdcall GetLastError();
  747. extern "C" __declspec(dllimport) void __stdcall SetLastError(unsigned long);
  748. extern "C" __declspec(dllimport) void * __stdcall GetCurrentProcess();
  749. extern "C" __declspec(dllimport) int __stdcall CloseHandle(void*);
  750. extern "C" __declspec(dllimport) int __stdcall DuplicateHandle
  751. ( void *hSourceProcessHandle, void *hSourceHandle
  752. , void *hTargetProcessHandle, void **lpTargetHandle
  753. , unsigned long dwDesiredAccess, int bInheritHandle
  754. , unsigned long dwOptions);
  755. extern "C" __declspec(dllimport) long __stdcall GetFileType(void *hFile);
  756. extern "C" __declspec(dllimport) void *__stdcall FindFirstFileA(const char *lpFileName, win32_find_data *lpFindFileData);
  757. extern "C" __declspec(dllimport) int __stdcall FindNextFileA(void *hFindFile, win32_find_data *lpFindFileData);
  758. extern "C" __declspec(dllimport) int __stdcall FindClose(void *hFindFile);
  759. //extern "C" __declspec(dllimport) void __stdcall GetSystemTimeAsFileTime(interprocess_filetime*);
  760. //extern "C" __declspec(dllimport) int __stdcall FileTimeToLocalFileTime(const interprocess_filetime *in, const interprocess_filetime *out);
  761. extern "C" __declspec(dllimport) void * __stdcall CreateMutexA(interprocess_security_attributes*, int, const char *);
  762. extern "C" __declspec(dllimport) void * __stdcall OpenMutexA(unsigned long, int, const char *);
  763. extern "C" __declspec(dllimport) unsigned long __stdcall WaitForSingleObject(void *, unsigned long);
  764. extern "C" __declspec(dllimport) int __stdcall ReleaseMutex(void *);
  765. extern "C" __declspec(dllimport) int __stdcall UnmapViewOfFile(void *);
  766. extern "C" __declspec(dllimport) void * __stdcall CreateSemaphoreA(interprocess_security_attributes*, long, long, const char *);
  767. extern "C" __declspec(dllimport) int __stdcall ReleaseSemaphore(void *, long, long *);
  768. extern "C" __declspec(dllimport) void * __stdcall OpenSemaphoreA(unsigned long, int, const char *);
  769. extern "C" __declspec(dllimport) void * __stdcall CreateFileMappingA (void *, interprocess_security_attributes*, unsigned long, unsigned long, unsigned long, const char *);
  770. extern "C" __declspec(dllimport) void * __stdcall MapViewOfFileEx (void *, unsigned long, unsigned long, unsigned long, std::size_t, void*);
  771. extern "C" __declspec(dllimport) void * __stdcall OpenFileMappingA (unsigned long, int, const char *);
  772. extern "C" __declspec(dllimport) void * __stdcall CreateFileA (const char *, unsigned long, unsigned long, struct interprocess_security_attributes*, unsigned long, unsigned long, void *);
  773. extern "C" __declspec(dllimport) void __stdcall GetSystemInfo (struct system_info *);
  774. extern "C" __declspec(dllimport) int __stdcall FlushViewOfFile (void *, std::size_t);
  775. extern "C" __declspec(dllimport) int __stdcall VirtualUnlock (void *, std::size_t);
  776. extern "C" __declspec(dllimport) int __stdcall VirtualProtect (void *, std::size_t, unsigned long, unsigned long *);
  777. extern "C" __declspec(dllimport) int __stdcall FlushFileBuffers (void *);
  778. extern "C" __declspec(dllimport) int __stdcall GetFileSizeEx (void *, large_integer *size);
  779. extern "C" __declspec(dllimport) unsigned long __stdcall FormatMessageA
  780. (unsigned long dwFlags, const void *lpSource, unsigned long dwMessageId,
  781. unsigned long dwLanguageId, char *lpBuffer, unsigned long nSize,
  782. std::va_list *Arguments);
  783. extern "C" __declspec(dllimport) void *__stdcall LocalFree (void *);
  784. extern "C" __declspec(dllimport) unsigned long __stdcall GetFileAttributesA(const char *);
  785. extern "C" __declspec(dllimport) int __stdcall CreateDirectoryA(const char *, interprocess_security_attributes*);
  786. extern "C" __declspec(dllimport) int __stdcall RemoveDirectoryA(const char *lpPathName);
  787. extern "C" __declspec(dllimport) int __stdcall GetTempPathA(unsigned long length, char *buffer);
  788. extern "C" __declspec(dllimport) int __stdcall CreateDirectory(const char *, interprocess_security_attributes*);
  789. extern "C" __declspec(dllimport) int __stdcall SetFileValidData(void *, __int64 size);
  790. extern "C" __declspec(dllimport) int __stdcall SetEndOfFile(void *);
  791. extern "C" __declspec(dllimport) int __stdcall SetFilePointerEx(void *, large_integer distance, large_integer *new_file_pointer, unsigned long move_method);
  792. 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);
  793. 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);
  794. 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);
  795. extern "C" __declspec(dllimport) int __stdcall UnlockFileEx(void *hnd, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped* overlapped);
  796. extern "C" __declspec(dllimport) int __stdcall WriteFile(void *hnd, const void *buffer, unsigned long bytes_to_write, unsigned long *bytes_written, interprocess_overlapped* overlapped);
  797. extern "C" __declspec(dllimport) int __stdcall ReadFile(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped);
  798. extern "C" __declspec(dllimport) int __stdcall InitializeSecurityDescriptor(interprocess_security_descriptor *pSecurityDescriptor, unsigned long dwRevision);
  799. extern "C" __declspec(dllimport) int __stdcall SetSecurityDescriptorDacl(interprocess_security_descriptor *pSecurityDescriptor, int bDaclPresent, interprocess_acl *pDacl, int bDaclDefaulted);
  800. extern "C" __declspec(dllimport) hmodule __stdcall LoadLibraryA(const char *);
  801. extern "C" __declspec(dllimport) int __stdcall FreeLibrary(hmodule);
  802. extern "C" __declspec(dllimport) farproc_t __stdcall GetProcAddress(void *, const char*);
  803. extern "C" __declspec(dllimport) hmodule __stdcall GetModuleHandleA(const char*);
  804. extern "C" __declspec(dllimport) void *__stdcall GetFileInformationByHandle(void *, interprocess_by_handle_file_information*);
  805. //Advapi32.dll
  806. extern "C" __declspec(dllimport) long __stdcall RegOpenKeyExA(hkey, const char *, unsigned long, unsigned long, hkey*);
  807. extern "C" __declspec(dllimport) long __stdcall RegQueryValueExA(hkey, const char *, unsigned long*, unsigned long*, unsigned char *, unsigned long*);
  808. extern "C" __declspec(dllimport) long __stdcall RegCloseKey(hkey);
  809. //Ole32.dll
  810. extern "C" __declspec(dllimport) long __stdcall CoInitializeEx(void *pvReserved, unsigned long dwCoInit);
  811. extern "C" __declspec(dllimport) long __stdcall CoInitializeSecurity(
  812. void* pSecDesc,
  813. long cAuthSvc,
  814. void * asAuthSvc,
  815. void *pReserved1,
  816. unsigned long dwAuthnLevel,
  817. unsigned long dwImpLevel,
  818. void *pAuthList,
  819. unsigned long dwCapabilities,
  820. void *pReserved3 );
  821. extern "C" __declspec(dllimport) long __stdcall CoSetProxyBlanket(
  822. IUnknown_BIPC *pProxy,
  823. unsigned long dwAuthnSvc,
  824. unsigned long dwAuthzSvc,
  825. wchar_t *pServerPrincName,
  826. unsigned long dwAuthnLevel,
  827. unsigned long dwImpLevel,
  828. void *pAuthInfo,
  829. unsigned long dwCapabilities);
  830. extern "C" __declspec(dllimport) long __stdcall CoCreateInstance(const GUID_BIPC & rclsid, IUnknown_BIPC *pUnkOuter,
  831. unsigned long dwClsContext, const GUID_BIPC & riid, void** ppv);
  832. extern "C" __declspec(dllimport) void __stdcall CoUninitialize(void);
  833. //OleAut32.dll
  834. extern "C" __declspec(dllimport) long __stdcall VariantClear(wchar_variant * pvarg);
  835. //Shell32.dll
  836. extern "C" __declspec(dllimport) int __stdcall SHGetSpecialFolderPathA
  837. (void* hwnd, const char *pszPath, int csidl, int fCreate);
  838. extern "C" __declspec(dllimport) int __stdcall SHGetFolderPathA(void *hwnd, int csidl, void *hToken, unsigned long dwFlags, const char *pszPath);
  839. //EventLog access functions
  840. extern "C" __declspec(dllimport) void* __stdcall OpenEventLogA
  841. (const char* lpUNCServerName, const char* lpSourceName);
  842. extern "C" __declspec(dllimport) int __stdcall CloseEventLog(void *hEventLog);
  843. extern "C" __declspec(dllimport) int __stdcall ReadEventLogA
  844. (void *hEventLog,
  845. unsigned long dwReadFlags,
  846. unsigned long dwRecordOffset,
  847. void *lpBuffer,
  848. unsigned long nNumberOfBytesToRead,
  849. unsigned long *pnBytesRead,
  850. unsigned long *pnMinNumberOfBytesNeeded
  851. );
  852. #endif //#ifndef BOOST_USE_WINDOWS_H
  853. //kernel32.dll
  854. typedef int (__stdcall *QueryPerformanceCounter_t) (__int64 *lpPerformanceCount);
  855. typedef int (__stdcall *QueryPerformanceFrequency_t)(__int64 *lpFrequency);
  856. //ntdll.dll
  857. typedef long (__stdcall *NtDeleteFile_t)(object_attributes_t *ObjectAttributes);
  858. typedef long (__stdcall *NtSetInformationFile_t)(void *FileHandle, io_status_block_t *IoStatusBlock, void *FileInformation, unsigned long Length, int FileInformationClass );
  859. typedef long (__stdcall *NtOpenFile)(void **FileHandle, unsigned long DesiredAccess, object_attributes_t *ObjectAttributes
  860. , io_status_block_t *IoStatusBlock, unsigned long ShareAccess, unsigned long Length, unsigned long OpenOptions);
  861. typedef long (__stdcall *NtQuerySystemInformation_t)(int, void*, unsigned long, unsigned long *);
  862. typedef long (__stdcall *NtQueryObject_t)(void*, object_information_class, void *, unsigned long, unsigned long *);
  863. typedef long (__stdcall *NtQuerySemaphore_t)(void*, unsigned int info_class, interprocess_semaphore_basic_information *pinfo, unsigned int info_size, unsigned int *ret_len);
  864. typedef long (__stdcall *NtQuerySection_t)(void*, section_information_class, interprocess_section_basic_information *pinfo, unsigned long info_size, unsigned long *ret_len);
  865. typedef long (__stdcall *NtQueryInformationFile_t)(void *,io_status_block_t *,void *, long, int);
  866. typedef long (__stdcall *NtOpenFile_t)(void*,unsigned long ,object_attributes_t*,io_status_block_t*,unsigned long,unsigned long);
  867. typedef long (__stdcall *NtClose_t) (void*);
  868. typedef long (__stdcall *NtQueryTimerResolution_t) (unsigned long* LowestResolution, unsigned long* HighestResolution, unsigned long* CurrentResolution);
  869. typedef long (__stdcall *NtSetTimerResolution_t) (unsigned long RequestedResolution, int Set, unsigned long* ActualResolution);
  870. } //namespace winapi {
  871. } //namespace interprocess {
  872. } //namespace boost {
  873. //////////////////////////////////////////////////////////////////////////////
  874. //
  875. // Forward declaration of constants
  876. //
  877. //////////////////////////////////////////////////////////////////////////////
  878. namespace boost {
  879. namespace interprocess {
  880. namespace winapi {
  881. //Some used constants
  882. static const unsigned long infinite_time = 0xFFFFFFFF;
  883. static const unsigned long error_already_exists = 183L;
  884. static const unsigned long error_invalid_handle = 6L;
  885. static const unsigned long error_sharing_violation = 32L;
  886. static const unsigned long error_file_not_found = 2u;
  887. static const unsigned long error_no_more_files = 18u;
  888. static const unsigned long error_not_locked = 158L;
  889. //Retries in CreateFile, see http://support.microsoft.com/kb/316609
  890. static const unsigned long error_sharing_violation_tries = 3L;
  891. static const unsigned long error_sharing_violation_sleep_ms = 250L;
  892. static const unsigned long error_file_too_large = 223L;
  893. static const unsigned long error_insufficient_buffer = 122L;
  894. static const unsigned long error_handle_eof = 38L;
  895. static const unsigned long semaphore_all_access = (0x000F0000L)|(0x00100000L)|0x3;
  896. static const unsigned long mutex_all_access = (0x000F0000L)|(0x00100000L)|0x0001;
  897. static const unsigned long page_readonly = 0x02;
  898. static const unsigned long page_readwrite = 0x04;
  899. static const unsigned long page_writecopy = 0x08;
  900. static const unsigned long page_noaccess = 0x01;
  901. static const unsigned long standard_rights_required = 0x000F0000L;
  902. static const unsigned long section_query = 0x0001;
  903. static const unsigned long section_map_write = 0x0002;
  904. static const unsigned long section_map_read = 0x0004;
  905. static const unsigned long section_map_execute = 0x0008;
  906. static const unsigned long section_extend_size = 0x0010;
  907. static const unsigned long section_all_access = standard_rights_required |
  908. section_query |
  909. section_map_write |
  910. section_map_read |
  911. section_map_execute |
  912. section_extend_size;
  913. static const unsigned long file_map_copy = section_query;
  914. static const unsigned long file_map_write = section_map_write;
  915. static const unsigned long file_map_read = section_map_read;
  916. static const unsigned long file_map_all_access = section_all_access;
  917. static const unsigned long delete_access = 0x00010000L;
  918. static const unsigned long file_flag_backup_semantics = 0x02000000;
  919. static const long file_flag_delete_on_close = 0x04000000;
  920. //Native API constants
  921. static const unsigned long file_open_for_backup_intent = 0x00004000;
  922. static const int file_share_valid_flags = 0x00000007;
  923. static const long file_delete_on_close = 0x00001000L;
  924. static const long obj_case_insensitive = 0x00000040L;
  925. static const long delete_flag = 0x00010000L;
  926. static const unsigned long movefile_copy_allowed = 0x02;
  927. static const unsigned long movefile_delay_until_reboot = 0x04;
  928. static const unsigned long movefile_replace_existing = 0x01;
  929. static const unsigned long movefile_write_through = 0x08;
  930. static const unsigned long movefile_create_hardlink = 0x10;
  931. static const unsigned long movefile_fail_if_not_trackable = 0x20;
  932. static const unsigned long file_share_read = 0x00000001;
  933. static const unsigned long file_share_write = 0x00000002;
  934. static const unsigned long file_share_delete = 0x00000004;
  935. static const unsigned long file_attribute_readonly = 0x00000001;
  936. static const unsigned long file_attribute_hidden = 0x00000002;
  937. static const unsigned long file_attribute_system = 0x00000004;
  938. static const unsigned long file_attribute_directory = 0x00000010;
  939. static const unsigned long file_attribute_archive = 0x00000020;
  940. static const unsigned long file_attribute_device = 0x00000040;
  941. static const unsigned long file_attribute_normal = 0x00000080;
  942. static const unsigned long file_attribute_temporary = 0x00000100;
  943. static const unsigned long generic_read = 0x80000000L;
  944. static const unsigned long generic_write = 0x40000000L;
  945. static const unsigned long wait_object_0 = 0;
  946. static const unsigned long wait_abandoned = 0x00000080L;
  947. static const unsigned long wait_timeout = 258L;
  948. static const unsigned long wait_failed = (unsigned long)0xFFFFFFFF;
  949. static const unsigned long duplicate_close_source = (unsigned long)0x00000001;
  950. static const unsigned long duplicate_same_access = (unsigned long)0x00000002;
  951. static const unsigned long format_message_allocate_buffer
  952. = (unsigned long)0x00000100;
  953. static const unsigned long format_message_ignore_inserts
  954. = (unsigned long)0x00000200;
  955. static const unsigned long format_message_from_string
  956. = (unsigned long)0x00000400;
  957. static const unsigned long format_message_from_hmodule
  958. = (unsigned long)0x00000800;
  959. static const unsigned long format_message_from_system
  960. = (unsigned long)0x00001000;
  961. static const unsigned long format_message_argument_array
  962. = (unsigned long)0x00002000;
  963. static const unsigned long format_message_max_width_mask
  964. = (unsigned long)0x000000FF;
  965. static const unsigned long lang_neutral = (unsigned long)0x00;
  966. static const unsigned long sublang_default = (unsigned long)0x01;
  967. static const unsigned long invalid_file_size = (unsigned long)0xFFFFFFFF;
  968. static const unsigned long invalid_file_attributes = ((unsigned long)-1);
  969. static void * const invalid_handle_value = ((void*)(long)(-1));
  970. static const unsigned long file_type_char = 0x0002L;
  971. static const unsigned long file_type_disk = 0x0001L;
  972. static const unsigned long file_type_pipe = 0x0003L;
  973. static const unsigned long file_type_remote = 0x8000L;
  974. static const unsigned long file_type_unknown = 0x0000L;
  975. static const unsigned long create_new = 1;
  976. static const unsigned long create_always = 2;
  977. static const unsigned long open_existing = 3;
  978. static const unsigned long open_always = 4;
  979. static const unsigned long truncate_existing = 5;
  980. static const unsigned long file_begin = 0;
  981. static const unsigned long file_current = 1;
  982. static const unsigned long file_end = 2;
  983. static const unsigned long lockfile_fail_immediately = 1;
  984. static const unsigned long lockfile_exclusive_lock = 2;
  985. static const unsigned long error_lock_violation = 33;
  986. static const unsigned long security_descriptor_revision = 1;
  987. const unsigned long max_record_buffer_size = 0x10000L; // 64K
  988. const unsigned long max_path = 260;
  989. //Keys
  990. static const hkey hkey_local_machine = (hkey)(unsigned long*)(long)(0x80000002);
  991. static unsigned long key_query_value = 0x0001;
  992. //COM API
  993. const unsigned long RPC_C_AUTHN_LEVEL_PKT_BIPC = 4;
  994. const unsigned long RPC_C_AUTHN_DEFAULT_BIPC = 0xffffffffL;
  995. const unsigned long RPC_C_AUTHZ_DEFAULT_BIPC = 0xffffffffL;
  996. const unsigned long RPC_C_IMP_LEVEL_IMPERSONATE_BIPC = 3;
  997. const signed long EOAC_NONE_BIPC = 0;
  998. const signed long CLSCTX_INPROC_SERVER_BIPC = 0x1;
  999. const signed long CLSCTX_LOCAL_SERVER_BIPC = 0x4;
  1000. const signed long WBEM_FLAG_RETURN_IMMEDIATELY_BIPC = 0x10;
  1001. const signed long WBEM_FLAG_RETURN_WHEN_COMPLETE_BIPC = 0x0;
  1002. const signed long WBEM_FLAG_FORWARD_ONLY_BIPC = 0x20;
  1003. const signed long WBEM_INFINITE_BIPC = 0xffffffffL;
  1004. const signed long RPC_E_TOO_LATE_BIPC = 0x80010119L;
  1005. const signed long S_OK_BIPC = 0L;
  1006. const signed long S_FALSE_BIPC = 1;
  1007. const signed long RPC_E_CHANGED_MODE_BIPC = 0x80010106L;
  1008. const unsigned long COINIT_APARTMENTTHREADED_BIPC = 0x2;
  1009. const unsigned long COINIT_MULTITHREADED_BIPC = 0x0;
  1010. const unsigned long COINIT_DISABLE_OLE1DDE_BIPC = 0x4;
  1011. const unsigned long COINIT_SPEED_OVER_MEMORY_BIPC = 0x4;
  1012. // Registry types
  1013. #define reg_none ( 0 ) // No value type
  1014. #define reg_sz ( 1 ) // Unicode nul terminated string
  1015. #define reg_expand_sz ( 2 ) // Unicode nul terminated string
  1016. // (with environment variable references)
  1017. #define reg_binary ( 3 ) // Free form binary
  1018. #define reg_dword ( 4 ) // 32-bit number
  1019. #define reg_dword_little_endian ( 4 ) // 32-bit number (same as REG_DWORD)
  1020. #define reg_dword_big_endian ( 5 ) // 32-bit number
  1021. #define reg_link ( 6 ) // Symbolic Link (unicode)
  1022. #define reg_multi_sz ( 7 ) // Multiple Unicode strings
  1023. #define reg_resource_list ( 8 ) // Resource list in the resource map
  1024. #define reg_full_resource_descriptor ( 9 ) // Resource list in the hardware description
  1025. #define reg_resource_requirements_list ( 10 )
  1026. #define reg_qword ( 11 ) // 64-bit number
  1027. #define reg_qword_little_endian ( 11 ) // 64-bit number (same as reg_qword)
  1028. //If the user needs to change default COM initialization model,
  1029. //it can define BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL to one of these:
  1030. //
  1031. // COINIT_APARTMENTTHREADED_BIPC
  1032. // COINIT_MULTITHREADED_BIPC
  1033. // COINIT_DISABLE_OLE1DDE_BIPC
  1034. // COINIT_SPEED_OVER_MEMORY_BIPC
  1035. #if !defined(BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL)
  1036. #define BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL COINIT_APARTMENTTHREADED_BIPC
  1037. #elif (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_APARTMENTTHREADED_BIPC) &&\
  1038. (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_MULTITHREADED_BIPC) &&\
  1039. (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_DISABLE_OLE1DDE_BIPC) &&\
  1040. (BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL != COINIT_SPEED_OVER_MEMORY_BIPC)
  1041. #error "Wrong value for BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL macro"
  1042. #endif
  1043. const GUID_BIPC CLSID_WbemAdministrativeLocator =
  1044. { 0xcb8555cc, 0x9128, 0x11d1, {0xad, 0x9b, 0x00, 0xc0, 0x4f, 0xd8, 0xfd, 0xff}};
  1045. const GUID_BIPC IID_IUnknown = { 0x00000000, 0x0000, 0x0000, {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}};
  1046. static const unsigned long eventlog_sequential_read = 0x0001;
  1047. static const unsigned long eventlog_backwards_read = 0x0008;
  1048. } //namespace winapi {
  1049. } //namespace interprocess {
  1050. } //namespace boost {
  1051. namespace boost {
  1052. namespace interprocess {
  1053. namespace winapi {
  1054. inline unsigned long get_last_error()
  1055. { return GetLastError(); }
  1056. inline void set_last_error(unsigned long err)
  1057. { return SetLastError(err); }
  1058. inline unsigned long format_message
  1059. (unsigned long dwFlags, const void *lpSource,
  1060. unsigned long dwMessageId, unsigned long dwLanguageId,
  1061. char *lpBuffer, unsigned long nSize, std::va_list *Arguments)
  1062. {
  1063. return FormatMessageA
  1064. (dwFlags, lpSource, dwMessageId, dwLanguageId, lpBuffer, nSize, Arguments);
  1065. }
  1066. //And now, wrapper functions
  1067. inline void * local_free(void *hmem)
  1068. { return LocalFree(hmem); }
  1069. inline unsigned long make_lang_id(unsigned long p, unsigned long s)
  1070. { return ((((unsigned short)(s)) << 10) | (unsigned short)(p)); }
  1071. inline void sched_yield()
  1072. {
  1073. if(!SwitchToThread()){
  1074. Sleep(0);
  1075. }
  1076. }
  1077. inline void sleep_tick()
  1078. { Sleep(1); }
  1079. inline void sleep(unsigned long ms)
  1080. { Sleep(ms); }
  1081. inline unsigned long get_current_thread_id()
  1082. { return GetCurrentThreadId(); }
  1083. inline bool get_process_times
  1084. ( void *hProcess, interprocess_filetime* lpCreationTime
  1085. , interprocess_filetime *lpExitTime, interprocess_filetime *lpKernelTime
  1086. , interprocess_filetime *lpUserTime )
  1087. { return 0 != GetProcessTimes(hProcess, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime); }
  1088. inline unsigned long get_current_process_id()
  1089. { return GetCurrentProcessId(); }
  1090. inline unsigned int close_handle(void* handle)
  1091. { return CloseHandle(handle); }
  1092. inline void * find_first_file(const char *lpFileName, win32_find_data *lpFindFileData)
  1093. { return FindFirstFileA(lpFileName, lpFindFileData); }
  1094. inline bool find_next_file(void *hFindFile, win32_find_data *lpFindFileData)
  1095. { return FindNextFileA(hFindFile, lpFindFileData) != 0; }
  1096. inline bool find_close(void *handle)
  1097. { return FindClose(handle) != 0; }
  1098. inline bool duplicate_current_process_handle
  1099. (void *hSourceHandle, void **lpTargetHandle)
  1100. {
  1101. return 0 != DuplicateHandle
  1102. ( GetCurrentProcess(), hSourceHandle, GetCurrentProcess()
  1103. , lpTargetHandle, 0, 0
  1104. , duplicate_same_access);
  1105. }
  1106. inline unsigned long get_file_type(void *hFile)
  1107. {
  1108. return GetFileType(hFile);
  1109. }
  1110. /*
  1111. inline void get_system_time_as_file_time(interprocess_filetime *filetime)
  1112. { GetSystemTimeAsFileTime(filetime); }
  1113. inline bool file_time_to_local_file_time
  1114. (const interprocess_filetime *in, const interprocess_filetime *out)
  1115. { return 0 != FileTimeToLocalFileTime(in, out); }
  1116. */
  1117. inline void *open_or_create_mutex(const char *name, bool initial_owner, interprocess_security_attributes *attr)
  1118. { return CreateMutexA(attr, (int)initial_owner, name); }
  1119. inline unsigned long wait_for_single_object(void *handle, unsigned long time)
  1120. { return WaitForSingleObject(handle, time); }
  1121. inline int release_mutex(void *handle)
  1122. { return ReleaseMutex(handle); }
  1123. inline int unmap_view_of_file(void *address)
  1124. { return UnmapViewOfFile(address); }
  1125. inline void *open_or_create_semaphore(const char *name, long initial_count, long maximum_count, interprocess_security_attributes *attr)
  1126. { return CreateSemaphoreA(attr, initial_count, maximum_count, name); }
  1127. inline void *open_semaphore(const char *name)
  1128. { return OpenSemaphoreA(semaphore_all_access, 0, name); }
  1129. inline int release_semaphore(void *handle, long release_count, long *prev_count)
  1130. { return ReleaseSemaphore(handle, release_count, prev_count); }
  1131. class interprocess_all_access_security
  1132. {
  1133. interprocess_security_attributes sa;
  1134. interprocess_security_descriptor sd;
  1135. bool initialized;
  1136. public:
  1137. interprocess_all_access_security()
  1138. : initialized(false)
  1139. {
  1140. if(!InitializeSecurityDescriptor(&sd, security_descriptor_revision))
  1141. return;
  1142. if(!SetSecurityDescriptorDacl(&sd, true, 0, false))
  1143. return;
  1144. sa.lpSecurityDescriptor = &sd;
  1145. sa.nLength = sizeof(interprocess_security_attributes);
  1146. sa.bInheritHandle = false;
  1147. initialized = false;
  1148. }
  1149. interprocess_security_attributes *get_attributes()
  1150. { return &sa; }
  1151. };
  1152. inline void * create_file_mapping (void * handle, unsigned long access, ::boost::ulong_long_type file_offset, const char * name, interprocess_security_attributes *psec)
  1153. {
  1154. const unsigned long high_size(file_offset >> 32), low_size((boost::uint32_t)file_offset);
  1155. return CreateFileMappingA (handle, psec, access, high_size, low_size, name);
  1156. }
  1157. inline void * open_file_mapping (unsigned long access, const char *name)
  1158. { return OpenFileMappingA (access, 0, name); }
  1159. 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)
  1160. {
  1161. const unsigned long offset_low = (unsigned long)(offset & ((::boost::ulong_long_type)0xFFFFFFFF));
  1162. const unsigned long offset_high = offset >> 32;
  1163. return MapViewOfFileEx(handle, file_access, offset_high, offset_low, numbytes, base_addr);
  1164. }
  1165. inline void *create_file(const char *name, unsigned long access, unsigned long creation_flags, unsigned long attributes, interprocess_security_attributes *psec)
  1166. {
  1167. for (unsigned int attempt(0); attempt < error_sharing_violation_tries; ++attempt){
  1168. void * const handle = CreateFileA(name, access,
  1169. file_share_read | file_share_write | file_share_delete,
  1170. psec, creation_flags, attributes, 0);
  1171. bool const invalid(invalid_handle_value == handle);
  1172. if (!invalid){
  1173. return handle;
  1174. }
  1175. if (error_sharing_violation != get_last_error()){
  1176. return handle;
  1177. }
  1178. sleep(error_sharing_violation_sleep_ms);
  1179. }
  1180. return invalid_handle_value;
  1181. }
  1182. inline void get_system_info(system_info *info)
  1183. { GetSystemInfo(info); }
  1184. inline bool flush_view_of_file(void *base_addr, std::size_t numbytes)
  1185. { return 0 != FlushViewOfFile(base_addr, numbytes); }
  1186. inline bool virtual_unlock(void *base_addr, std::size_t numbytes)
  1187. { return 0 != VirtualUnlock(base_addr, numbytes); }
  1188. inline bool virtual_protect(void *base_addr, std::size_t numbytes, unsigned long flNewProtect, unsigned long &lpflOldProtect)
  1189. { return 0 != VirtualProtect(base_addr, numbytes, flNewProtect, &lpflOldProtect); }
  1190. inline bool flush_file_buffers(void *handle)
  1191. { return 0 != FlushFileBuffers(handle); }
  1192. inline bool get_file_size(void *handle, __int64 &size)
  1193. { return 0 != GetFileSizeEx(handle, (large_integer*)&size); }
  1194. inline bool create_directory(const char *name)
  1195. {
  1196. interprocess_all_access_security sec;
  1197. return 0 != CreateDirectoryA(name, sec.get_attributes());
  1198. }
  1199. inline bool remove_directory(const char *lpPathName)
  1200. { return 0 != RemoveDirectoryA(lpPathName); }
  1201. inline unsigned long get_temp_path(unsigned long length, char *buffer)
  1202. { return GetTempPathA(length, buffer); }
  1203. inline int set_end_of_file(void *handle)
  1204. { return 0 != SetEndOfFile(handle); }
  1205. inline bool set_file_pointer_ex(void *handle, __int64 distance, __int64 *new_file_pointer, unsigned long move_method)
  1206. {
  1207. large_integer d; d.QuadPart = distance;
  1208. return 0 != SetFilePointerEx(handle, d, (large_integer*)new_file_pointer, move_method);
  1209. }
  1210. inline bool lock_file_ex(void *hnd, unsigned long flags, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
  1211. { return 0 != LockFileEx(hnd, flags, reserved, size_low, size_high, overlapped); }
  1212. inline bool unlock_file_ex(void *hnd, unsigned long reserved, unsigned long size_low, unsigned long size_high, interprocess_overlapped *overlapped)
  1213. { return 0 != UnlockFileEx(hnd, reserved, size_low, size_high, overlapped); }
  1214. inline bool write_file(void *hnd, const void *buffer, unsigned long bytes_to_write, unsigned long *bytes_written, interprocess_overlapped* overlapped)
  1215. { return 0 != WriteFile(hnd, buffer, bytes_to_write, bytes_written, overlapped); }
  1216. inline bool read_file(void *hnd, void *buffer, unsigned long bytes_to_read, unsigned long *bytes_read, interprocess_overlapped* overlapped)
  1217. { return 0 != ReadFile(hnd, buffer, bytes_to_read, bytes_read, overlapped); }
  1218. inline bool get_file_information_by_handle(void *hnd, interprocess_by_handle_file_information *info)
  1219. { return 0 != GetFileInformationByHandle(hnd, info); }
  1220. inline long interlocked_increment(long volatile *addr)
  1221. { return BOOST_INTERLOCKED_INCREMENT(const_cast<long*>(addr)); }
  1222. inline long interlocked_decrement(long volatile *addr)
  1223. { return BOOST_INTERLOCKED_DECREMENT(const_cast<long*>(addr)); }
  1224. inline long interlocked_compare_exchange(long volatile *addr, long val1, long val2)
  1225. { return BOOST_INTERLOCKED_COMPARE_EXCHANGE(const_cast<long*>(addr), val1, val2); }
  1226. inline long interlocked_exchange_add(long volatile* addend, long value)
  1227. { return BOOST_INTERLOCKED_EXCHANGE_ADD(const_cast<long*>(addend), value); }
  1228. inline long interlocked_exchange(long volatile* addend, long value)
  1229. { return BOOST_INTERLOCKED_EXCHANGE(const_cast<long*>(addend), value); }
  1230. //Forward functions
  1231. inline hmodule load_library(const char *name)
  1232. { return LoadLibraryA(name); }
  1233. inline bool free_library(hmodule module)
  1234. { return 0 != FreeLibrary(module); }
  1235. inline farproc_t get_proc_address(hmodule module, const char *name)
  1236. { return GetProcAddress(module, name); }
  1237. inline void *get_current_process()
  1238. { return GetCurrentProcess(); }
  1239. inline hmodule get_module_handle(const char *name)
  1240. { return GetModuleHandleA(name); }
  1241. inline long reg_open_key_ex(hkey hKey, const char *lpSubKey, unsigned long ulOptions, unsigned long samDesired, hkey *phkResult)
  1242. { return RegOpenKeyExA(hKey, lpSubKey, ulOptions, samDesired, phkResult); }
  1243. inline long reg_query_value_ex(hkey hKey, const char *lpValueName, unsigned long*lpReserved, unsigned long*lpType, unsigned char *lpData, unsigned long*lpcbData)
  1244. { return RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData); }
  1245. inline long reg_close_key(hkey hKey)
  1246. { return RegCloseKey(hKey); }
  1247. inline void initialize_object_attributes
  1248. ( object_attributes_t *pobject_attr, unicode_string_t *name
  1249. , unsigned long attr, void *rootdir, void *security_descr)
  1250. {
  1251. pobject_attr->Length = sizeof(object_attributes_t);
  1252. pobject_attr->RootDirectory = rootdir;
  1253. pobject_attr->Attributes = attr;
  1254. pobject_attr->ObjectName = name;
  1255. pobject_attr->SecurityDescriptor = security_descr;
  1256. pobject_attr->SecurityQualityOfService = 0;
  1257. }
  1258. inline void rtl_init_empty_unicode_string(unicode_string_t *ucStr, wchar_t *buf, unsigned short bufSize)
  1259. {
  1260. ucStr->Buffer = buf;
  1261. ucStr->Length = 0;
  1262. ucStr->MaximumLength = bufSize;
  1263. }
  1264. //A class that locates and caches loaded DLL function addresses.
  1265. template<int Dummy>
  1266. struct function_address_holder
  1267. {
  1268. enum { NtSetInformationFile
  1269. , NtQuerySystemInformation
  1270. , NtQueryObject
  1271. , NtQuerySemaphore
  1272. , NtQuerySection
  1273. , NtOpenFile
  1274. , NtClose
  1275. , NtQueryTimerResolution
  1276. , QueryPerformanceCounter
  1277. , QueryPerformanceFrequency
  1278. , NumFunction
  1279. };
  1280. enum { NtDll_dll, Kernel32_dll, NumModule };
  1281. private:
  1282. static const char *FunctionNames[NumFunction];
  1283. static const char *ModuleNames[NumModule];
  1284. static farproc_t FunctionAddresses[NumFunction];
  1285. static unsigned int FunctionModules[NumFunction];
  1286. static volatile long FunctionStates[NumFunction];
  1287. static hmodule ModuleAddresses[NumModule];
  1288. static volatile long ModuleStates[NumModule];
  1289. static hmodule get_module_from_id(unsigned int id)
  1290. {
  1291. BOOST_ASSERT(id < (unsigned int)NumModule);
  1292. hmodule addr = get_module_handle(ModuleNames[id]);
  1293. BOOST_ASSERT(addr);
  1294. return addr;
  1295. }
  1296. static hmodule get_module(const unsigned int id)
  1297. {
  1298. BOOST_ASSERT(id < (unsigned int)NumModule);
  1299. for(unsigned i = 0; ModuleStates[id] < 2; ++i){
  1300. if(interlocked_compare_exchange(&ModuleStates[id], 1, 0) == 0){
  1301. ModuleAddresses[id] = get_module_from_id(id);
  1302. interlocked_increment(&ModuleStates[id]);
  1303. break;
  1304. }
  1305. else if(i & 1){
  1306. sched_yield();
  1307. }
  1308. else{
  1309. sleep_tick();
  1310. }
  1311. }
  1312. return ModuleAddresses[id];
  1313. }
  1314. static farproc_t get_address_from_dll(const unsigned int id)
  1315. {
  1316. BOOST_ASSERT(id < (unsigned int)NumFunction);
  1317. farproc_t addr = get_proc_address(get_module(FunctionModules[id]), FunctionNames[id]);
  1318. BOOST_ASSERT(addr);
  1319. return addr;
  1320. }
  1321. public:
  1322. static farproc_t get(const unsigned int id)
  1323. {
  1324. BOOST_ASSERT(id < (unsigned int)NumFunction);
  1325. for(unsigned i = 0; FunctionStates[id] < 2; ++i){
  1326. if(interlocked_compare_exchange(&FunctionStates[id], 1, 0) == 0){
  1327. FunctionAddresses[id] = get_address_from_dll(id);
  1328. interlocked_increment(&FunctionStates[id]);
  1329. break;
  1330. }
  1331. else if(i & 1){
  1332. sched_yield();
  1333. }
  1334. else{
  1335. sleep_tick();
  1336. }
  1337. }
  1338. return FunctionAddresses[id];
  1339. }
  1340. };
  1341. template<int Dummy>
  1342. const char *function_address_holder<Dummy>::FunctionNames[function_address_holder<Dummy>::NumFunction] =
  1343. {
  1344. "NtSetInformationFile",
  1345. "NtQuerySystemInformation",
  1346. "NtQueryObject",
  1347. "NtQuerySemaphore",
  1348. "NtQuerySection",
  1349. "NtOpenFile",
  1350. "NtClose",
  1351. "NtQueryTimerResolution",
  1352. "QueryPerformanceCounter",
  1353. "QueryPerformanceFrequency"
  1354. };
  1355. template<int Dummy>
  1356. unsigned int function_address_holder<Dummy>::FunctionModules[function_address_holder<Dummy>::NumFunction] =
  1357. {
  1358. NtDll_dll,
  1359. NtDll_dll,
  1360. NtDll_dll,
  1361. NtDll_dll,
  1362. NtDll_dll,
  1363. NtDll_dll,
  1364. NtDll_dll,
  1365. NtDll_dll,
  1366. Kernel32_dll,
  1367. Kernel32_dll
  1368. };
  1369. template<int Dummy>
  1370. const char *function_address_holder<Dummy>::ModuleNames[function_address_holder<Dummy>::NumModule] =
  1371. {
  1372. "ntdll.dll",
  1373. "kernel32.dll"
  1374. };
  1375. template<int Dummy>
  1376. farproc_t function_address_holder<Dummy>::FunctionAddresses[function_address_holder<Dummy>::NumFunction];
  1377. template<int Dummy>
  1378. volatile long function_address_holder<Dummy>::FunctionStates[function_address_holder<Dummy>::NumFunction];
  1379. template<int Dummy>
  1380. hmodule function_address_holder<Dummy>::ModuleAddresses[function_address_holder<Dummy>::NumModule];
  1381. template<int Dummy>
  1382. volatile long function_address_holder<Dummy>::ModuleStates[function_address_holder<Dummy>::NumModule];
  1383. struct dll_func
  1384. : public function_address_holder<0>
  1385. {};
  1386. //Complex winapi based functions...
  1387. struct library_unloader
  1388. {
  1389. hmodule lib_;
  1390. library_unloader(hmodule module) : lib_(module){}
  1391. ~library_unloader(){ free_library(lib_); }
  1392. };
  1393. inline bool get_system_time_of_day_information(system_timeofday_information &info)
  1394. {
  1395. NtQuerySystemInformation_t pNtQuerySystemInformation = reinterpret_cast<NtQuerySystemInformation_t>
  1396. (dll_func::get(dll_func::NtQuerySystemInformation));
  1397. unsigned long res;
  1398. long status = pNtQuerySystemInformation(system_time_of_day_information, &info, sizeof(info), &res);
  1399. if(status){
  1400. return false;
  1401. }
  1402. return true;
  1403. }
  1404. inline bool get_boot_time(unsigned char (&bootstamp) [BootstampLength])
  1405. {
  1406. system_timeofday_information info;
  1407. bool ret = get_system_time_of_day_information(info);
  1408. if(!ret){
  1409. return false;
  1410. }
  1411. std::memcpy(&bootstamp[0], &info.Reserved1, sizeof(bootstamp));
  1412. return true;
  1413. }
  1414. inline bool get_boot_and_system_time(unsigned char (&bootsystemstamp) [BootAndSystemstampLength])
  1415. {
  1416. system_timeofday_information info;
  1417. bool ret = get_system_time_of_day_information(info);
  1418. if(!ret){
  1419. return false;
  1420. }
  1421. std::memcpy(&bootsystemstamp[0], &info.Reserved1, sizeof(bootsystemstamp));
  1422. return true;
  1423. }
  1424. //Writes the hexadecimal value of the buffer, in the wide character string.
  1425. //str must be twice length
  1426. inline void buffer_to_wide_str(const void *buf, std::size_t length, wchar_t *str)
  1427. {
  1428. const wchar_t Characters [] =
  1429. { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7'
  1430. , L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
  1431. std::size_t char_counter = 0;
  1432. const char *chbuf = static_cast<const char *>(buf);
  1433. for(std::size_t i = 0; i != length; ++i){
  1434. str[char_counter++] = Characters[(chbuf[i]&0xF0)>>4];
  1435. str[char_counter++] = Characters[(chbuf[i]&0x0F)];
  1436. }
  1437. }
  1438. //Writes the hexadecimal value of the buffer, in the narrow character string.
  1439. //str must be twice length
  1440. inline void buffer_to_narrow_str(const void *buf, std::size_t length, char *str)
  1441. {
  1442. const char Characters [] =
  1443. { '0', '1', '2', '3', '4', '5', '6', '7'
  1444. , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  1445. std::size_t char_counter = 0;
  1446. const char *chbuf = static_cast<const char *>(buf);
  1447. for(std::size_t i = 0; i != length; ++i){
  1448. str[char_counter++] = Characters[(chbuf[i]&0xF0)>>4];
  1449. str[char_counter++] = Characters[(chbuf[i]&0x0F)];
  1450. }
  1451. }
  1452. inline bool get_boot_time_str(char *bootstamp_str, std::size_t &s)
  1453. //will write BootstampLength chars
  1454. {
  1455. if(s < (BootstampLength*2))
  1456. return false;
  1457. system_timeofday_information info;
  1458. bool ret = get_system_time_of_day_information(info);
  1459. if(!ret){
  1460. return false;
  1461. }
  1462. buffer_to_narrow_str(info.Reserved1, BootstampLength, bootstamp_str);
  1463. s = BootstampLength*2;
  1464. return true;
  1465. }
  1466. inline bool get_boot_and_system_time_wstr(wchar_t *bootsystemstamp, std::size_t &s)
  1467. //will write BootAndSystemstampLength chars
  1468. {
  1469. if(s < (BootAndSystemstampLength*2))
  1470. return false;
  1471. system_timeofday_information info;
  1472. bool ret = get_system_time_of_day_information(info);
  1473. if(!ret){
  1474. return false;
  1475. }
  1476. buffer_to_wide_str(&info.Reserved1[0], BootAndSystemstampLength, bootsystemstamp);
  1477. s = BootAndSystemstampLength*2;
  1478. return true;
  1479. }
  1480. class handle_closer
  1481. {
  1482. void *handle_;
  1483. handle_closer(const handle_closer &);
  1484. handle_closer& operator=(const handle_closer &);
  1485. public:
  1486. explicit handle_closer(void *handle) : handle_(handle){}
  1487. ~handle_closer()
  1488. { close_handle(handle_); }
  1489. };
  1490. class eventlog_handle_closer
  1491. {
  1492. void *handle_;
  1493. eventlog_handle_closer(const handle_closer &);
  1494. eventlog_handle_closer& operator=(const eventlog_handle_closer &);
  1495. public:
  1496. explicit eventlog_handle_closer(void *handle) : handle_(handle){}
  1497. ~eventlog_handle_closer()
  1498. { CloseEventLog(handle_); }
  1499. };
  1500. union ntquery_mem_t
  1501. {
  1502. object_name_information_t name;
  1503. struct ren_t
  1504. {
  1505. file_rename_information_t info;
  1506. wchar_t buf[1];
  1507. } ren;
  1508. };
  1509. class nt_query_mem_deleter
  1510. {
  1511. static const std::size_t rename_offset = offsetof(ntquery_mem_t, ren.info.FileName) -
  1512. offsetof(ntquery_mem_t, name.Name.Buffer);
  1513. // Timestamp process id atomic count
  1514. static const std::size_t rename_suffix =
  1515. (SystemTimeOfDayInfoLength + sizeof(unsigned long) + sizeof(boost::uint32_t))*2;
  1516. public:
  1517. explicit nt_query_mem_deleter(std::size_t object_name_information_size)
  1518. : m_size(object_name_information_size + rename_offset + rename_suffix)
  1519. , m_buf(new char [m_size])
  1520. {}
  1521. ~nt_query_mem_deleter()
  1522. {
  1523. delete[]m_buf;
  1524. }
  1525. void realloc_mem(std::size_t num_bytes)
  1526. {
  1527. num_bytes += rename_suffix + rename_offset;
  1528. char *buf = m_buf;
  1529. m_buf = new char[num_bytes];
  1530. delete[]buf;
  1531. m_size = num_bytes;
  1532. }
  1533. ntquery_mem_t *query_mem() const
  1534. { return static_cast<ntquery_mem_t *>(static_cast<void*>(m_buf)); }
  1535. unsigned long object_name_information_size() const
  1536. {
  1537. return static_cast<unsigned long>(m_size - rename_offset - SystemTimeOfDayInfoLength*2);
  1538. }
  1539. std::size_t file_rename_information_size() const
  1540. { return static_cast<unsigned long>(m_size); }
  1541. private:
  1542. std::size_t m_size;
  1543. char *m_buf;
  1544. };
  1545. class c_heap_deleter
  1546. {
  1547. public:
  1548. explicit c_heap_deleter(std::size_t size)
  1549. : m_buf(::malloc(size))
  1550. {}
  1551. ~c_heap_deleter()
  1552. {
  1553. if(m_buf) ::free(m_buf);
  1554. }
  1555. void realloc_mem(std::size_t num_bytes)
  1556. {
  1557. void *oldBuf = m_buf;
  1558. m_buf = ::realloc(m_buf, num_bytes);
  1559. if (!m_buf){
  1560. free(oldBuf);
  1561. }
  1562. }
  1563. void *get() const
  1564. { return m_buf; }
  1565. private:
  1566. void *m_buf;
  1567. };
  1568. inline bool unlink_file(const char *filename)
  1569. {
  1570. //Don't try to optimize doing a DeleteFile first
  1571. //as there are interactions with permissions and
  1572. //in-use files.
  1573. //
  1574. //if(!delete_file(filename)){
  1575. // (...)
  1576. //
  1577. //This functions tries to emulate UNIX unlink semantics in windows.
  1578. //
  1579. //- Open the file and mark the handle as delete-on-close
  1580. //- Rename the file to an arbitrary name based on a random number
  1581. //- Close the handle. If there are no file users, it will be deleted.
  1582. // Otherwise it will be used by already connected handles but the
  1583. // file name can't be used to open this file again
  1584. try{
  1585. NtSetInformationFile_t pNtSetInformationFile =
  1586. reinterpret_cast<NtSetInformationFile_t>(dll_func::get(dll_func::NtSetInformationFile));
  1587. NtQueryObject_t pNtQueryObject = reinterpret_cast<NtQueryObject_t>(dll_func::get(dll_func::NtQueryObject));
  1588. //First step: Obtain a handle to the file using Win32 rules. This resolves relative paths
  1589. void *fh = create_file(filename, generic_read | delete_access, open_existing, 0, 0);
  1590. if(fh == invalid_handle_value){
  1591. return false;
  1592. }
  1593. handle_closer h_closer(fh);
  1594. {
  1595. //Obtain name length
  1596. unsigned long size;
  1597. const std::size_t initial_string_mem = 512u;
  1598. nt_query_mem_deleter nt_query_mem(sizeof(ntquery_mem_t)+initial_string_mem);
  1599. //Obtain file name with guessed length
  1600. if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
  1601. //Obtain file name with exact length buffer
  1602. nt_query_mem.realloc_mem(size);
  1603. if(pNtQueryObject(fh, object_name_information, nt_query_mem.query_mem(), nt_query_mem.object_name_information_size(), &size)){
  1604. return false;
  1605. }
  1606. }
  1607. ntquery_mem_t *pmem = nt_query_mem.query_mem();
  1608. file_rename_information_t *pfri = &pmem->ren.info;
  1609. const std::size_t RenMaxNumChars =
  1610. (((char*)(pmem) + nt_query_mem.file_rename_information_size()) - (char*)&pmem->ren.info.FileName[0])/sizeof(wchar_t);
  1611. //Copy filename to the rename member
  1612. std::memmove(pmem->ren.info.FileName, pmem->name.Name.Buffer, pmem->name.Name.Length);
  1613. std::size_t filename_string_length = pmem->name.Name.Length/sizeof(wchar_t);
  1614. //Search '\\' character to replace from it
  1615. for(std::size_t i = filename_string_length; i != 0; --filename_string_length){
  1616. if(pmem->ren.info.FileName[--i] == L'\\')
  1617. break;
  1618. }
  1619. //Add random number
  1620. std::size_t s = RenMaxNumChars - filename_string_length;
  1621. if(!get_boot_and_system_time_wstr(&pfri->FileName[filename_string_length], s)){
  1622. return false;
  1623. }
  1624. filename_string_length += s;
  1625. //Sometimes the precission of the timestamp is not enough and we need to add another random number.
  1626. //The process id (to exclude concurrent processes) and an atomic count (to exclude concurrent threads).
  1627. //should be enough
  1628. const unsigned long pid = get_current_process_id();
  1629. buffer_to_wide_str(&pid, sizeof(pid), &pfri->FileName[filename_string_length]);
  1630. filename_string_length += sizeof(pid)*2;
  1631. static volatile boost::uint32_t u32_count = 0;
  1632. interlocked_decrement(reinterpret_cast<volatile long*>(&u32_count));
  1633. buffer_to_wide_str(const_cast<const boost::uint32_t *>(&u32_count), sizeof(boost::uint32_t), &pfri->FileName[filename_string_length]);
  1634. filename_string_length += sizeof(boost::uint32_t)*2;
  1635. //Fill rename information (FileNameLength is in bytes)
  1636. pfri->FileNameLength = static_cast<unsigned long>(sizeof(wchar_t)*(filename_string_length));
  1637. pfri->Replace = 1;
  1638. pfri->RootDir = 0;
  1639. //Cange the name of the in-use file...
  1640. io_status_block_t io;
  1641. if(0 != pNtSetInformationFile(fh, &io, pfri, nt_query_mem.file_rename_information_size(), file_rename_information)){
  1642. return false;
  1643. }
  1644. }
  1645. //...and mark it as delete-on-close
  1646. {
  1647. //Don't use pNtSetInformationFile with file_disposition_information as it can return STATUS_CANNOT_DELETE
  1648. //if the file is still mapped. Reopen it with NtOpenFile and file_delete_on_close
  1649. NtOpenFile_t pNtOpenFile = reinterpret_cast<NtOpenFile_t>(dll_func::get(dll_func::NtOpenFile));
  1650. NtClose_t pNtClose = reinterpret_cast<NtClose_t>(dll_func::get(dll_func::NtClose));
  1651. const wchar_t empty_str [] = L"";
  1652. unicode_string_t ustring = { sizeof(empty_str) - sizeof (wchar_t) //length in bytes without null
  1653. , sizeof(empty_str) //total size in bytes of memory allocated for Buffer.
  1654. , const_cast<wchar_t*>(empty_str)
  1655. };
  1656. object_attributes_t object_attr;
  1657. initialize_object_attributes(&object_attr, &ustring, 0, fh, 0);
  1658. void* fh2 = 0;
  1659. io_status_block_t io;
  1660. pNtOpenFile( &fh2, delete_flag, &object_attr, &io
  1661. , file_share_read | file_share_write | file_share_delete, file_delete_on_close);
  1662. pNtClose(fh2);
  1663. //Even if NtOpenFile fails, the file was renamed and the original no longer exists, so return a success status
  1664. return true;
  1665. }
  1666. }
  1667. catch(...){
  1668. return false;
  1669. }
  1670. return true;
  1671. }
  1672. struct reg_closer
  1673. {
  1674. hkey key_;
  1675. reg_closer(hkey key) : key_(key){}
  1676. ~reg_closer(){ reg_close_key(key_); }
  1677. };
  1678. inline bool get_registry_value_buffer(hkey key_type, const char *subkey_name, const char *value_name, void *buf, std::size_t &buflen)
  1679. {
  1680. bool bret = false;
  1681. hkey key;
  1682. if (reg_open_key_ex( key_type
  1683. , subkey_name
  1684. , 0
  1685. , key_query_value
  1686. , &key) == 0){
  1687. reg_closer key_closer(key);
  1688. //Obtain the value
  1689. unsigned long size = buflen;
  1690. unsigned long type;
  1691. buflen = 0;
  1692. bret = 0 == reg_query_value_ex( key, value_name, 0, &type, (unsigned char*)buf, &size);
  1693. if(bret)
  1694. buflen = (std::size_t)size;
  1695. }
  1696. return bret;
  1697. }
  1698. inline bool get_registry_value_string(hkey key_type, const char *subkey_name, const char *value_name, std::string &s)
  1699. {
  1700. bool bret = false;
  1701. s.clear();
  1702. hkey key;
  1703. if (reg_open_key_ex( key_type
  1704. , subkey_name
  1705. , 0
  1706. , key_query_value
  1707. , &key) == 0){
  1708. reg_closer key_closer(key);
  1709. //Obtain the value
  1710. unsigned long size;
  1711. unsigned long type;
  1712. long err = reg_query_value_ex( key, value_name, 0, &type, 0, &size);
  1713. if((reg_sz == type || reg_expand_sz == type) && !err){
  1714. //Size includes terminating NULL
  1715. s.resize(size);
  1716. err = reg_query_value_ex( key, value_name, 0, &type, (unsigned char*)(&s[0]), &size);
  1717. if(!err){
  1718. s.erase(s.end()-1);
  1719. bret = true;
  1720. }
  1721. (void)err;
  1722. }
  1723. }
  1724. return bret;
  1725. }
  1726. inline void get_shared_documents_folder(std::string &s)
  1727. {
  1728. #if 1 //Original registry search code
  1729. get_registry_value_string( hkey_local_machine
  1730. , "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
  1731. , "Common AppData"
  1732. , s);
  1733. #else //registry alternative: SHGetFolderPath
  1734. const int BIPC_CSIDL_COMMON_APPDATA = 0x0023; // All Users\Application Data
  1735. const int BIPC_CSIDL_FLAG_CREATE = 0x8000; // new for Win2K, or this in to force creation of folder
  1736. const int BIPC_SHGFP_TYPE_CURRENT = 0; // current value for user, verify it exists
  1737. s.clear();
  1738. char szPath[max_path];
  1739. if(0 == SHGetFolderPathA(0, BIPC_CSIDL_COMMON_APPDATA | BIPC_CSIDL_FLAG_CREATE, 0, BIPC_SHGFP_TYPE_CURRENT, szPath)){
  1740. s = szPath;
  1741. }
  1742. #endif
  1743. }
  1744. inline void get_registry_value(const char *folder, const char *value_key, std::vector<unsigned char> &s)
  1745. {
  1746. s.clear();
  1747. hkey key;
  1748. if (reg_open_key_ex( hkey_local_machine
  1749. , folder
  1750. , 0
  1751. , key_query_value
  1752. , &key) == 0){
  1753. reg_closer key_closer(key);
  1754. //Obtain the value
  1755. unsigned long size;
  1756. unsigned long type;
  1757. const char *const reg_value = value_key;
  1758. //long err = (*pRegQueryValue)( key, reg_value, 0, &type, 0, &size);
  1759. long err = reg_query_value_ex( key, reg_value, 0, &type, 0, &size);
  1760. if(!err){
  1761. //Size includes terminating NULL
  1762. s.resize(size);
  1763. //err = (*pRegQueryValue)( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
  1764. err = reg_query_value_ex( key, reg_value, 0, &type, (unsigned char*)(&s[0]), &size);
  1765. if(!err)
  1766. s.erase(s.end()-1);
  1767. (void)err;
  1768. }
  1769. }
  1770. }
  1771. #if defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME)
  1772. struct co_uninitializer
  1773. {
  1774. co_uninitializer(bool b_uninitialize)
  1775. : m_b_uninitialize(b_uninitialize)
  1776. {}
  1777. ~co_uninitializer()
  1778. {
  1779. if(m_b_uninitialize){
  1780. CoUninitialize();
  1781. }
  1782. }
  1783. private:
  1784. const bool m_b_uninitialize;
  1785. };
  1786. template<class Object>
  1787. struct com_releaser
  1788. {
  1789. Object *&object_;
  1790. com_releaser(Object *&object) : object_(object) {}
  1791. ~com_releaser() { object_->Release(); object_ = 0; }
  1792. };
  1793. inline bool get_wmi_class_attribute( std::wstring& strValue, const wchar_t *wmi_class, const wchar_t *wmi_class_var)
  1794. {
  1795. //See example http://msdn.microsoft.com/en-us/library/aa390423%28v=VS.85%29.aspx
  1796. //
  1797. //See BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL definition if you need to change the
  1798. //default value of this macro in your application
  1799. long co_init_ret = CoInitializeEx(0, BOOST_INTERPROCESS_WINDOWS_COINIT_MODEL);
  1800. if(co_init_ret != S_OK_BIPC && co_init_ret != S_FALSE_BIPC && co_init_ret != RPC_E_CHANGED_MODE_BIPC)
  1801. return false;
  1802. co_uninitializer co_initialize_end(co_init_ret != RPC_E_CHANGED_MODE_BIPC);
  1803. (void)co_initialize_end;
  1804. bool bRet = false;
  1805. long sec_init_ret = CoInitializeSecurity
  1806. ( 0 //pVoid
  1807. ,-1 //cAuthSvc
  1808. , 0 //asAuthSvc
  1809. , 0 //pReserved1
  1810. , RPC_C_AUTHN_LEVEL_PKT_BIPC //dwAuthnLevel
  1811. , RPC_C_IMP_LEVEL_IMPERSONATE_BIPC //dwImpLevel
  1812. , 0 //pAuthList
  1813. , EOAC_NONE_BIPC //dwCapabilities
  1814. , 0 //pReserved3
  1815. );
  1816. if( 0 == sec_init_ret || RPC_E_TOO_LATE_BIPC == sec_init_ret)
  1817. {
  1818. IWbemLocator_BIPC * pIWbemLocator = 0;
  1819. const wchar_t * bstrNamespace = L"root\\cimv2";
  1820. if( 0 != CoCreateInstance(
  1821. CLSID_WbemAdministrativeLocator,
  1822. 0,
  1823. CLSCTX_INPROC_SERVER_BIPC | CLSCTX_LOCAL_SERVER_BIPC,
  1824. IID_IUnknown, (void **)&pIWbemLocator)){
  1825. return false;
  1826. }
  1827. com_releaser<IWbemLocator_BIPC> IWbemLocator_releaser(pIWbemLocator);
  1828. IWbemServices_BIPC *pWbemServices = 0;
  1829. if( 0 != pIWbemLocator->ConnectServer(
  1830. (bstr)bstrNamespace, // Namespace
  1831. 0, // Userid
  1832. 0, // PW
  1833. 0, // Locale
  1834. 0, // flags
  1835. 0, // Authority
  1836. 0, // Context
  1837. &pWbemServices
  1838. )
  1839. ){
  1840. return false;
  1841. }
  1842. if( S_OK_BIPC != CoSetProxyBlanket(
  1843. pWbemServices,
  1844. RPC_C_AUTHN_DEFAULT_BIPC,
  1845. RPC_C_AUTHZ_DEFAULT_BIPC,
  1846. 0,
  1847. RPC_C_AUTHN_LEVEL_PKT_BIPC,
  1848. RPC_C_IMP_LEVEL_IMPERSONATE_BIPC,
  1849. 0,
  1850. EOAC_NONE_BIPC
  1851. )
  1852. ){
  1853. return false;
  1854. }
  1855. com_releaser<IWbemServices_BIPC> IWbemServices_releaser(pWbemServices);
  1856. strValue.clear();
  1857. strValue += L"Select ";
  1858. strValue += wmi_class_var;
  1859. strValue += L" from ";
  1860. strValue += wmi_class;
  1861. IEnumWbemClassObject_BIPC * pEnumObject = 0;
  1862. if ( 0 != pWbemServices->ExecQuery(
  1863. (bstr)L"WQL",
  1864. (bstr)strValue.c_str(),
  1865. //WBEM_FLAG_RETURN_IMMEDIATELY_BIPC,
  1866. WBEM_FLAG_RETURN_WHEN_COMPLETE_BIPC | WBEM_FLAG_FORWARD_ONLY_BIPC,
  1867. 0,
  1868. &pEnumObject
  1869. )
  1870. ){
  1871. return false;
  1872. }
  1873. com_releaser<IEnumWbemClassObject_BIPC> IEnumWbemClassObject_releaser(pEnumObject);
  1874. //WBEM_FLAG_FORWARD_ONLY_BIPC incompatible with Reset
  1875. //if ( 0 != pEnumObject->Reset() ){
  1876. //return false;
  1877. //}
  1878. wchar_variant vwchar;
  1879. unsigned long uCount = 1, uReturned;
  1880. IWbemClassObject_BIPC * pClassObject = 0;
  1881. while( 0 == pEnumObject->Next( WBEM_INFINITE_BIPC, uCount, &pClassObject, &uReturned ) )
  1882. {
  1883. com_releaser<IWbemClassObject_BIPC> IWbemClassObject_releaser(pClassObject);
  1884. if ( 0 == pClassObject->Get( (bstr)L"LastBootUpTime", 0, &vwchar, 0, 0 ) ){
  1885. bRet = true;
  1886. strValue = (wchar_t*)vwchar.bstrVal;
  1887. VariantClear(&vwchar );
  1888. break;
  1889. }
  1890. }
  1891. }
  1892. return bRet;
  1893. }
  1894. //Obtains the bootup time from WMI LastBootUpTime.
  1895. //This time seems to change with hibernation and clock synchronization so avoid it.
  1896. inline bool get_last_bootup_time( std::wstring& strValue )
  1897. {
  1898. bool ret = get_wmi_class_attribute(strValue, L"Win32_OperatingSystem", L"LastBootUpTime");
  1899. std::size_t timezone = strValue.find(L'+');
  1900. if(timezone != std::wstring::npos){
  1901. strValue.erase(timezone);
  1902. }
  1903. timezone = strValue.find(L'-');
  1904. if(timezone != std::wstring::npos){
  1905. strValue.erase(timezone);
  1906. }
  1907. return ret;
  1908. }
  1909. inline bool get_last_bootup_time( std::string& str )
  1910. {
  1911. std::wstring wstr;
  1912. bool ret = get_last_bootup_time(wstr);
  1913. str.resize(wstr.size());
  1914. for(std::size_t i = 0, max = str.size(); i != max; ++i){
  1915. str[i] = '0' + (wstr[i]-L'0');
  1916. }
  1917. return ret;
  1918. }
  1919. #endif //BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME
  1920. #if defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED)
  1921. // Loop through the buffer and obtain the contents of the
  1922. // requested record in the buffer.
  1923. inline bool find_record_in_buffer( const void* pBuffer, unsigned long dwBytesRead, const char *provider_name
  1924. , unsigned int id_to_find, interprocess_eventlogrecord *&pevent_log_record)
  1925. {
  1926. const unsigned char * pRecord = static_cast<const unsigned char*>(pBuffer);
  1927. const unsigned char * pEndOfRecords = pRecord + dwBytesRead;
  1928. while (pRecord < pEndOfRecords){
  1929. interprocess_eventlogrecord *pTypedRecord = (interprocess_eventlogrecord*)pRecord;
  1930. // Check provider, written at the end of the fixed-part of the record
  1931. if (0 == std::strcmp(provider_name, (char*)(pRecord + sizeof(interprocess_eventlogrecord))))
  1932. {
  1933. // Check event id
  1934. if(id_to_find == (pTypedRecord->EventID & 0xFFFF)){
  1935. pevent_log_record = pTypedRecord;
  1936. return true;
  1937. }
  1938. }
  1939. pRecord += pTypedRecord->Length;
  1940. }
  1941. pevent_log_record = 0;
  1942. return false;
  1943. }
  1944. //Obtains the bootup time from the System Event Log,
  1945. //event ID == 6005 (event log started).
  1946. //Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/bb427356.aspx
  1947. inline bool get_last_bootup_time(std::string &stamp)
  1948. {
  1949. const char *source_name = "System";
  1950. const char *provider_name = "EventLog";
  1951. const unsigned short event_id = 6005u;
  1952. unsigned long status = 0;
  1953. unsigned long dwBytesToRead = 0;
  1954. unsigned long dwBytesRead = 0;
  1955. unsigned long dwMinimumBytesToRead = 0;
  1956. // The source name (provider) must exist as a subkey of Application.
  1957. void *hEventLog = OpenEventLogA(0, source_name);
  1958. if (hEventLog){
  1959. eventlog_handle_closer hnd_closer(hEventLog); (void)hnd_closer;
  1960. // Allocate an initial block of memory used to read event records. The number
  1961. // of records read into the buffer will vary depending on the size of each event.
  1962. // The size of each event will vary based on the size of the user-defined
  1963. // data included with each event, the number and length of insertion
  1964. // strings, and other data appended to the end of the event record.
  1965. dwBytesToRead = max_record_buffer_size;
  1966. c_heap_deleter heap_deleter(dwBytesToRead);
  1967. // Read blocks of records until you reach the end of the log or an
  1968. // error occurs. The records are read from newest to oldest. If the buffer
  1969. // is not big enough to hold a complete event record, reallocate the buffer.
  1970. if (heap_deleter.get() != 0){
  1971. while (0 == status){
  1972. if (!ReadEventLogA(hEventLog,
  1973. eventlog_sequential_read | eventlog_backwards_read,
  1974. 0,
  1975. heap_deleter.get(),
  1976. dwBytesToRead,
  1977. &dwBytesRead,
  1978. &dwMinimumBytesToRead)) {
  1979. status = get_last_error();
  1980. if (error_insufficient_buffer == status) {
  1981. status = 0;
  1982. dwBytesToRead = dwMinimumBytesToRead;
  1983. heap_deleter.realloc_mem(dwMinimumBytesToRead);
  1984. if (!heap_deleter.get()){
  1985. return false;
  1986. }
  1987. }
  1988. else{ //Not found or EOF
  1989. return false;
  1990. }
  1991. }
  1992. else
  1993. {
  1994. interprocess_eventlogrecord *pTypedRecord;
  1995. // Print the contents of each record in the buffer.
  1996. if(find_record_in_buffer(heap_deleter.get(), dwBytesRead, provider_name, event_id, pTypedRecord)){
  1997. char stamp_str[sizeof(unsigned long)*3+1];
  1998. std::sprintf(&stamp_str[0], "%u", ((unsigned int)pTypedRecord->TimeGenerated));
  1999. stamp = stamp_str;
  2000. break;
  2001. }
  2002. }
  2003. }
  2004. }
  2005. }
  2006. return true;
  2007. }
  2008. #endif //BOOST_INTERPROCESS_BOOTSTAMP_IS_EVENTLOG_BASED
  2009. #if defined(BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED)
  2010. inline bool get_last_bootup_time(std::string &stamp)
  2011. {
  2012. unsigned dword_val = 0;
  2013. std::size_t dword_size = sizeof(dword_val);
  2014. bool b_ret = get_registry_value_buffer( hkey_local_machine
  2015. , "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management\\PrefetchParameters"
  2016. , "BootId", &dword_val, dword_size);
  2017. if (b_ret)
  2018. {
  2019. char dword_str[sizeof(dword_val)*2u+1];
  2020. buffer_to_narrow_str(&dword_val, dword_size, dword_str);
  2021. dword_str[sizeof(dword_val)*2] = '\0';
  2022. stamp = dword_str;
  2023. b_ret = get_registry_value_buffer( hkey_local_machine
  2024. , "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Power"
  2025. , "HybridBootAnimationTime", &dword_val, dword_size);
  2026. //Old Windows versions have no HybridBootAnimationTime
  2027. if(b_ret)
  2028. {
  2029. buffer_to_narrow_str(&dword_val, dword_size, dword_str);
  2030. dword_str[sizeof(dword_val)*2] = '\0';
  2031. stamp += "_";
  2032. stamp += dword_str;
  2033. }
  2034. b_ret = true;
  2035. }
  2036. return b_ret;
  2037. }
  2038. #endif //BOOST_INTERPROCESS_BOOTSTAMP_IS_SESSION_MANAGER_BASED
  2039. inline bool is_directory(const char *path)
  2040. {
  2041. unsigned long attrib = GetFileAttributesA(path);
  2042. return (attrib != invalid_file_attributes &&
  2043. (attrib & file_attribute_directory));
  2044. }
  2045. inline bool get_file_mapping_size(void *file_mapping_hnd, __int64 &size)
  2046. {
  2047. NtQuerySection_t pNtQuerySection =
  2048. reinterpret_cast<NtQuerySection_t>(dll_func::get(dll_func::NtQuerySection));
  2049. //Obtain file name
  2050. interprocess_section_basic_information info;
  2051. unsigned long ntstatus =
  2052. pNtQuerySection(file_mapping_hnd, section_basic_information, &info, sizeof(info), 0);
  2053. size = info.section_size;
  2054. return !ntstatus;
  2055. }
  2056. inline bool get_semaphore_info(void *handle, long &count, long &limit)
  2057. {
  2058. winapi::interprocess_semaphore_basic_information info;
  2059. winapi::NtQuerySemaphore_t pNtQuerySemaphore =
  2060. reinterpret_cast<winapi::NtQuerySemaphore_t>(dll_func::get(winapi::dll_func::NtQuerySemaphore));
  2061. unsigned int ret_len;
  2062. long status = pNtQuerySemaphore(handle, winapi::semaphore_basic_information, &info, sizeof(info), &ret_len);
  2063. count = info.count;
  2064. limit = info.limit;
  2065. return !status;
  2066. }
  2067. inline bool query_timer_resolution(unsigned long *lowres, unsigned long *highres, unsigned long *curres)
  2068. {
  2069. winapi::NtQueryTimerResolution_t pNtQueryTimerResolution =
  2070. reinterpret_cast<winapi::NtQueryTimerResolution_t>(dll_func::get(winapi::dll_func::NtQueryTimerResolution));
  2071. return !pNtQueryTimerResolution(lowres, highres, curres);
  2072. }
  2073. inline bool query_performance_counter(__int64 *lpPerformanceCount)
  2074. {
  2075. QueryPerformanceCounter_t pQueryPerformanceCounter = reinterpret_cast<QueryPerformanceCounter_t>
  2076. (dll_func::get(dll_func::QueryPerformanceCounter));
  2077. return 0 != pQueryPerformanceCounter(lpPerformanceCount);
  2078. }
  2079. inline bool query_performance_frequency(__int64 *lpFrequency)
  2080. {
  2081. QueryPerformanceCounter_t pQueryPerformanceFrequency = reinterpret_cast<QueryPerformanceFrequency_t>
  2082. (dll_func::get(dll_func::QueryPerformanceFrequency));
  2083. return 0 != pQueryPerformanceFrequency(lpFrequency);
  2084. }
  2085. inline unsigned long get_tick_count()
  2086. { return GetTickCount(); }
  2087. } //namespace winapi
  2088. } //namespace interprocess
  2089. } //namespace boost
  2090. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  2091. # pragma GCC diagnostic pop
  2092. #endif
  2093. #include <boost/interprocess/detail/config_end.hpp>
  2094. #endif //#ifdef BOOST_INTERPROCESS_WIN32_API_HPP