PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/referencesource/System.Core/System/Diagnostics/Eventing/Reader/NativeWrapper.cs

https://github.com/pruiz/mono
C# | 997 lines | 812 code | 130 blank | 55 comment | 169 complexity | 050df31eaf93f8c0bf3c969f9f39b184 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // ==++==
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // ==--==
  6. /*============================================================
  7. **
  8. ** Class: NativeWrapper
  9. **
  10. ** Purpose:
  11. ** This internal class contains wrapper methods over the Native
  12. ** Methods of the Eventlog API. Unlike the raw Native Methods,
  13. ** these methods throw EventLogExceptions, check platform
  14. ** availablity and perform additional helper functionality
  15. ** specific to function. Also, all methods of this class expose
  16. ** the Link Demand for Unmanaged Permission to callers.
  17. **
  18. ============================================================*/
  19. using System;
  20. using System.Collections;
  21. using System.Collections.Generic;
  22. using System.Runtime.InteropServices;
  23. using System.Text;
  24. using System.Security.Principal;
  25. using System.Security.Permissions;
  26. using Microsoft.Win32.SafeHandles;
  27. using Microsoft.Win32;
  28. namespace System.Diagnostics.Eventing.Reader {
  29. internal class NativeWrapper {
  30. private static bool s_platformNotSupported = (Environment.OSVersion.Version.Major < 6);
  31. [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
  32. public class SystemProperties {
  33. //indicates if the SystemProperties values were already computed (for this event Instance, surely).
  34. public bool filled = false;
  35. public ushort? Id = null;
  36. public byte? Version = null;
  37. public ushort? Qualifiers = null;
  38. public byte? Level = null;
  39. public ushort? Task = null;
  40. public byte? Opcode = null;
  41. public ulong? Keywords = null;
  42. public ulong? RecordId = null;
  43. public string ProviderName = null;
  44. public Guid? ProviderId = null;
  45. public string ChannelName = null;
  46. public uint? ProcessId = null;
  47. public uint? ThreadId = null;
  48. public string ComputerName = null;
  49. public System.Security.Principal.SecurityIdentifier UserId = null;
  50. public DateTime? TimeCreated = null;
  51. public Guid? ActivityId = null;
  52. public Guid? RelatedActivityId = null;
  53. public SystemProperties() {
  54. }
  55. }
  56. [System.Security.SecurityCritical]
  57. public static EventLogHandle EvtQuery(
  58. EventLogHandle session,
  59. string path,
  60. string query,
  61. int flags) {
  62. if (s_platformNotSupported)
  63. throw new System.PlatformNotSupportedException();
  64. EventLogHandle handle = UnsafeNativeMethods.EvtQuery(session, path, query, flags);
  65. int win32Error = Marshal.GetLastWin32Error();
  66. if (handle.IsInvalid)
  67. EventLogException.Throw(win32Error);
  68. return handle;
  69. }
  70. [System.Security.SecurityCritical]
  71. public static void EvtSeek(
  72. EventLogHandle resultSet,
  73. long position,
  74. EventLogHandle bookmark,
  75. int timeout,
  76. UnsafeNativeMethods.EvtSeekFlags flags) {
  77. bool status = UnsafeNativeMethods.EvtSeek(resultSet, position, bookmark, timeout, flags);
  78. int win32Error = Marshal.GetLastWin32Error();
  79. if (!status)
  80. EventLogException.Throw(win32Error);
  81. }
  82. [System.Security.SecurityCritical]
  83. public static EventLogHandle EvtSubscribe(
  84. EventLogHandle session,
  85. SafeWaitHandle signalEvent,
  86. string path,
  87. string query,
  88. EventLogHandle bookmark,
  89. IntPtr context,
  90. IntPtr callback,
  91. int flags) {
  92. if (s_platformNotSupported)
  93. throw new System.PlatformNotSupportedException();
  94. EventLogHandle handle = UnsafeNativeMethods.EvtSubscribe(
  95. session,
  96. signalEvent,
  97. path,
  98. query,
  99. bookmark,
  100. context,
  101. callback,
  102. flags);
  103. int win32Error = Marshal.GetLastWin32Error();
  104. if (handle.IsInvalid)
  105. EventLogException.Throw(win32Error);
  106. return handle;
  107. }
  108. [System.Security.SecurityCritical]
  109. public static bool EvtNext(
  110. EventLogHandle queryHandle,
  111. int eventSize,
  112. IntPtr[] events,
  113. int timeout,
  114. int flags,
  115. ref int returned) {
  116. bool status = UnsafeNativeMethods.EvtNext(queryHandle, eventSize, events, timeout, flags, ref returned);
  117. int win32Error = Marshal.GetLastWin32Error();
  118. if (!status && win32Error != UnsafeNativeMethods.ERROR_NO_MORE_ITEMS)
  119. EventLogException.Throw(win32Error);
  120. return win32Error == 0;
  121. }
  122. [System.Security.SecuritySafeCritical]
  123. public static void EvtCancel(EventLogHandle handle) {
  124. EventLogPermissionHolder.GetEventLogPermission().Demand();
  125. if (!UnsafeNativeMethods.EvtCancel(handle)) {
  126. int win32Error = Marshal.GetLastWin32Error();
  127. EventLogException.Throw(win32Error);
  128. }
  129. }
  130. [System.Security.SecurityCritical]
  131. public static void EvtClose(IntPtr handle) {
  132. //
  133. // purposely don't check and throw - this is
  134. // always called in cleanup / finalize / etc..
  135. //
  136. UnsafeNativeMethods.EvtClose(handle);
  137. }
  138. [System.Security.SecurityCritical]
  139. public static EventLogHandle EvtOpenProviderMetadata(
  140. EventLogHandle session,
  141. string ProviderId,
  142. string logFilePath,
  143. int locale,
  144. int flags) {
  145. if (s_platformNotSupported)
  146. throw new System.PlatformNotSupportedException();
  147. //
  148. // ignore locale and pass 0 instead: that way, the thread locale will be retrieved in the API layer
  149. // and the "strict rendering" flag will NOT be set. Otherwise, the fall back logic is broken and the descriptions
  150. // are not returned if the exact locale is not present on the server.
  151. //
  152. EventLogHandle handle = UnsafeNativeMethods.EvtOpenPublisherMetadata(session, ProviderId, logFilePath, 0, flags);
  153. int win32Error = Marshal.GetLastWin32Error();
  154. if (handle.IsInvalid)
  155. EventLogException.Throw(win32Error);
  156. return handle;
  157. }
  158. [System.Security.SecurityCritical]
  159. public static int EvtGetObjectArraySize(EventLogHandle objectArray) {
  160. int arraySize;
  161. bool status = UnsafeNativeMethods.EvtGetObjectArraySize(objectArray, out arraySize);
  162. int win32Error = Marshal.GetLastWin32Error();
  163. if (!status)
  164. EventLogException.Throw(win32Error);
  165. return arraySize;
  166. }
  167. [System.Security.SecurityCritical]
  168. public static EventLogHandle EvtOpenEventMetadataEnum(EventLogHandle ProviderMetadata, int flags) {
  169. EventLogHandle emEnumHandle = UnsafeNativeMethods.EvtOpenEventMetadataEnum(ProviderMetadata, flags);
  170. int win32Error = Marshal.GetLastWin32Error();
  171. if (emEnumHandle.IsInvalid)
  172. EventLogException.Throw(win32Error);
  173. return emEnumHandle;
  174. }
  175. // returns null if EOF
  176. [System.Security.SecurityCritical]
  177. public static EventLogHandle EvtNextEventMetadata(EventLogHandle eventMetadataEnum, int flags) {
  178. EventLogHandle emHandle = UnsafeNativeMethods.EvtNextEventMetadata(eventMetadataEnum, flags);
  179. int win32Error = Marshal.GetLastWin32Error();
  180. if (emHandle.IsInvalid) {
  181. if (win32Error != UnsafeNativeMethods.ERROR_NO_MORE_ITEMS)
  182. EventLogException.Throw(win32Error);
  183. return null;
  184. }
  185. return emHandle;
  186. }
  187. [System.Security.SecurityCritical]
  188. public static EventLogHandle EvtOpenChannelEnum(EventLogHandle session, int flags) {
  189. if (s_platformNotSupported)
  190. throw new System.PlatformNotSupportedException();
  191. EventLogHandle channelEnum = UnsafeNativeMethods.EvtOpenChannelEnum(session, flags);
  192. int win32Error = Marshal.GetLastWin32Error();
  193. if (channelEnum.IsInvalid)
  194. EventLogException.Throw(win32Error);
  195. return channelEnum;
  196. }
  197. [System.Security.SecurityCritical]
  198. public static EventLogHandle EvtOpenProviderEnum(EventLogHandle session, int flags) {
  199. if (s_platformNotSupported)
  200. throw new System.PlatformNotSupportedException();
  201. EventLogHandle pubEnum = UnsafeNativeMethods.EvtOpenPublisherEnum(session, flags);
  202. int win32Error = Marshal.GetLastWin32Error();
  203. if (pubEnum.IsInvalid)
  204. EventLogException.Throw(win32Error);
  205. return pubEnum;
  206. }
  207. [System.Security.SecurityCritical]
  208. public static EventLogHandle EvtOpenChannelConfig(EventLogHandle session, String channelPath, int flags) {
  209. if (s_platformNotSupported)
  210. throw new System.PlatformNotSupportedException();
  211. EventLogHandle handle = UnsafeNativeMethods.EvtOpenChannelConfig(session, channelPath, flags);
  212. int win32Error = Marshal.GetLastWin32Error();
  213. if (handle.IsInvalid)
  214. EventLogException.Throw(win32Error);
  215. return handle;
  216. }
  217. [System.Security.SecuritySafeCritical]
  218. public static void EvtSaveChannelConfig(EventLogHandle channelConfig, int flags) {
  219. EventLogPermissionHolder.GetEventLogPermission().Demand();
  220. bool status = UnsafeNativeMethods.EvtSaveChannelConfig(channelConfig, flags);
  221. int win32Error = Marshal.GetLastWin32Error();
  222. if (!status)
  223. EventLogException.Throw(win32Error);
  224. }
  225. [System.Security.SecurityCritical]
  226. public static EventLogHandle EvtOpenLog(EventLogHandle session, string path, PathType flags) {
  227. if (s_platformNotSupported)
  228. throw new System.PlatformNotSupportedException();
  229. EventLogHandle logHandle = UnsafeNativeMethods.EvtOpenLog(session, path, flags);
  230. int win32Error = Marshal.GetLastWin32Error();
  231. if (logHandle.IsInvalid)
  232. EventLogException.Throw(win32Error);
  233. return logHandle;
  234. }
  235. [System.Security.SecuritySafeCritical]
  236. public static void EvtExportLog(
  237. EventLogHandle session,
  238. string channelPath,
  239. string query,
  240. string targetFilePath,
  241. int flags) {
  242. if (s_platformNotSupported)
  243. throw new System.PlatformNotSupportedException();
  244. EventLogPermissionHolder.GetEventLogPermission().Demand();
  245. bool status;
  246. status = UnsafeNativeMethods.EvtExportLog(session, channelPath, query, targetFilePath, flags);
  247. int win32Error = Marshal.GetLastWin32Error();
  248. if (!status)
  249. EventLogException.Throw(win32Error);
  250. }
  251. [System.Security.SecuritySafeCritical]
  252. public static void EvtArchiveExportedLog(
  253. EventLogHandle session,
  254. string logFilePath,
  255. int locale,
  256. int flags) {
  257. if (s_platformNotSupported)
  258. throw new System.PlatformNotSupportedException();
  259. EventLogPermissionHolder.GetEventLogPermission().Demand();
  260. bool status;
  261. status = UnsafeNativeMethods.EvtArchiveExportedLog(session, logFilePath, locale, flags);
  262. int win32Error = Marshal.GetLastWin32Error();
  263. if (!status)
  264. EventLogException.Throw(win32Error);
  265. }
  266. [System.Security.SecuritySafeCritical]
  267. public static void EvtClearLog(
  268. EventLogHandle session,
  269. string channelPath,
  270. string targetFilePath,
  271. int flags) {
  272. if (s_platformNotSupported)
  273. throw new System.PlatformNotSupportedException();
  274. EventLogPermissionHolder.GetEventLogPermission().Demand();
  275. bool status;
  276. status = UnsafeNativeMethods.EvtClearLog(session, channelPath, targetFilePath, flags);
  277. int win32Error = Marshal.GetLastWin32Error();
  278. if (!status)
  279. EventLogException.Throw(win32Error);
  280. }
  281. [System.Security.SecurityCritical]
  282. public static EventLogHandle EvtCreateRenderContext(
  283. Int32 valuePathsCount,
  284. String[] valuePaths,
  285. UnsafeNativeMethods.EvtRenderContextFlags flags) {
  286. if (s_platformNotSupported)
  287. throw new System.PlatformNotSupportedException();
  288. EventLogHandle renderContextHandleValues = UnsafeNativeMethods.EvtCreateRenderContext(valuePathsCount, valuePaths, flags);
  289. int win32Error = Marshal.GetLastWin32Error();
  290. if (renderContextHandleValues.IsInvalid)
  291. EventLogException.Throw(win32Error);
  292. return renderContextHandleValues;
  293. }
  294. [System.Security.SecurityCritical]
  295. public static void EvtRender(
  296. EventLogHandle context,
  297. EventLogHandle eventHandle,
  298. UnsafeNativeMethods.EvtRenderFlags flags,
  299. StringBuilder buffer) {
  300. if (s_platformNotSupported)
  301. throw new System.PlatformNotSupportedException();
  302. int buffUsed;
  303. int propCount;
  304. bool status = UnsafeNativeMethods.EvtRender(context, eventHandle, flags, buffer.Capacity, buffer, out buffUsed, out propCount);
  305. int win32Error = Marshal.GetLastWin32Error();
  306. if (!status) {
  307. if (win32Error == UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) {
  308. //reallocate the new RenderBuffer with the right size.
  309. buffer.Capacity = buffUsed;
  310. status = UnsafeNativeMethods.EvtRender(context, eventHandle, flags, buffer.Capacity, buffer, out buffUsed, out propCount);
  311. win32Error = Marshal.GetLastWin32Error();
  312. }
  313. if (!status) {
  314. EventLogException.Throw(win32Error);
  315. }
  316. }
  317. }
  318. [System.Security.SecurityCritical]
  319. public static EventLogHandle EvtOpenSession(UnsafeNativeMethods.EvtLoginClass loginClass, ref UnsafeNativeMethods.EvtRpcLogin login, int timeout, int flags) {
  320. if (s_platformNotSupported)
  321. throw new System.PlatformNotSupportedException();
  322. EventLogHandle handle = UnsafeNativeMethods.EvtOpenSession(loginClass, ref login, timeout, flags);
  323. int win32Error = Marshal.GetLastWin32Error();
  324. if (handle.IsInvalid)
  325. EventLogException.Throw(win32Error);
  326. return handle;
  327. }
  328. [System.Security.SecurityCritical]
  329. public static EventLogHandle EvtCreateBookmark(string bookmarkXml) {
  330. if (s_platformNotSupported)
  331. throw new System.PlatformNotSupportedException();
  332. EventLogHandle handle = UnsafeNativeMethods.EvtCreateBookmark(bookmarkXml);
  333. int win32Error = Marshal.GetLastWin32Error();
  334. if (handle.IsInvalid)
  335. EventLogException.Throw(win32Error);
  336. return handle;
  337. }
  338. [System.Security.SecurityCritical]
  339. public static void EvtUpdateBookmark(EventLogHandle bookmark, EventLogHandle eventHandle) {
  340. bool status = UnsafeNativeMethods.EvtUpdateBookmark(bookmark, eventHandle);
  341. int win32Error = Marshal.GetLastWin32Error();
  342. if (!status)
  343. EventLogException.Throw(win32Error);
  344. }
  345. [System.Security.SecuritySafeCritical]
  346. public static object EvtGetEventInfo(EventLogHandle handle, UnsafeNativeMethods.EvtEventPropertyId enumType) {
  347. IntPtr buffer = IntPtr.Zero;
  348. int bufferNeeded;
  349. EventLogPermissionHolder.GetEventLogPermission().Demand();
  350. try {
  351. bool status = UnsafeNativeMethods.EvtGetEventInfo(handle, enumType, 0, IntPtr.Zero, out bufferNeeded);
  352. int error = Marshal.GetLastWin32Error();
  353. if (!status) {
  354. if (error == UnsafeNativeMethods.ERROR_SUCCESS) { }
  355. else
  356. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  357. EventLogException.Throw(error);
  358. }
  359. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  360. status = UnsafeNativeMethods.EvtGetEventInfo(handle, enumType, bufferNeeded, buffer, out bufferNeeded);
  361. error = Marshal.GetLastWin32Error();
  362. if (!status)
  363. EventLogException.Throw(error);
  364. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.EvtVariant));
  365. return ConvertToObject(varVal);
  366. }
  367. finally {
  368. if (buffer != IntPtr.Zero)
  369. Marshal.FreeHGlobal(buffer);
  370. }
  371. }
  372. [System.Security.SecurityCritical]
  373. public static object EvtGetQueryInfo(EventLogHandle handle, UnsafeNativeMethods.EvtQueryPropertyId enumType) {
  374. IntPtr buffer = IntPtr.Zero;
  375. int bufferNeeded = 0;
  376. try {
  377. bool status = UnsafeNativeMethods.EvtGetQueryInfo(handle, enumType, 0, IntPtr.Zero, ref bufferNeeded);
  378. int error = Marshal.GetLastWin32Error();
  379. if (!status) {
  380. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  381. EventLogException.Throw(error);
  382. }
  383. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  384. status = UnsafeNativeMethods.EvtGetQueryInfo(handle, enumType, bufferNeeded, buffer, ref bufferNeeded);
  385. error = Marshal.GetLastWin32Error();
  386. if (!status)
  387. EventLogException.Throw(error);
  388. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.EvtVariant));
  389. return ConvertToObject(varVal);
  390. }
  391. finally {
  392. if (buffer != IntPtr.Zero) Marshal.FreeHGlobal(buffer);
  393. }
  394. }
  395. [System.Security.SecuritySafeCritical]
  396. public static object EvtGetPublisherMetadataProperty(EventLogHandle pmHandle, UnsafeNativeMethods.EvtPublisherMetadataPropertyId thePropertyId) {
  397. IntPtr buffer = IntPtr.Zero;
  398. int bufferNeeded;
  399. EventLogPermissionHolder.GetEventLogPermission().Demand();
  400. try {
  401. bool status = UnsafeNativeMethods.EvtGetPublisherMetadataProperty(pmHandle, thePropertyId, 0, 0, IntPtr.Zero, out bufferNeeded);
  402. int error = Marshal.GetLastWin32Error();
  403. if (!status) {
  404. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  405. EventLogException.Throw(error);
  406. }
  407. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  408. status = UnsafeNativeMethods.EvtGetPublisherMetadataProperty(pmHandle, thePropertyId, 0, bufferNeeded, buffer, out bufferNeeded);
  409. error = Marshal.GetLastWin32Error();
  410. if (!status)
  411. EventLogException.Throw(error);
  412. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.EvtVariant));
  413. return ConvertToObject(varVal);
  414. }
  415. finally {
  416. if (buffer != IntPtr.Zero) Marshal.FreeHGlobal(buffer);
  417. }
  418. }
  419. [System.Security.SecurityCritical]
  420. internal static EventLogHandle EvtGetPublisherMetadataPropertyHandle(EventLogHandle pmHandle, UnsafeNativeMethods.EvtPublisherMetadataPropertyId thePropertyId) {
  421. IntPtr buffer = IntPtr.Zero;
  422. try {
  423. int bufferNeeded;
  424. bool status = UnsafeNativeMethods.EvtGetPublisherMetadataProperty(pmHandle, thePropertyId, 0, 0, IntPtr.Zero, out bufferNeeded);
  425. int error = Marshal.GetLastWin32Error();
  426. if (!status) {
  427. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  428. EventLogException.Throw(error);
  429. }
  430. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  431. status = UnsafeNativeMethods.EvtGetPublisherMetadataProperty(pmHandle, thePropertyId, 0, bufferNeeded, buffer, out bufferNeeded);
  432. error = Marshal.GetLastWin32Error();
  433. if (!status)
  434. EventLogException.Throw(error);
  435. //
  436. // note: there is a case where returned variant does have allocated native resources
  437. // associated with (e.g. ConfigArrayHandle). If PtrToStructure throws, then we would
  438. // leak that resource - fortunately PtrToStructure only throws InvalidArgument which
  439. // is a logic error - not a possible runtime condition here. Other System exceptions
  440. // shouldn't be handled anyhow and the application will terminate.
  441. //
  442. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.EvtVariant));
  443. return ConvertToSafeHandle(varVal);
  444. }
  445. finally {
  446. if (buffer != IntPtr.Zero) Marshal.FreeHGlobal(buffer);
  447. }
  448. }
  449. // implies UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId flag.
  450. [System.Security.SecurityCritical]
  451. public static string EvtFormatMessage(EventLogHandle handle, uint msgId) {
  452. if (s_platformNotSupported)
  453. throw new System.PlatformNotSupportedException();
  454. int bufferNeeded;
  455. StringBuilder sb = new StringBuilder(null);
  456. bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, sb, out bufferNeeded);
  457. int error = Marshal.GetLastWin32Error();
  458. // ERROR_EVT_UNRESOLVED_VALUE_INSERT and its cousins are commonly returned for raw message text.
  459. if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
  460. && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT
  461. && error != UnsafeNativeMethods.ERROR_EVT_MAX_INSERTS_REACHED ) {
  462. switch (error) {
  463. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
  464. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
  465. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
  466. case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
  467. case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
  468. return null;
  469. }
  470. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  471. EventLogException.Throw(error);
  472. }
  473. sb.EnsureCapacity(bufferNeeded);
  474. status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, bufferNeeded, sb, out bufferNeeded);
  475. error = Marshal.GetLastWin32Error();
  476. if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
  477. && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_PARAMETER_INSERT
  478. && error != UnsafeNativeMethods.ERROR_EVT_MAX_INSERTS_REACHED ) {
  479. switch (error) {
  480. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
  481. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
  482. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
  483. case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
  484. case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
  485. return null;
  486. }
  487. if (error == UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT) {
  488. return null;
  489. }
  490. EventLogException.Throw(error);
  491. }
  492. return sb.ToString();
  493. }
  494. [System.Security.SecurityCritical]
  495. public static object EvtGetObjectArrayProperty(EventLogHandle objArrayHandle, int index, int thePropertyId) {
  496. IntPtr buffer = IntPtr.Zero;
  497. int bufferNeeded;
  498. try {
  499. bool status = UnsafeNativeMethods.EvtGetObjectArrayProperty(objArrayHandle, thePropertyId, index, 0, 0, IntPtr.Zero, out bufferNeeded);
  500. int error = Marshal.GetLastWin32Error();
  501. if (!status) {
  502. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  503. EventLogException.Throw(error);
  504. }
  505. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  506. status = UnsafeNativeMethods.EvtGetObjectArrayProperty(objArrayHandle, thePropertyId, index, 0, bufferNeeded, buffer, out bufferNeeded);
  507. error = Marshal.GetLastWin32Error();
  508. if (!status)
  509. EventLogException.Throw(error);
  510. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.EvtVariant));
  511. return ConvertToObject(varVal);
  512. }
  513. finally {
  514. if (buffer != IntPtr.Zero) Marshal.FreeHGlobal(buffer);
  515. }
  516. }
  517. [System.Security.SecurityCritical]
  518. public static object EvtGetEventMetadataProperty(EventLogHandle handle, UnsafeNativeMethods.EvtEventMetadataPropertyId enumType) {
  519. IntPtr buffer = IntPtr.Zero;
  520. int bufferNeeded;
  521. try {
  522. bool status = UnsafeNativeMethods.EvtGetEventMetadataProperty(handle, enumType, 0, 0, IntPtr.Zero, out bufferNeeded);
  523. int win32Error = Marshal.GetLastWin32Error();
  524. if (!status) {
  525. if (win32Error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  526. EventLogException.Throw(win32Error);
  527. }
  528. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  529. status = UnsafeNativeMethods.EvtGetEventMetadataProperty(handle, enumType, 0, bufferNeeded, buffer, out bufferNeeded);
  530. win32Error = Marshal.GetLastWin32Error();
  531. if (!status)
  532. EventLogException.Throw(win32Error);
  533. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.EvtVariant));
  534. return ConvertToObject(varVal);
  535. }
  536. finally {
  537. if (buffer != IntPtr.Zero)
  538. Marshal.FreeHGlobal(buffer);
  539. }
  540. }
  541. [System.Security.SecuritySafeCritical]
  542. public static object EvtGetChannelConfigProperty(EventLogHandle handle, UnsafeNativeMethods.EvtChannelConfigPropertyId enumType) {
  543. IntPtr buffer = IntPtr.Zero;
  544. int bufferNeeded;
  545. EventLogPermissionHolder.GetEventLogPermission().Demand();
  546. try {
  547. bool status = UnsafeNativeMethods.EvtGetChannelConfigProperty(handle, enumType, 0, 0, IntPtr.Zero, out bufferNeeded);
  548. int win32Error = Marshal.GetLastWin32Error();
  549. if (!status) {
  550. if (win32Error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  551. EventLogException.Throw(win32Error);
  552. }
  553. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  554. status = UnsafeNativeMethods.EvtGetChannelConfigProperty(handle, enumType, 0, bufferNeeded, buffer, out bufferNeeded);
  555. win32Error = Marshal.GetLastWin32Error();
  556. if (!status)
  557. EventLogException.Throw(win32Error);
  558. //
  559. // note: there is a case where returned variant does have allocated native resources
  560. // associated with (e.g. ConfigArrayHandle). If PtrToStructure throws, then we would
  561. // leak that resource - fortunately PtrToStructure only throws InvalidArgument which
  562. // is a logic error - not a possible runtime condition here. Other System exceptions
  563. // shouldn't be handled anyhow and the application will terminate.
  564. //
  565. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.EvtVariant));
  566. return ConvertToObject(varVal);
  567. }
  568. finally {
  569. if (buffer != IntPtr.Zero)
  570. Marshal.FreeHGlobal(buffer);
  571. }
  572. }
  573. [System.Security.SecuritySafeCritical]
  574. public static void EvtSetChannelConfigProperty(EventLogHandle handle, UnsafeNativeMethods.EvtChannelConfigPropertyId enumType, object val) {
  575. EventLogPermissionHolder.GetEventLogPermission().Demand();
  576. UnsafeNativeMethods.EvtVariant varVal = new UnsafeNativeMethods.EvtVariant();
  577. CoTaskMemSafeHandle taskMem = new CoTaskMemSafeHandle();
  578. using (taskMem) {
  579. if (val != null) {
  580. switch (enumType) {
  581. case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigEnabled: {
  582. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
  583. if ((bool)val == true) varVal.Bool = 1;
  584. else varVal.Bool = 0;
  585. }
  586. break;
  587. case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelConfigAccess: {
  588. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeString;
  589. taskMem.SetMemory(Marshal.StringToCoTaskMemAuto((string)val));
  590. varVal.StringVal = taskMem.GetMemory();
  591. }
  592. break;
  593. case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigLogFilePath: {
  594. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeString;
  595. taskMem.SetMemory(Marshal.StringToCoTaskMemAuto((string)val));
  596. varVal.StringVal = taskMem.GetMemory();
  597. }
  598. break;
  599. case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigMaxSize: {
  600. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt64;
  601. varVal.ULong = (ulong)((long)val);
  602. }
  603. break;
  604. case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigLevel: {
  605. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt32;
  606. varVal.UInteger = (uint)((int)val);
  607. }
  608. break;
  609. case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelPublishingConfigKeywords: {
  610. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt64;
  611. varVal.ULong = (ulong)((long)val);
  612. }
  613. break;
  614. case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigRetention: {
  615. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
  616. if ((bool)val == true) varVal.Bool = 1;
  617. else varVal.Bool = 0;
  618. }
  619. break;
  620. case UnsafeNativeMethods.EvtChannelConfigPropertyId.EvtChannelLoggingConfigAutoBackup: {
  621. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeBoolean;
  622. if ((bool)val == true) varVal.Bool = 1;
  623. else varVal.Bool = 0;
  624. }
  625. break;
  626. default:
  627. throw new InvalidOperationException();
  628. }
  629. }
  630. else {
  631. varVal.Type = (uint)UnsafeNativeMethods.EvtVariantType.EvtVarTypeNull;
  632. }
  633. bool status = UnsafeNativeMethods.EvtSetChannelConfigProperty(handle, enumType, 0, ref varVal);
  634. int win32Error = Marshal.GetLastWin32Error();
  635. if (!status)
  636. EventLogException.Throw(win32Error);
  637. }
  638. }
  639. [System.Security.SecurityCritical]
  640. public static string EvtNextChannelPath(EventLogHandle handle, ref bool finish) {
  641. StringBuilder sb = new StringBuilder(null);
  642. int channelNameNeeded;
  643. bool status = UnsafeNativeMethods.EvtNextChannelPath(handle, 0, sb, out channelNameNeeded);
  644. int win32Error = Marshal.GetLastWin32Error();
  645. if (!status) {
  646. if (win32Error == UnsafeNativeMethods.ERROR_NO_MORE_ITEMS) {
  647. finish = true;
  648. return null;
  649. }
  650. if (win32Error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  651. EventLogException.Throw(win32Error);
  652. }
  653. sb.EnsureCapacity(channelNameNeeded);
  654. status = UnsafeNativeMethods.EvtNextChannelPath(handle, channelNameNeeded, sb, out channelNameNeeded);
  655. win32Error = Marshal.GetLastWin32Error();
  656. if (!status)
  657. EventLogException.Throw(win32Error);
  658. return sb.ToString();
  659. }
  660. [System.Security.SecurityCritical]
  661. public static string EvtNextPublisherId(EventLogHandle handle, ref bool finish) {
  662. StringBuilder sb = new StringBuilder(null);
  663. int ProviderIdNeeded;
  664. bool status = UnsafeNativeMethods.EvtNextPublisherId(handle, 0, sb, out ProviderIdNeeded);
  665. int win32Error = Marshal.GetLastWin32Error();
  666. if (!status) {
  667. if (win32Error == UnsafeNativeMethods.ERROR_NO_MORE_ITEMS) {
  668. finish = true;
  669. return null;
  670. }
  671. if (win32Error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  672. EventLogException.Throw(win32Error);
  673. }
  674. sb.EnsureCapacity(ProviderIdNeeded);
  675. status = UnsafeNativeMethods.EvtNextPublisherId(handle, ProviderIdNeeded, sb, out ProviderIdNeeded);
  676. win32Error = Marshal.GetLastWin32Error();
  677. if (!status)
  678. EventLogException.Throw(win32Error);
  679. return sb.ToString();
  680. }
  681. [System.Security.SecurityCritical]
  682. public static object EvtGetLogInfo(EventLogHandle handle, UnsafeNativeMethods.EvtLogPropertyId enumType) {
  683. IntPtr buffer = IntPtr.Zero;
  684. int bufferNeeded;
  685. try {
  686. bool status = UnsafeNativeMethods.EvtGetLogInfo(handle, enumType, 0, IntPtr.Zero, out bufferNeeded);
  687. int win32Error = Marshal.GetLastWin32Error();
  688. if (!status) {
  689. if (win32Error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  690. EventLogException.Throw(win32Error);
  691. }
  692. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  693. status = UnsafeNativeMethods.EvtGetLogInfo(handle, enumType, bufferNeeded, buffer, out bufferNeeded);
  694. win32Error = Marshal.GetLastWin32Error();
  695. if (!status)
  696. EventLogException.Throw(win32Error);
  697. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(buffer, typeof(UnsafeNativeMethods.EvtVariant));
  698. return ConvertToObject(varVal);
  699. }
  700. finally {
  701. if (buffer != IntPtr.Zero)
  702. Marshal.FreeHGlobal(buffer);
  703. }
  704. }
  705. [System.Security.SecuritySafeCritical]
  706. public static void EvtRenderBufferWithContextSystem(EventLogHandle contextHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtRenderFlags flag, SystemProperties systemProperties, int SYSTEM_PROPERTY_COUNT) {
  707. IntPtr buffer = IntPtr.Zero;
  708. IntPtr pointer = IntPtr.Zero;
  709. int bufferNeeded;
  710. int propCount;
  711. EventLogPermissionHolder.GetEventLogPermission().Demand();
  712. try {
  713. bool status = UnsafeNativeMethods.EvtRender(contextHandle, eventHandle, flag, 0, IntPtr.Zero, out bufferNeeded, out propCount);
  714. if (!status) {
  715. int error = Marshal.GetLastWin32Error();
  716. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  717. EventLogException.Throw(error);
  718. }
  719. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  720. status = UnsafeNativeMethods.EvtRender(contextHandle, eventHandle, flag, bufferNeeded, buffer, out bufferNeeded, out propCount);
  721. int win32Error = Marshal.GetLastWin32Error();
  722. if (!status)
  723. EventLogException.Throw(win32Error);
  724. if (propCount != SYSTEM_PROPERTY_COUNT)
  725. throw new InvalidOperationException("We do not have " + SYSTEM_PROPERTY_COUNT + " variants given for the UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventValues flag. (System Properties)");
  726. pointer = buffer;
  727. //read each Variant structure
  728. for (int i = 0; i < propCount; i++) {
  729. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(pointer, typeof(UnsafeNativeMethods.EvtVariant));
  730. switch (i) {
  731. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemProviderName:
  732. systemProperties.ProviderName = (string)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeString);
  733. break;
  734. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemProviderGuid:
  735. systemProperties.ProviderId = (Guid?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeGuid);
  736. break;
  737. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemEventID:
  738. systemProperties.Id = (ushort?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt16);
  739. break;
  740. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemQualifiers:
  741. systemProperties.Qualifiers = (ushort?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt16);
  742. break;
  743. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemLevel:
  744. systemProperties.Level = (byte?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeByte);
  745. break;
  746. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemTask:
  747. systemProperties.Task = (ushort?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt16);
  748. break;
  749. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemOpcode:
  750. systemProperties.Opcode = (byte?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeByte);
  751. break;
  752. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemKeywords:
  753. systemProperties.Keywords = (ulong?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeHexInt64);
  754. break;
  755. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemTimeCreated:
  756. systemProperties.TimeCreated = (DateTime?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeFileTime);
  757. break;
  758. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemEventRecordId:
  759. systemProperties.RecordId = (ulong?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt64);
  760. break;
  761. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemActivityID:
  762. systemProperties.ActivityId = (Guid?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeGuid);
  763. break;
  764. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemRelatedActivityID:
  765. systemProperties.RelatedActivityId = (Guid?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeGuid);
  766. break;
  767. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemProcessID:
  768. systemProperties.ProcessId = (uint?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt32);
  769. break;
  770. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemThreadID:
  771. systemProperties.ThreadId = (uint?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeUInt32);
  772. break;
  773. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemChannel:
  774. systemProperties.ChannelName = (string)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeString);
  775. break;
  776. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemComputer:
  777. systemProperties.ComputerName = (string)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeString);
  778. break;
  779. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemUserID:
  780. systemProperties.UserId = (SecurityIdentifier)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeSid);
  781. break;
  782. case (int) UnsafeNativeMethods.EvtSystemPropertyId.EvtSystemVersion:
  783. systemProperties.Version = (byte?)ConvertToObject(varVal, UnsafeNativeMethods.EvtVariantType.EvtVarTypeByte);
  784. break;
  785. }
  786. pointer = new IntPtr(((Int64)pointer + Marshal.SizeOf(varVal)));
  787. }
  788. }
  789. finally {
  790. if (buffer != IntPtr.Zero)
  791. Marshal.FreeHGlobal(buffer);
  792. }
  793. }
  794. //EvtRenderContextFlags can be both: EvtRenderContextFlags.EvtRenderContextUser and EvtRenderContextFlags.EvtRenderContextValues
  795. //Render with Context = ContextUser or ContextValues (with user defined Xpath query strings)
  796. [System.Security.SecuritySafeCritical]
  797. public static IList<object> EvtRenderBufferWithContextUserOrValues(EventLogHandle contextHandle, EventLogHandle eventHandle) {
  798. IntPtr buffer = IntPtr.Zero;
  799. IntPtr pointer = IntPtr.Zero;
  800. int bufferNeeded;
  801. int propCount;
  802. UnsafeNativeMethods.EvtRenderFlags flag = UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventValues;
  803. EventLogPermissionHolder.GetEventLogPermission().Demand();
  804. try {
  805. bool status = UnsafeNativeMethods.EvtRender(contextHandle, eventHandle, flag, 0, IntPtr.Zero, out bufferNeeded, out propCount);
  806. if (!status) {
  807. int error = Marshal.GetLastWin32Error();
  808. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  809. EventLogException.Throw(error);
  810. }
  811. buffer = Marshal.AllocHGlobal((int)bufferNeeded);
  812. status = UnsafeNativeMethods.EvtRender(contextHandle, eventHandle, flag, bufferNeeded, buffer, out bufferNeeded, out propCount);
  813. int win32Error = Marshal.GetLastWin32Error();
  814. if (!status)
  815. EventLogException.Throw(win32Error);
  816. List<object> valuesList = new List<object>(propCount);
  817. if (propCount > 0) {
  818. pointer = buffer;
  819. for (int i = 0; i < propCount; i++) {
  820. UnsafeNativeMethods.EvtVariant varVal = (UnsafeNativeMethods.EvtVariant)Marshal.PtrToStructure(pointer, typeof(UnsafeNativeMethods.EvtVariant));
  821. valuesList.Add(ConvertToObject(varVal));
  822. pointer = new IntPtr(((Int64)pointer + Marshal.SizeOf(varVal)));
  823. }
  824. }
  825. return valuesList;
  826. }
  827. finally {
  828. if (buffer != IntPtr.Zero)
  829. Marshal.FreeHGlobal(buffer);
  830. }
  831. }
  832. [System.Security.SecuritySafeCritical]
  833. public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtFormatMessageFlags flag) {
  834. EventLogPermissionHolder.GetEventLogPermission().Demand();
  835. int bufferNeeded;
  836. StringBuilder sb = new StringBuilder(null);
  837. bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, sb, out bufferNeeded);
  838. int error = Marshal.GetLastWin32Error();
  839. if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT) {
  840. //
  841. // ERROR_EVT_UNRESOLVED_VALUE_INSERT can be returned. It means
  842. // message may have one or more unsubstitued strings. This is
  843. // not an exception, but we have no way to convey the partial
  844. // success out to enduser.
  845. //
  846. switch (error) {
  847. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
  848. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
  849. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
  850. case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
  851. case UnsafeNativeMethods.ERROR_MUI_FILE_NOT_FOUND:
  852. return null;
  853. }
  854. if (error != (int)UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  855. EventLogException.Throw(error);
  856. }
  857. sb.EnsureCapacity(bufferNeeded);
  858. status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, bufferNeeded, sb, out bufferNeeded);
  859. error = Marshal.GetLastWin32Error();
  860. if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT)
  861. {
  862. switch (error) {
  863. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_NOT_FOUND:
  864. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_ID_NOT_FOUND:
  865. case UnsafeNativeMethods.ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
  866. case UnsafeNativeMethods.ERROR_RESOURCE_LANG_NOT_FOUND:
  867. case UnsafeNativeMetho