Skip to content

Commit

Permalink
Use GeneratedDllImport in System.Diagnostics.EventLog (#61990)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung authored Nov 24, 2021
1 parent 7cfcbb0 commit 97f7e68
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool ClearEventLog(SafeEventLogReadHandle hEventLog, string lpBackupFileName);
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static partial bool ClearEventLog(SafeEventLogReadHandle hEventLog, string lpBackupFileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, SetLastError = true)]
internal static extern bool CloseEventLog(IntPtr hEventLog);
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
internal static partial bool CloseEventLog(IntPtr hEventLog);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, SetLastError = true)]
internal static extern bool DeregisterEventSource(IntPtr hEventLog);
[GeneratedDllImport(Libraries.Advapi32, SetLastError = true)]
internal static partial bool DeregisterEventSource(IntPtr hEventLog);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetNumberOfEventLogRecords(SafeEventLogReadHandle hEventLog, out int NumberOfRecords);
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static partial bool GetNumberOfEventLogRecords(SafeEventLogReadHandle hEventLog, out int NumberOfRecords);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetOldestEventLogRecord(SafeEventLogReadHandle hEventLog, out int OldestRecord);
public static partial bool GetOldestEventLogRecord(SafeEventLogReadHandle hEventLog, out int OldestRecord);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool NotifyChangeEventLog(SafeEventLogReadHandle hEventLog, SafeWaitHandle hEvent);
public static partial bool NotifyChangeEventLog(SafeEventLogReadHandle hEventLog, SafeWaitHandle hEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeEventLogReadHandle OpenEventLog(string lpUNCServerName, string lpSourceName);
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial SafeEventLogReadHandle OpenEventLog(string lpUNCServerName, string lpSourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ internal static partial class Advapi32
internal const int FORWARDS_READ = 0x4;
internal const int BACKWARDS_READ = 0x8;

[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadEventLog(
public static partial bool ReadEventLog(
SafeEventLogReadHandle hEventLog,
int dwReadFlags,
int dwRecordOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeEventLogWriteHandle RegisterEventSource(string lpUNCServerName, string lpSourceName);
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial SafeEventLogWriteHandle RegisterEventSource(string lpUNCServerName, string lpSourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal static partial class Interop
{
internal static partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool ReportEvent(
[GeneratedDllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static partial bool ReportEvent(
SafeEventLogWriteHandle hEventLog,
short wType,
ushort wcategory,
Expand Down
1 change: 1 addition & 0 deletions src/libraries/Common/src/Interop/Windows/Interop.Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ internal static partial class Errors
internal const int ERROR_TIMEOUT = 0x5B4;
internal const int ERROR_EVENTLOG_FILE_CHANGED = 0x5DF;
internal const int ERROR_TRUSTED_RELATIONSHIP_FAILURE = 0x6FD;
internal const int ERROR_RESOURCE_TYPE_NOT_FOUND = 0x715;
internal const int ERROR_RESOURCE_LANG_NOT_FOUND = 0x717;
internal const int ERROR_NOT_A_REPARSE_POINT = 0x1126;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal static partial class Libraries
internal const string User32 = "user32.dll";
internal const string Version = "version.dll";
internal const string WebSocket = "websocket.dll";
internal const string Wevtapi = "wevtapi.dll";
internal const string WinHttp = "winhttp.dll";
internal const string WinMM = "winmm.dll";
internal const string Wkscli = "wkscli.dll";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal static partial class Kernel32
public const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;
public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;

[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, BestFitMapping = true)]
public static extern int FormatMessage(
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)]
public static partial int FormatMessage(
int dwFlags,
SafeLibraryHandle lpSource,
uint dwMessageId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static partial class Kernel32
public const int LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
public const int LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800;

[DllImport(Libraries.Kernel32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeLibraryHandle LoadLibraryExW([In] string lpwLibFileName, [In] IntPtr hFile, [In] uint dwFlags);
[GeneratedDllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
public static partial SafeLibraryHandle LoadLibraryExW(string lpwLibFileName, IntPtr hFile, uint dwFlags);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<NoWarn>$(NoWarn);CA1838;CA1847</NoWarn>
<NoWarn>$(NoWarn);CA1847</NoWarn>
<!-- Suppressions to avoid ifdefs:
CA1845: Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring'
CA1846: Prefer 'AsSpan' over 'Substring' when span-based overloads are available -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,8 @@ public IList<object> GetPropertyValues(EventLogPropertySelector propertySelector

public override string ToXml()
{
StringBuilder renderBuffer = new StringBuilder(2000);
NativeWrapper.EvtRender(EventLogHandle.Zero, Handle, UnsafeNativeMethods.EvtRenderFlags.EvtRenderEventXml, renderBuffer);
return renderBuffer.ToString();
char[] renderBuffer = GC.AllocateUninitializedArray<char>(2000);
return NativeWrapper.EvtRenderXml(EventLogHandle.Zero, Handle, renderBuffer);
}

protected override void Dispose(bool disposing)
Expand Down
Loading

0 comments on commit 97f7e68

Please sign in to comment.