未验证 提交 cff5854f 编写于 作者: E Elinor Fung 提交者: GitHub

Make some p/invokes blittable (#54370)

上级 cdf65116
......@@ -68,23 +68,25 @@ internal static int BioTell(SafeBioHandle bio)
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PushX509StackField(SafeSharedX509StackHandle stack, SafeX509Handle x509);
internal static string? GetX509RootStorePath(out bool defaultPath)
internal static unsafe string? GetX509RootStorePath(out bool defaultPath)
{
IntPtr ptr = GetX509RootStorePath_private(out byte usedDefault);
byte usedDefault;
IntPtr ptr = GetX509RootStorePath_private(&usedDefault);
defaultPath = (usedDefault != 0);
return Marshal.PtrToStringAnsi(ptr);
}
internal static string? GetX509RootStoreFile()
internal static unsafe string? GetX509RootStoreFile()
{
return Marshal.PtrToStringAnsi(GetX509RootStoreFile_private(out _));
byte unused;
return Marshal.PtrToStringAnsi(GetX509RootStoreFile_private(&unused));
}
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509RootStorePath")]
private static extern IntPtr GetX509RootStorePath_private(out byte defaultPath);
private static unsafe extern IntPtr GetX509RootStorePath_private(byte* defaultPath);
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509RootStoreFile")]
private static extern IntPtr GetX509RootStoreFile_private(out byte defaultPath);
private static unsafe extern IntPtr GetX509RootStoreFile_private(byte* defaultPath);
[DllImport(Libraries.CryptoNative)]
private static extern int CryptoNative_X509StoreSetVerifyTime(
......
......@@ -8,6 +8,6 @@ internal static partial class Interop
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32)]
internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
internal static unsafe extern BOOL GlobalMemoryStatusEx(MEMORYSTATUSEX* lpBuffer);
}
}
......@@ -14,10 +14,10 @@ internal static partial class NtDll
// https://msdn.microsoft.com/en-us/library/windows/hardware/ff566424.aspx
[DllImport(Libraries.NtDll, CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern unsafe uint NtCreateFile(
out IntPtr FileHandle,
IntPtr* FileHandle,
DesiredAccess DesiredAccess,
ref OBJECT_ATTRIBUTES ObjectAttributes,
out IO_STATUS_BLOCK IoStatusBlock,
OBJECT_ATTRIBUTES* ObjectAttributes,
IO_STATUS_BLOCK* IoStatusBlock,
long* AllocationSize,
FileAttributes FileAttributes,
FileShare ShareAccess,
......@@ -55,11 +55,13 @@ internal static partial class NtDll
rootDirectory,
securityQualityOfService);
IntPtr handle;
IO_STATUS_BLOCK statusBlock;
uint status = NtCreateFile(
out IntPtr handle,
&handle,
desiredAccess,
ref attributes,
out IO_STATUS_BLOCK statusBlock,
&attributes,
&statusBlock,
AllocationSize: preallocationSize,
fileAttributes,
shareAccess,
......
......@@ -16,7 +16,7 @@ internal static partial class NtDll
IntPtr Event,
IntPtr ApcRoutine,
IntPtr ApcContext,
out IO_STATUS_BLOCK IoStatusBlock,
IO_STATUS_BLOCK* IoStatusBlock,
IntPtr FileInformation,
uint Length,
FILE_INFORMATION_CLASS FileInformationClass,
......
......@@ -9,7 +9,7 @@ internal static partial class Interop
internal static partial class User32
{
[DllImport(Libraries.User32, ExactSpelling = true)]
public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
public static unsafe extern int GetWindowThreadProcessId(IntPtr handle, int* processId);
[DllImport(Libraries.User32, ExactSpelling = true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
......
......@@ -9,6 +9,6 @@ internal static partial class Interop
internal static partial class User32
{
[DllImport(Libraries.User32)]
public static extern bool IsWindowVisible(IntPtr hWnd);
public static extern BOOL IsWindowVisible(IntPtr hWnd);
}
}
......@@ -42,7 +42,7 @@ public static unsafe IntPtr FindMainWindow(int processId)
private static bool IsMainWindow(IntPtr handle)
{
return (Interop.User32.GetWindow(handle, GW_OWNER) == IntPtr.Zero) && Interop.User32.IsWindowVisible(handle);
return (Interop.User32.GetWindow(handle, GW_OWNER) == IntPtr.Zero) && Interop.User32.IsWindowVisible(handle) != Interop.BOOL.FALSE;
}
[UnmanagedCallersOnly]
......@@ -51,7 +51,7 @@ private static unsafe Interop.BOOL EnumWindowsCallback(IntPtr handle, IntPtr ext
MainWindowFinder* instance = (MainWindowFinder*)extraParameter;
int processId = 0; // Avoid uninitialized variable if the window got closed in the meantime
Interop.User32.GetWindowThreadProcessId(handle, out processId);
Interop.User32.GetWindowThreadProcessId(handle, &processId);
if ((processId == instance->_processId) && IsMainWindow(handle))
{
......
......@@ -13,7 +13,7 @@ public static unsafe void CheckForAvailableVirtualMemory(ulong nativeSize)
{
Interop.Kernel32.MEMORYSTATUSEX memoryStatus = default;
memoryStatus.dwLength = (uint)sizeof(Interop.Kernel32.MEMORYSTATUSEX);
if (Interop.Kernel32.GlobalMemoryStatusEx(ref memoryStatus))
if (Interop.Kernel32.GlobalMemoryStatusEx(&memoryStatus) != Interop.BOOL.FALSE)
{
ulong totalVirtual = memoryStatus.ullTotalVirtual;
if (nativeSize >= totalVirtual)
......
......@@ -20,12 +20,13 @@ private unsafe bool GetData()
{
Debug.Assert(_directoryHandle != (IntPtr)(-1) && _directoryHandle != IntPtr.Zero && !_lastEntryFound);
Interop.NtDll.IO_STATUS_BLOCK statusBlock;
int status = Interop.NtDll.NtQueryDirectoryFile(
FileHandle: _directoryHandle,
Event: IntPtr.Zero,
ApcRoutine: IntPtr.Zero,
ApcContext: IntPtr.Zero,
IoStatusBlock: out Interop.NtDll.IO_STATUS_BLOCK statusBlock,
IoStatusBlock: &statusBlock,
FileInformation: _buffer,
Length: (uint)_bufferLength,
FileInformationClass: Interop.NtDll.FILE_INFORMATION_CLASS.FileFullDirectoryInformation,
......
......@@ -17,7 +17,7 @@ private static unsafe bool CheckForAvailableMemory(out ulong availPageFile, out
{
Interop.Kernel32.MEMORYSTATUSEX memoryStatus = default;
memoryStatus.dwLength = (uint)sizeof(Interop.Kernel32.MEMORYSTATUSEX);
if (!Interop.Kernel32.GlobalMemoryStatusEx(ref memoryStatus))
if (Interop.Kernel32.GlobalMemoryStatusEx(&memoryStatus) == Interop.BOOL.FALSE)
{
availPageFile = default;
totalAddressSpaceFree = default;
......
......@@ -51,6 +51,7 @@
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="System\Runtime\Caching\MemoryMonitor.Windows.cs" />
<Compile Include="System\Runtime\Caching\PhysicalMemoryMonitor.Windows.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.BOOL.cs" Link="Common\Interop\Windows\Interop.BOOL.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GlobalMemoryStatusEx.cs" Link="Common\Interop\Windows\Kernel32\Interop.GlobalMemoryStatusEx.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.MEMORYSTATUSEX.cs" Link="Common\Interop\Windows\Kernel32\Interop.MEMORYSTATUSEX.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs" Link="Common\Interop\Windows\Interop.Libraries.cs" />
......@@ -85,4 +86,4 @@
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Configuration.ConfigurationManager\src\System.Configuration.ConfigurationManager.csproj" />
</ItemGroup>
</Project>
\ No newline at end of file
</Project>
......@@ -16,7 +16,7 @@ static unsafe MemoryMonitor()
{
Interop.Kernel32.MEMORYSTATUSEX memoryStatus = default;
memoryStatus.dwLength = (uint)sizeof(Interop.Kernel32.MEMORYSTATUSEX);
if (Interop.Kernel32.GlobalMemoryStatusEx(ref memoryStatus))
if (Interop.Kernel32.GlobalMemoryStatusEx(&memoryStatus) != Interop.BOOL.FALSE)
{
s_totalPhysical = (long)memoryStatus.ullTotalPhys;
s_totalVirtual = (long)memoryStatus.ullTotalVirtual;
......
......@@ -14,7 +14,7 @@ protected override unsafe int GetCurrentPressure()
{
Interop.Kernel32.MEMORYSTATUSEX memoryStatus = default;
memoryStatus.dwLength = (uint)sizeof(Interop.Kernel32.MEMORYSTATUSEX);
if (!Interop.Kernel32.GlobalMemoryStatusEx(ref memoryStatus))
if (Interop.Kernel32.GlobalMemoryStatusEx(&memoryStatus) == Interop.BOOL.FALSE)
{
return 0;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册