Skip to content

Commit

Permalink
Add new Marshal.InitHandle API.
Browse files Browse the repository at this point in the history
Fix SetHandle accessibility

Fix test.

Use Marshal.InitHandle.
  • Loading branch information
jkoritzinsky committed Jan 19, 2021
1 parent 923f6f5 commit 7b99f90
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,5 @@ public SafeProcessHandle(IntPtr existingHandle, bool ownsHandle)
{
SetHandle(existingHandle);
}

internal void InitialSetHandle(IntPtr h)
{
Debug.Assert(IsInvalid, "Safe handle should only be set once");
base.handle = h;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ public SafeThreadHandle()
{
}

internal void InitialSetHandle(IntPtr h)
{
Debug.Assert(IsInvalid, "Safe handle should only be set once");
base.SetHandle(h);
}

public override bool IsInvalid
{
get { return handle == IntPtr.Zero || handle == new IntPtr(-1); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ ref processInfo // pointer to PROCESS_INFORMATION
}

if (processInfo.hProcess != IntPtr.Zero && processInfo.hProcess != new IntPtr(-1))
procSH.InitialSetHandle(processInfo.hProcess);
Marshal.InitHandle(procSH, processInfo.hProcess);
if (processInfo.hThread != IntPtr.Zero && processInfo.hThread != new IntPtr(-1))
Interop.Kernel32.CloseHandle(processInfo.hThread);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,5 +996,12 @@ internal static unsafe uint SysStringByteLen(IntPtr s)

[SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID(Guid clsid) => GetTypeFromCLSID(clsid, null, throwOnError: false);

/// <summary>
/// Initializes the underlying handle of a newly created <see cref="SafeHandle" /> to the provided value.
/// </summary>
/// <param name="safeHandle"><see cref="SafeHandle"/> instance to update</param>
/// <param name="handle">Pre-existing handle</param>
public static void InitHandle(SafeHandle safeHandle, IntPtr handle) => safeHandle.SetHandle(handle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected SafeHandle(IntPtr invalidHandleValue, bool ownsHandle)
}
#endif

protected void SetHandle(IntPtr handle) => this.handle = handle;
protected internal void SetHandle(IntPtr handle) => this.handle = handle;

public IntPtr DangerousGetHandle() => handle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ public static void GetNativeVariantForObject<T>(T? obj, System.IntPtr pDstNative
public static string GetTypeInfoName(System.Runtime.InteropServices.ComTypes.ITypeInfo typeInfo) { throw null; }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public static object GetUniqueObjectForIUnknown(System.IntPtr unknown) { throw null; }
public static void InitHandle(SafeHandle safeHandle, IntPtr handle) { }
public static bool IsComObject(object o) { throw null; }
public static bool IsTypeVisibleFromCom(System.Type t) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="System\Runtime\InteropServices\Marshal\GetTypeInfoNameTests.cs" />
<Compile Include="System\Runtime\InteropServices\Marshal\GetUniqueObjectForIUnknownTests.cs" />
<Compile Include="System\Runtime\InteropServices\Marshal\GetUniqueObjectForIUnknownTests.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
<Compile Include="System\Runtime\InteropServices\Marshal\InitHandleTests.cs" />
<Compile Include="System\Runtime\InteropServices\Marshal\IsComObjectTests.cs" />
<Compile Include="System\Runtime\InteropServices\Marshal\IsComObjectTests.Windows.cs" Condition="'$(TargetsWindows)' == 'true'" />
<Compile Include="System\Runtime\InteropServices\Marshal\ReleaseTests.cs" />
Expand Down Expand Up @@ -176,4 +177,4 @@
<Compile Include="System\Runtime\InteropServices\TypeLibVarAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\TypeLibVersionAttributeTests.cs" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.Runtime.InteropServices.Tests
{
public class InitHandleTests
{
[Fact]
public void InitHandle_SetsHandle()
{
var safeHandle = new TestSafeHandle();
nint underlyingHandle = 42;

Marshal.InitHandle(safeHandle, underlyingHandle);

Assert.Equal((IntPtr)underlyingHandle, safeHandle.DangerousGetHandle());
}

class TestSafeHandle : SafeHandle
{
public TestSafeHandle() : base(IntPtr.Zero, true) { }

public override bool IsInvalid => handle == IntPtr.Zero;

protected override bool ReleaseHandle() => true;
}
}
}

0 comments on commit 7b99f90

Please sign in to comment.