WindowsApi.cs 3.6 KB

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