Skip to content

Commit

Permalink
System.Diagnostics.EventLog . Refactor DllImports (dotnet/corefx#33835)
Browse files Browse the repository at this point in the history
* System.Diagnostics.EventLog . Refactor DllImports into Common\src\Interop

* Using SafeEventLogReadHandle/SafeEventLogWriteHandle instead of SafeHandle


Commit migrated from dotnet/corefx@547f7c4
  • Loading branch information
Marusyk authored and jkotas committed Dec 8, 2018
1 parent 9f5b15f commit 3e9a71b
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool ClearEventLog(SafeEventLogReadHandle hEventLog, string lpBackupFileName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, SetLastError = true)]
internal static extern bool CloseEventLog(IntPtr hEventLog);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, SetLastError = true)]
internal static extern bool DeregisterEventSource(IntPtr hEventLog);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetNumberOfEventLogRecords(SafeEventLogReadHandle hEventLog, out int NumberOfRecords);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetOldestEventLogRecord(SafeEventLogReadHandle hEventLog, out int OldestRecord);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool NotifyChangeEventLog(SafeEventLogReadHandle hEventLog, SafeWaitHandle hEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeEventLogReadHandle OpenEventLog(string lpUNCServerName, string lpSourceName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadEventLog(
SafeEventLogReadHandle hEventLog,
int dwReadFlags,
int dwRecordOffset,
byte[] lpBuffer,
int nNumberOfBytesRead,
out int pnBytesRead,
out int pnMinNumberOfBytesNeeded);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern SafeEventLogWriteHandle RegisterEventSource(string lpUNCServerName, string lpSourceName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal partial class Interop
{
internal partial class Advapi32
{
[DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool ReportEvent(
SafeEventLogWriteHandle hEventLog,
short wType,
ushort wcategory,
uint dwEventID,
byte[] lpUserSid,
short wNumStrings,
int dwDataSize,
IntPtr lpStrings,
byte[] lpRawData);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPartialFacadeAssembly Condition="'$(TargetsNetFx)' == 'true'">true</IsPartialFacadeAssembly>
Expand Down Expand Up @@ -29,19 +29,45 @@
<Compile Include="System\Diagnostics\SafeEventLogReadHandle.cs" />
<Compile Include="System\Diagnostics\SafeEventLogWriteHandle.cs" />
<Compile Include="System\Diagnostics\SharedUtils.cs" />
<Compile Include="System\Diagnostics\UnsafeNativeMethods.cs" />
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
<Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.CloseHandle.cs">
<Link>Common\CoreLib\Interop\Windows\Kernel32\Interop.CloseHandle.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\SafeHandles\SafeLibraryHandle.cs">
<Link>Common\CoreLib\Microsoft\Win32\SafeHandles\SafeLibraryHandle.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.ClearEventLog.cs">
<Link>Common\Interop\Windows\advapi32\Interop.ClearEventLog.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.CloseEventLog.cs">
<Link>Common\Interop\Windows\advapi32\Interop.CloseEventLog.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.DeregisterEventSource.cs">
<Link>Common\Interop\Windows\advapi32\Interop.DeregisterEventSource.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.GetNumberOfEventLogRecords.cs">
<Link>Common\Interop\Windows\advapi32\Interop.GetNumberOfEventLogRecords.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.GetOldestEventLogRecord.cs">
<Link>Common\Interop\Windows\advapi32\Interop.GetOldestEventLogRecord.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.LookupAccountSid.cs">
<Link>Common\Interop\Windows\Advapi32\Interop.LookupAccountSid.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.NotifyChangeEventLog.cs">
<Link>Common\Interop\Windows\advapi32\Interop.NotifyChangeEventLog.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.OpenEventLog.cs">
<Link>Common\Interop\Windows\advapi32\Interop.OpenEventLog.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.ReadEventLog.cs">
<Link>Common\Interop\Windows\advapi32\Interop.ReadEventLog.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.RegisterEventSource.cs">
<Link>Common\Interop\Windows\advapi32\Interop.RegisterEventSource.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\advapi32\Interop.ReportEvent.cs">
<Link>Common\Interop\Windows\advapi32\Interop.ReportEvent.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.FormatMessage_SafeLibraryHandle.cs">
<Link>Common\Interop\Windows\kernel32\Interop.FormatMessage_SafeLibraryHandle.cs</Link>
</Compile>
Expand All @@ -54,6 +80,9 @@
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.WaitForSingleObject.cs">
<Link>Common\Interop\Windows\kernel32\Interop.WaitForSingleObject.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
<Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsNetFx)' == 'true'">
<Reference Include="mscorlib" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ internal int EntryCount
if (!IsOpenForRead)
OpenForRead(this.machineName);
int count;
bool success = UnsafeNativeMethods.GetNumberOfEventLogRecords(readHandle, out count);
bool success = Interop.Advapi32.GetNumberOfEventLogRecords(readHandle, out count);
if (!success)
throw SharedUtils.CreateSafeWin32Exception();
return count;
Expand Down Expand Up @@ -364,7 +364,7 @@ private int OldestEntryNumber
if (!IsOpenForRead)
OpenForRead(this.machineName);
int num;
bool success = UnsafeNativeMethods.GetOldestEventLogRecord(readHandle, out num);
bool success = Interop.Advapi32.GetOldestEventLogRecord(readHandle, out num);
if (!success)
throw SharedUtils.CreateSafeWin32Exception();

Expand Down Expand Up @@ -439,7 +439,7 @@ private static void AddListenerComponent(EventLogInternal component, string comp
info.handleOwner = new EventLogInternal(compLogName, compMachineName);
// tell the event log system about it
info.waitHandle = new AutoResetEvent(false);
bool success = UnsafeNativeMethods.NotifyChangeEventLog(info.handleOwner.ReadHandle, info.waitHandle.SafeWaitHandle);
bool success = Interop.Advapi32.NotifyChangeEventLog(info.handleOwner.ReadHandle, info.waitHandle.SafeWaitHandle);
if (!success)
throw new InvalidOperationException(SR.CantMonitorEventLog, SharedUtils.CreateSafeWin32Exception());

Expand Down Expand Up @@ -479,7 +479,7 @@ public void Clear()

if (!IsOpenForRead)
OpenForRead(currentMachineName);
bool success = UnsafeNativeMethods.ClearEventLog(readHandle, NativeMethods.NullHandleRef);
bool success = Interop.Advapi32.ClearEventLog(readHandle, null);
if (!success)
{
// Ignore file not found errors. ClearEventLog seems to try to delete the file where the event log is
Expand Down Expand Up @@ -748,7 +748,7 @@ internal EventLogEntry[] GetAllEntries()
while (idx < entries.Length)
{
byte[] buf = new byte[BUF_SIZE];
bool success = UnsafeNativeMethods.ReadEventLog(readHandle, NativeMethods.FORWARDS_READ | NativeMethods.SEEK_READ,
bool success = Interop.Advapi32.ReadEventLog(readHandle, NativeMethods.FORWARDS_READ | NativeMethods.SEEK_READ,
oldestEntry + idx, buf, buf.Length, out bytesRead, out minBytesNeeded);
if (!success)
{
Expand All @@ -767,7 +767,7 @@ internal EventLogEntry[] GetAllEntries()
Debug.WriteLineIf(CompModSwitches.EventLog.TraceVerbose, "Increasing buffer size from " + buf.Length.ToString(CultureInfo.InvariantCulture) + " to " + minBytesNeeded.ToString(CultureInfo.InvariantCulture) + " bytes");
buf = new byte[minBytesNeeded];
}
success = UnsafeNativeMethods.ReadEventLog(readHandle, NativeMethods.FORWARDS_READ | NativeMethods.SEEK_READ,
success = Interop.Advapi32.ReadEventLog(readHandle, NativeMethods.FORWARDS_READ | NativeMethods.SEEK_READ,
oldestEntry + idx, buf, buf.Length, out bytesRead, out minBytesNeeded);
if (!success)
break;
Expand Down Expand Up @@ -925,7 +925,7 @@ private EventLogEntry GetEntryWithOldest(int index)
cache = new byte[BUF_SIZE];
int bytesRead;
int minBytesNeeded;
bool success = UnsafeNativeMethods.ReadEventLog(readHandle, flags, index,
bool success = Interop.Advapi32.ReadEventLog(readHandle, flags, index,
cache, cache.Length, out bytesRead, out minBytesNeeded);
if (!success)
{
Expand All @@ -947,7 +947,7 @@ private EventLogEntry GetEntryWithOldest(int index)
cache = new byte[minBytesNeeded];
}
}
success = UnsafeNativeMethods.ReadEventLog(readHandle, NativeMethods.FORWARDS_READ | NativeMethods.SEEK_READ, index,
success = Interop.Advapi32.ReadEventLog(readHandle, NativeMethods.FORWARDS_READ | NativeMethods.SEEK_READ, index,
cache, cache.Length, out bytesRead, out minBytesNeeded);
}

Expand Down Expand Up @@ -1101,7 +1101,7 @@ private void OpenForRead(string currentMachineName)
bytesCached = 0;
firstCachedEntry = -1;

SafeEventLogReadHandle handle = SafeEventLogReadHandle.OpenEventLog(currentMachineName, logname);
SafeEventLogReadHandle handle = Interop.Advapi32.OpenEventLog(currentMachineName, logname);
if (handle.IsInvalid)
{
Win32Exception e = null;
Expand All @@ -1125,7 +1125,7 @@ private void OpenForWrite(string currentMachineName)
if (sourceName == null || sourceName.Length == 0)
throw new ArgumentException(SR.NeedSourceToOpen);

SafeEventLogWriteHandle handle = SafeEventLogWriteHandle.RegisterEventSource(currentMachineName, sourceName);
SafeEventLogWriteHandle handle = Interop.Advapi32.RegisterEventSource(currentMachineName, sourceName);
if (handle.IsInvalid)
{
Win32Exception e = null;
Expand Down Expand Up @@ -1435,8 +1435,8 @@ private void InternalWriteEvent(uint eventID, ushort category, EventLogEntryType

byte[] sid = null;
// actually report the event
bool success = UnsafeNativeMethods.ReportEvent(writeHandle, (short)type, category, eventID,
sid, (short)strings.Length, rawData.Length, new HandleRef(this, stringsRootHandle.AddrOfPinnedObject()), rawData);
bool success = Interop.Advapi32.ReportEvent(writeHandle, (short)type, category, eventID,
sid, (short)strings.Length, rawData.Length, stringsRootHandle.AddrOfPinnedObject(), rawData);
if (!success)
{
// Trace("WriteEvent", "Throwing Win32Exception");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

namespace Microsoft.Win32.SafeHandles
{
internal sealed class SafeEventLogReadHandle : SafeHandleZeroOrMinusOneIsInvalid
{
internal SafeEventLogReadHandle() : base(true) { }

[DllImport(Interop.Libraries.Advapi32, CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
internal static extern SafeEventLogReadHandle OpenEventLog(string UNCServerName, string sourceName);

[DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
private static extern bool CloseEventLog(IntPtr hEventLog);

protected override bool ReleaseHandle()
{
return CloseEventLog(handle);
return Interop.Advapi32.CloseEventLog(handle);
}
}
}
}
Loading

0 comments on commit 3e9a71b

Please sign in to comment.