Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
using MS.Internal;
using System.Collections.Specialized;
using System.IO;
using System.Security;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

using SR = MS.Internal.PresentationCore.SR;
using IComDataObject = System.Runtime.InteropServices.ComTypes.IDataObject;
Expand Down Expand Up @@ -530,62 +527,6 @@ internal static bool IsClipboardPopulated()

#region Private Methods

/// <summary>
/// Calls IsDynamicCodePolicyEnabled to determine if DeviceGuard is enabled, then caches it so subsequent calls only return the cached value.
/// </summary>
private static bool IsDeviceGuardEnabled
{
get
{
if (_isDeviceGuardEnabled < 0) return false;
if (_isDeviceGuardEnabled > 0) return true;

bool isDynamicCodePolicyEnabled = IsDynamicCodePolicyEnabled();
_isDeviceGuardEnabled = isDynamicCodePolicyEnabled ? 1 : -1;

return isDynamicCodePolicyEnabled;
}
}

/// <summary>
/// Loads Wldp.dll and looks for WldpIsDynamicCodePolicyEnabled to determine whether DeviceGuard is enabled.
/// </summary>
private static bool IsDynamicCodePolicyEnabled()
{
bool isEnabled = false;

IntPtr hModule = IntPtr.Zero;
try
{
hModule = LoadLibraryHelper.SecureLoadLibraryEx(ExternDll.Wldp, IntPtr.Zero, UnsafeNativeMethods.LoadLibraryFlags.LOAD_LIBRARY_SEARCH_SYSTEM32);
if (hModule != IntPtr.Zero)
{
IntPtr entryPoint = UnsafeNativeMethods.GetProcAddressNoThrow(new HandleRef(null, hModule), "WldpIsDynamicCodePolicyEnabled");
if (entryPoint != IntPtr.Zero)
{
int hResult = UnsafeNativeMethods.WldpIsDynamicCodePolicyEnabled(out isEnabled);

if (hResult != NativeMethods.S_OK)
{
isEnabled = false;
}
}
}
}
catch
{
}
finally
{
if (hModule != IntPtr.Zero)
{
UnsafeNativeMethods.FreeLibrary(hModule);
}
}

return isEnabled;
}

private static IDataObject GetDataObjectInternal()
{
IDataObject dataObject;
Expand Down Expand Up @@ -769,7 +710,6 @@ private static bool IsDataFormatAutoConvert(string format)

#endregion Private Constants

private static int _isDeviceGuardEnabled = 0;
}

#endregion Clipboard class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ internal static class ExternDll
public const string Wininet = "wininet.dll";
public const string Winmm = "winmm.dll";
public const string Winspool = "winspool.drv";
public const string Wldp = "wldp.dll";
public const string WpfGfx = "WpfGfx_cor3.dll";
public const string WtsApi32 = "wtsapi32.dll";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3150,18 +3150,6 @@ public static extern uint GetRawInputDeviceInfo(
/// </remarks>
[DllImport(ExternDll.User32, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool EnumDisplayMonitors(
IntPtr hdc,
IntPtr lprcClip,
NativeMethods.MonitorEnumProc lpfnEnum,
IntPtr lParam);

/// <summary>
/// Retrieves a value that describes the Device Guard policy enforcement status for .NET dynamic code.
/// </summary>
/// <param name="enabled">On success, returns true if the Device Guard policy enforces .NET Dynamic Code policy; otherwise, returns false.</param>
/// <returns>This method returns S_OK if successful or a failure code otherwise.</returns>
[DllImport(ExternDll.Wldp, CallingConvention = CallingConvention.Winapi, ExactSpelling = true)]
internal static extern int WldpIsDynamicCodePolicyEnabled([Out] out bool enabled);
internal static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, NativeMethods.MonitorEnumProc lpfnEnum, IntPtr lParam);
}
}