Skip to content

Commit

Permalink
System.Management. Refactor DllImports (dotnet/corefx#34477)
Browse files Browse the repository at this point in the history
* System.Management. Refactor DllImports


Commit migrated from dotnet/corefx@2905380
  • Loading branch information
Marusyk authored and AaronRobinsonMSFT committed Jan 16, 2019
1 parent 1dbdaea commit 4bde620
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 41 deletions.
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;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

internal static partial class Interop
{
internal static partial class Ole32
{
[DllImport(Libraries.Ole32, PreserveSig = false)]
internal static extern IStream CreateStreamOnHGlobal(IntPtr hGlobal, bool fDeleteOnRelease);
}
}
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;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

internal static partial class Interop
{
internal static partial class Ole32
{
[DllImport(Libraries.Ole32, PreserveSig = false)]
internal static extern void CoMarshalInterface(
IStream pStm, // Pointer to the stream used for marshaling
[MarshalAs(UnmanagedType.LPStruct)] Guid riid, // Reference to the identifier of the
IntPtr pUnk, // Pointer to the interface to be marshaled
uint dwDestContext, // Destination process
IntPtr pvDestContext, // Reserved for future use
uint mshlflags // Reason for marshaling
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 System.Runtime.InteropServices.ComTypes;

internal static partial class Interop
{
internal static partial class Ole32
{
[DllImport(Libraries.Ole32, PreserveSig = false)]
internal static extern IntPtr CoUnmarshalInterface(
IStream pStm, // Pointer to the stream
[MarshalAs(UnmanagedType.LPStruct)] Guid riid // Reference to the identifier of the interface
);
}
}
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 static partial class Interop
{
internal static partial class Ole32
{
[DllImport(Libraries.Ole32)]
internal static extern int CoGetObjectContext([MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IntPtr ppv);
}
}
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;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

internal static partial class Interop
{
internal static partial class Ole32
{
[DllImport(Libraries.Ole32, PreserveSig = false)]
internal static extern IntPtr GetHGlobalFromStream(IStream pstm);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 Kernel32
{
[DllImport(Libraries.Kernel32, PreserveSig = true)]
internal static extern IntPtr GlobalLock(IntPtr hMem);

[DllImport(Libraries.Kernel32, PreserveSig = true)]
internal static extern int GlobalUnlock(IntPtr hMem);
}
}
20 changes: 19 additions & 1 deletion src/libraries/System.Management/src/System.Management.csproj
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>
<ProjectGuid>{950923D8-EC99-4F90-9BA6-8EB64582C555}</ProjectGuid>
<AssemblyName>System.Management</AssemblyName>
Expand All @@ -20,9 +20,27 @@
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.GetProcAddress.cs">
<Link>Common\Interop\Windows\kernel32\Interop.GetProcAddress.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.GlobalLock.cs">
<Link>Common\Interop\Windows\kernel32\Interop.GlobalLock.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\kernel32\Interop.LoadLibrary.cs">
<Link>Common\Interop\Windows\kernel32\Interop.LoadLibrary.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Ole32\Interop.CoGetObjectContext.cs">
<Link>Common\Interop\Windows\Ole32\Interop.CoGetObjectContext.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Ole32\Interop.CoMarshalInterface.cs">
<Link>Common\Interop\Windows\Ole32\Interop.CoMarshalInterface.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Ole32\Interop.CoUnmarshalInterface.cs">
<Link>Common\Interop\Windows\Ole32\Interop.CoUnmarshalInterface.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Ole32\Interop.CreateStreamOnHGlobal.cs">
<Link>Common\Interop\Windows\Ole32\Interop.CreateStreamOnHGlobal.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Ole32\Interop.GetHGlobalFromStream.cs">
<Link>Common\Interop\Windows\Ole32\Interop.GetHGlobalFromStream.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\CoreLib\Microsoft\Win32\SafeHandles\SafeLibraryHandle.cs">
<Link>Common\CoreLib\Microsoft\Win32\SafeHandles\SafeLibraryHandle.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ void DeserializeFromBlob(byte [] rg)

hGlobal = Marshal.AllocHGlobal(rg.Length);
Marshal.Copy(rg, 0, hGlobal, rg.Length);
stream = CreateStreamOnHGlobal(hGlobal, 0);
pWbemClassObject = CoUnmarshalInterface(stream, ref IID_IWbemClassObject);
stream = Interop.Ole32.CreateStreamOnHGlobal(hGlobal, false);
pWbemClassObject = Interop.Ole32.CoUnmarshalInterface(stream, IID_IWbemClassObject);
}
finally
{
Expand All @@ -108,20 +108,20 @@ byte[] SerializeToBlob()
try
{
// Stream will own the HGlobal
stream = CreateStreamOnHGlobal(IntPtr.Zero, 1);
stream = Interop.Ole32.CreateStreamOnHGlobal(IntPtr.Zero, true);

CoMarshalInterface(stream, ref IID_IWbemClassObject, pWbemClassObject, (uint)MSHCTX.MSHCTX_DIFFERENTMACHINE, IntPtr.Zero, (uint)MSHLFLAGS.MSHLFLAGS_TABLEWEAK);
Interop.Ole32.CoMarshalInterface(stream, IID_IWbemClassObject, pWbemClassObject, (uint)MSHCTX.MSHCTX_DIFFERENTMACHINE, IntPtr.Zero, (uint)MSHLFLAGS.MSHLFLAGS_TABLEWEAK);

System.Runtime.InteropServices.ComTypes.STATSTG statstg;
stream.Stat(out statstg, (int)STATFLAG.STATFLAG_DEFAULT);
rg = new byte[statstg.cbSize];
pData = GlobalLock(GetHGlobalFromStream(stream));
pData = Interop.Kernel32.GlobalLock(Interop.Ole32.GetHGlobalFromStream(stream));
Marshal.Copy(pData, rg, 0, (int)statstg.cbSize);
}
finally
{
if(pData != IntPtr.Zero)
GlobalUnlock(pData);
Interop.Kernel32.GlobalUnlock(pData);
if(stream != null)
Marshal.ReleaseComObject(stream);
}
Expand Down Expand Up @@ -421,34 +421,6 @@ enum MSHLFLAGS
MSHLFLAGS_TABLEWEAK = 2,
MSHLFLAGS_NOPING = 3
}

[DllImport("ole32.dll", PreserveSig=false)]
static extern System.Runtime.InteropServices.ComTypes.IStream CreateStreamOnHGlobal(IntPtr hGlobal, int fDeleteOnRelease);

[DllImport("ole32.dll", PreserveSig=false)]
static extern IntPtr GetHGlobalFromStream([In] System.Runtime.InteropServices.ComTypes.IStream pstm);

[DllImport("kernel32.dll", PreserveSig=true)]
static extern IntPtr GlobalLock([In] IntPtr hGlobal);

[DllImport("kernel32.dll", PreserveSig=true)]
static extern int GlobalUnlock([In] IntPtr pData);

[DllImport("ole32.dll", PreserveSig=false)]
static extern void CoMarshalInterface(
[In] System.Runtime.InteropServices.ComTypes.IStream pStm, //Pointer to the stream used for marshaling
[In] ref Guid riid, //Reference to the identifier of the
[In] IntPtr Unk, //Pointer to the interface to be marshaled
[In] uint dwDestContext, //Destination process
[In] IntPtr pvDestContext, //Reserved for future use
[In] uint mshlflags //Reason for marshaling
);

[DllImport("ole32.dll", PreserveSig=false)]
static extern IntPtr CoUnmarshalInterface(
[In] System.Runtime.InteropServices.ComTypes.IStream pStm, //Pointer to the stream
[In] ref Guid riid //Reference to the identifier of the interface
);
}

sealed class IWbemQualifierSetFreeThreaded : IDisposable
Expand Down Expand Up @@ -1911,11 +1883,6 @@ static void WorkerThread()
static Guid IID_IObjectContext = new Guid("51372AE0-CAE7-11CF-BE81-00AA00A2FA25");
static Guid IID_IComThreadingInfo = new Guid("000001ce-0000-0000-C000-000000000046");


// Import of CoGetObjectContext
[ DllImport("ole32.dll")]
static extern int CoGetObjectContext([In] ref Guid riid, [Out] out IntPtr pUnk);

// A variable that is initialized once to tell us if we are on
// a Win2k platform or above.
static bool CanCallCoGetObjectContext = IsWindows2000OrHigher();
Expand All @@ -1942,7 +1909,7 @@ public static bool IsNoContextMTA()
{
// If we CANNOT call CoGetObjectContext, assume we are not in the 'no context MTA' for safety
// (NOTE: This call is expected to always succeed)
if(0 != CoGetObjectContext(ref IID_IComThreadingInfo, out pComThreadingInfo))
if(0 != Interop.Ole32.CoGetObjectContext(IID_IComThreadingInfo, out pComThreadingInfo))
return false;

WmiNetUtilsHelper.APTTYPE aptType;
Expand Down

0 comments on commit 4bde620

Please sign in to comment.