WindowsApi.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Ryujinx.Memory.WindowsShared
  4. {
  5. static partial class WindowsApi
  6. {
  7. public static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
  8. public static readonly IntPtr CurrentProcessHandle = new IntPtr(-1);
  9. [LibraryImport("kernel32.dll", SetLastError = true)]
  10. public static partial IntPtr VirtualAlloc(
  11. IntPtr lpAddress,
  12. IntPtr dwSize,
  13. AllocationType flAllocationType,
  14. MemoryProtection flProtect);
  15. [LibraryImport("KernelBase.dll", SetLastError = true)]
  16. public static partial IntPtr VirtualAlloc2(
  17. IntPtr process,
  18. IntPtr lpAddress,
  19. IntPtr dwSize,
  20. AllocationType flAllocationType,
  21. MemoryProtection flProtect,
  22. IntPtr extendedParameters,
  23. ulong parameterCount);
  24. [LibraryImport("kernel32.dll", SetLastError = true)]
  25. [return: MarshalAs(UnmanagedType.Bool)]
  26. public static partial bool VirtualProtect(
  27. IntPtr lpAddress,
  28. IntPtr dwSize,
  29. MemoryProtection flNewProtect,
  30. out MemoryProtection lpflOldProtect);
  31. [LibraryImport("kernel32.dll", SetLastError = true)]
  32. [return: MarshalAs(UnmanagedType.Bool)]
  33. public static partial bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, AllocationType dwFreeType);
  34. [LibraryImport("kernel32.dll", SetLastError = true, EntryPoint = "CreateFileMappingW")]
  35. public static partial IntPtr CreateFileMapping(
  36. IntPtr hFile,
  37. IntPtr lpFileMappingAttributes,
  38. FileMapProtection flProtect,
  39. uint dwMaximumSizeHigh,
  40. uint dwMaximumSizeLow,
  41. [MarshalAs(UnmanagedType.LPWStr)] string lpName);
  42. [LibraryImport("kernel32.dll", SetLastError = true)]
  43. [return: MarshalAs(UnmanagedType.Bool)]
  44. public static partial bool CloseHandle(IntPtr hObject);
  45. [LibraryImport("kernel32.dll", SetLastError = true)]
  46. public static partial IntPtr MapViewOfFile(
  47. IntPtr hFileMappingObject,
  48. uint dwDesiredAccess,
  49. uint dwFileOffsetHigh,
  50. uint dwFileOffsetLow,
  51. IntPtr dwNumberOfBytesToMap);
  52. [LibraryImport("KernelBase.dll", SetLastError = true)]
  53. public static partial IntPtr MapViewOfFile3(
  54. IntPtr hFileMappingObject,
  55. IntPtr process,
  56. IntPtr baseAddress,
  57. ulong offset,
  58. IntPtr dwNumberOfBytesToMap,
  59. ulong allocationType,
  60. MemoryProtection dwDesiredAccess,
  61. IntPtr extendedParameters,
  62. ulong parameterCount);
  63. [LibraryImport("kernel32.dll", SetLastError = true)]
  64. [return: MarshalAs(UnmanagedType.Bool)]
  65. public static partial bool UnmapViewOfFile(IntPtr lpBaseAddress);
  66. [LibraryImport("KernelBase.dll", SetLastError = true)]
  67. [return: MarshalAs(UnmanagedType.Bool)]
  68. public static partial bool UnmapViewOfFile2(IntPtr process, IntPtr lpBaseAddress, ulong unmapFlags);
  69. [LibraryImport("kernel32.dll")]
  70. public static partial uint GetLastError();
  71. [LibraryImport("kernel32.dll")]
  72. public static partial int GetCurrentThreadId();
  73. public static MemoryProtection GetProtection(MemoryPermission permission)
  74. {
  75. return permission switch
  76. {
  77. MemoryPermission.None => MemoryProtection.NoAccess,
  78. MemoryPermission.Read => MemoryProtection.ReadOnly,
  79. MemoryPermission.ReadAndWrite => MemoryProtection.ReadWrite,
  80. MemoryPermission.ReadAndExecute => MemoryProtection.ExecuteRead,
  81. MemoryPermission.ReadWriteExecute => MemoryProtection.ExecuteReadWrite,
  82. MemoryPermission.Execute => MemoryProtection.Execute,
  83. _ => throw new MemoryProtectionException(permission)
  84. };
  85. }
  86. }
  87. }