win32_api.hpp 85 KB

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