Skip to content

Commit

Permalink
Workaround bug in EvtFormatMessage (#105636)
Browse files Browse the repository at this point in the history
* Workaround bug in EvtFormatMessage

* Add issue comment for 100198

* Use stackalloc buffer
  • Loading branch information
ericstj authored Jul 30, 2024
1 parent 852420c commit ee3b1f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ internal static EventLogHandle EvtGetPublisherMetadataPropertyHandle(EventLogHan
public static string EvtFormatMessage(EventLogHandle handle, uint msgId)
{
int bufferNeeded;
bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, null, out bufferNeeded);
Span<char> emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198
bool status = UnsafeNativeMethods.EvtFormatMessage(handle, EventLogHandle.Zero, msgId, 0, null, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageId, 0, emptyBuffer, out bufferNeeded);
int error = Marshal.GetLastWin32Error();

// ERROR_EVT_UNRESOLVED_VALUE_INSERT and its cousins are commonly returned for raw message text.
Expand Down Expand Up @@ -933,7 +934,8 @@ public static IList<object> EvtRenderBufferWithContextUserOrValues(EventLogHandl
public static string EvtFormatMessageRenderName(EventLogHandle pmHandle, EventLogHandle eventHandle, UnsafeNativeMethods.EvtFormatMessageFlags flag)
{
int bufferNeeded;
bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, null, out bufferNeeded);
Span<char> emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198
bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, emptyBuffer, out bufferNeeded);
int error = Marshal.GetLastWin32Error();

if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
Expand Down Expand Up @@ -985,11 +987,12 @@ public static IEnumerable<string> EvtFormatMessageRenderKeywords(EventLogHandle
{
IntPtr buffer = IntPtr.Zero;
int bufferNeeded;
Span<char> emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198

try
{
List<string> keywordsList = new List<string>();
bool status = UnsafeNativeMethods.EvtFormatMessageBuffer(pmHandle, eventHandle, 0, 0, IntPtr.Zero, flag, 0, IntPtr.Zero, out bufferNeeded);
bool status = UnsafeNativeMethods.EvtFormatMessage(pmHandle, eventHandle, 0, 0, null, flag, 0, emptyBuffer, out bufferNeeded);
int error = Marshal.GetLastWin32Error();

if (!status)
Expand Down Expand Up @@ -1071,6 +1074,7 @@ public static string EvtRenderBookmark(EventLogHandle eventHandle)
public static string EvtFormatMessageFormatDescription(EventLogHandle handle, EventLogHandle eventHandle, string[] values)
{
int bufferNeeded;
Span<char> emptyBuffer = [ '\0' ]; // issue: https://github.com/dotnet/runtime/issues/100198

UnsafeNativeMethods.EvtStringVariant[] stringVariants = new UnsafeNativeMethods.EvtStringVariant[values.Length];
for (int i = 0; i < values.Length; i++)
Expand All @@ -1079,7 +1083,7 @@ public static string EvtFormatMessageFormatDescription(EventLogHandle handle, Ev
stringVariants[i].StringVal = values[i];
}

bool status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, 0, null, out bufferNeeded);
bool status = UnsafeNativeMethods.EvtFormatMessage(handle, eventHandle, 0xffffffff, values.Length, stringVariants, UnsafeNativeMethods.EvtFormatMessageFlags.EvtFormatMessageEvent, 0, emptyBuffer, out bufferNeeded);
int error = Marshal.GetLastWin32Error();

if (!status && error != UnsafeNativeMethods.ERROR_EVT_UNRESOLVED_VALUE_INSERT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ internal static partial bool EvtFormatMessage(
EvtStringVariant[] values,
EvtFormatMessageFlags flags,
int bufferSize,
[Out] char[]? buffer,
Span<char> buffer,
out int bufferUsed);

[LibraryImport(Interop.Libraries.Wevtapi, EntryPoint = "EvtFormatMessage", SetLastError = true)]
Expand Down

0 comments on commit ee3b1f0

Please sign in to comment.