win32_api.hpp 68 KB

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