2022-05-03 01:30:02 +02:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Ryujinx.Memory.WindowsShared
|
|
|
|
{
|
2022-12-15 18:07:31 +01:00
|
|
|
static partial class WindowsApi
|
2022-05-03 01:30:02 +02:00
|
|
|
{
|
|
|
|
public static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
|
|
|
|
public static readonly IntPtr CurrentProcessHandle = new IntPtr(-1);
|
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll", SetLastError = true)]
|
|
|
|
public static partial IntPtr VirtualAlloc(
|
2022-05-03 01:30:02 +02:00
|
|
|
IntPtr lpAddress,
|
|
|
|
IntPtr dwSize,
|
|
|
|
AllocationType flAllocationType,
|
|
|
|
MemoryProtection flProtect);
|
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("KernelBase.dll", SetLastError = true)]
|
|
|
|
public static partial IntPtr VirtualAlloc2(
|
2022-05-03 01:30:02 +02:00
|
|
|
IntPtr process,
|
|
|
|
IntPtr lpAddress,
|
|
|
|
IntPtr dwSize,
|
|
|
|
AllocationType flAllocationType,
|
|
|
|
MemoryProtection flProtect,
|
|
|
|
IntPtr extendedParameters,
|
|
|
|
ulong parameterCount);
|
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll", SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
public static partial bool VirtualProtect(
|
2022-05-03 01:30:02 +02:00
|
|
|
IntPtr lpAddress,
|
|
|
|
IntPtr dwSize,
|
|
|
|
MemoryProtection flNewProtect,
|
|
|
|
out MemoryProtection lpflOldProtect);
|
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll", SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
public static partial bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, AllocationType dwFreeType);
|
2022-05-03 01:30:02 +02:00
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll", SetLastError = true, EntryPoint = "CreateFileMappingW")]
|
|
|
|
public static partial IntPtr CreateFileMapping(
|
2022-05-03 01:30:02 +02:00
|
|
|
IntPtr hFile,
|
|
|
|
IntPtr lpFileMappingAttributes,
|
|
|
|
FileMapProtection flProtect,
|
|
|
|
uint dwMaximumSizeHigh,
|
|
|
|
uint dwMaximumSizeLow,
|
|
|
|
[MarshalAs(UnmanagedType.LPWStr)] string lpName);
|
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll", SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
public static partial bool CloseHandle(IntPtr hObject);
|
2022-05-03 01:30:02 +02:00
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll", SetLastError = true)]
|
|
|
|
public static partial IntPtr MapViewOfFile(
|
2022-05-03 01:30:02 +02:00
|
|
|
IntPtr hFileMappingObject,
|
|
|
|
uint dwDesiredAccess,
|
|
|
|
uint dwFileOffsetHigh,
|
|
|
|
uint dwFileOffsetLow,
|
|
|
|
IntPtr dwNumberOfBytesToMap);
|
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("KernelBase.dll", SetLastError = true)]
|
|
|
|
public static partial IntPtr MapViewOfFile3(
|
2022-05-03 01:30:02 +02:00
|
|
|
IntPtr hFileMappingObject,
|
|
|
|
IntPtr process,
|
|
|
|
IntPtr baseAddress,
|
|
|
|
ulong offset,
|
|
|
|
IntPtr dwNumberOfBytesToMap,
|
|
|
|
ulong allocationType,
|
|
|
|
MemoryProtection dwDesiredAccess,
|
|
|
|
IntPtr extendedParameters,
|
|
|
|
ulong parameterCount);
|
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll", SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
public static partial bool UnmapViewOfFile(IntPtr lpBaseAddress);
|
2022-05-03 01:30:02 +02:00
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("KernelBase.dll", SetLastError = true)]
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
public static partial bool UnmapViewOfFile2(IntPtr process, IntPtr lpBaseAddress, ulong unmapFlags);
|
2022-05-03 01:30:02 +02:00
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll")]
|
|
|
|
public static partial uint GetLastError();
|
2022-05-03 01:30:02 +02:00
|
|
|
|
2022-12-15 18:07:31 +01:00
|
|
|
[LibraryImport("kernel32.dll")]
|
|
|
|
public static partial int GetCurrentThreadId();
|
2022-07-30 00:16:29 +02:00
|
|
|
|
2022-05-03 01:30:02 +02:00
|
|
|
public static MemoryProtection GetProtection(MemoryPermission permission)
|
|
|
|
{
|
|
|
|
return permission switch
|
|
|
|
{
|
|
|
|
MemoryPermission.None => MemoryProtection.NoAccess,
|
|
|
|
MemoryPermission.Read => MemoryProtection.ReadOnly,
|
|
|
|
MemoryPermission.ReadAndWrite => MemoryProtection.ReadWrite,
|
|
|
|
MemoryPermission.ReadAndExecute => MemoryProtection.ExecuteRead,
|
|
|
|
MemoryPermission.ReadWriteExecute => MemoryProtection.ExecuteReadWrite,
|
|
|
|
MemoryPermission.Execute => MemoryProtection.Execute,
|
|
|
|
_ => throw new MemoryProtectionException(permission)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|