win32_api.hpp 77 KB

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