Skip to content

Commit 97f7e68

Browse files
authored
Use GeneratedDllImport in System.Diagnostics.EventLog (#61990)
1 parent 7cfcbb0 commit 97f7e68

18 files changed

+241
-312
lines changed

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ClearEventLog.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
12-
public static extern bool ClearEventLog(SafeEventLogReadHandle hEventLog, string lpBackupFileName);
11+
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
12+
public static partial bool ClearEventLog(SafeEventLogReadHandle hEventLog, string lpBackupFileName);
1313
}
1414
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.CloseEventLog.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Libraries.Advapi32, SetLastError = true)]
12-
internal static extern bool CloseEventLog(IntPtr hEventLog);
11+
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
12+
internal static partial bool CloseEventLog(IntPtr hEventLog);
1313
}
1414
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.DeregisterEventSource.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Libraries.Advapi32, SetLastError = true)]
12-
internal static extern bool DeregisterEventSource(IntPtr hEventLog);
11+
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
12+
internal static partial bool DeregisterEventSource(IntPtr hEventLog);
1313
}
1414
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.GetNumberOfEventLogRecords.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
12-
public static extern bool GetNumberOfEventLogRecords(SafeEventLogReadHandle hEventLog, out int NumberOfRecords);
11+
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
12+
public static partial bool GetNumberOfEventLogRecords(SafeEventLogReadHandle hEventLog, out int NumberOfRecords);
1313
}
1414
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.GetOldestEventLogRecord.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
11+
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
1212
[return: MarshalAs(UnmanagedType.Bool)]
13-
public static extern bool GetOldestEventLogRecord(SafeEventLogReadHandle hEventLog, out int OldestRecord);
13+
public static partial bool GetOldestEventLogRecord(SafeEventLogReadHandle hEventLog, out int OldestRecord);
1414
}
1515
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.NotifyChangeEventLog.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
11+
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
1212
[return: MarshalAs(UnmanagedType.Bool)]
13-
public static extern bool NotifyChangeEventLog(SafeEventLogReadHandle hEventLog, SafeWaitHandle hEvent);
13+
public static partial bool NotifyChangeEventLog(SafeEventLogReadHandle hEventLog, SafeWaitHandle hEvent);
1414
}
1515
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.OpenEventLog.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
12-
internal static extern SafeEventLogReadHandle OpenEventLog(string lpUNCServerName, string lpSourceName);
11+
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
12+
internal static partial SafeEventLogReadHandle OpenEventLog(string lpUNCServerName, string lpSourceName);
1313
}
1414
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ReadEventLog.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ internal static partial class Advapi32
1212
internal const int FORWARDS_READ = 0x4;
1313
internal const int BACKWARDS_READ = 0x8;
1414

15-
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
15+
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
1616
[return: MarshalAs(UnmanagedType.Bool)]
17-
public static extern bool ReadEventLog(
17+
public static partial bool ReadEventLog(
1818
SafeEventLogReadHandle hEventLog,
1919
int dwReadFlags,
2020
int dwRecordOffset,

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.RegisterEventSource.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal static partial class Interop
88
{
99
internal static partial class Advapi32
1010
{
11-
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
12-
internal static extern SafeEventLogWriteHandle RegisterEventSource(string lpUNCServerName, string lpSourceName);
11+
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
12+
internal static partial SafeEventLogWriteHandle RegisterEventSource(string lpUNCServerName, string lpSourceName);
1313
}
1414
}

src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ReportEvent.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ internal static partial class Interop
99
{
1010
internal static partial class Advapi32
1111
{
12-
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
13-
public static extern bool ReportEvent(
12+
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
13+
public static partial bool ReportEvent(
1414
SafeEventLogWriteHandle hEventLog,
1515
short wType,
1616
ushort wcategory,

src/libraries/Common/src/Interop/Windows/Interop.Errors.cs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ internal static partial class Errors
9090
internal const int ERROR_TIMEOUT = 0x5B4;
9191
internal const int ERROR_EVENTLOG_FILE_CHANGED = 0x5DF;
9292
internal const int ERROR_TRUSTED_RELATIONSHIP_FAILURE = 0x6FD;
93+
internal const int ERROR_RESOURCE_TYPE_NOT_FOUND = 0x715;
9394
internal const int ERROR_RESOURCE_LANG_NOT_FOUND = 0x717;
9495
internal const int ERROR_NOT_A_REPARSE_POINT = 0x1126;
9596
}

src/libraries/Common/src/Interop/Windows/Interop.Libraries.cs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ internal static partial class Libraries
3434
internal const string User32 = "user32.dll";
3535
internal const string Version = "version.dll";
3636
internal const string WebSocket = "websocket.dll";
37+
internal const string Wevtapi = "wevtapi.dll";
3738
internal const string WinHttp = "winhttp.dll";
3839
internal const string WinMM = "winmm.dll";
3940
internal const string Wkscli = "wkscli.dll";

src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ internal static partial class Kernel32
1313
public const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;
1414
public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
1515

16-
[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = true)]
17-
public static extern int FormatMessage(
16+
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
17+
public static partial int FormatMessage(
1818
int dwFlags,
1919
SafeLibraryHandle lpSource,
2020
uint dwMessageId,

src/libraries/Common/src/Interop/Windows/Kernel32/Interop.LoadLibraryEx.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static partial class Kernel32
1313
public const int LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
1414
public const int LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800;
1515

16-
[DllImport(Libraries.Kernel32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
17-
public static extern SafeLibraryHandle LoadLibraryExW([In] string lpwLibFileName, [In] IntPtr hFile, [In] uint dwFlags);
16+
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
17+
public static partial SafeLibraryHandle LoadLibraryExW(string lpwLibFileName, IntPtr hFile, uint dwFlags);
1818
}
1919
}

src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
44
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
5-
<NoWarn>$(NoWarn);CA1838;CA1847</NoWarn>
5+
<NoWarn>$(NoWarn);CA1847</NoWarn>
66
<!-- Suppressions to avoid ifdefs:
77
CA1845: Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring'
88
CA1846: Prefer 'AsSpan' over 'Substring' when span-based overloads are available -->

src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogRecord.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,8 @@ public IList<object> GetPropertyValues(EventLogPropertySelector propertySelector
398398

399399
public override string ToXml()
400400
{
401-
StringBuilder renderBuffer = new StringBuilder(2000);
402-
NativeWrapper.EvtRender(EventLogHandle.Zero, Handle, UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventXml, renderBuffer);
403-
return renderBuffer.ToString();
401+
char[] renderBuffer = GC.AllocateUninitializedArray<char>(2000);
402+
return NativeWrapper.EvtRenderXml(EventLogHandle.Zero, Handle, renderBuffer);
404403
}
405404

406405
protected override void Dispose(bool disposing)

0 commit comments

Comments
 (0)