public static Boolean MsIoUnmapMemory(IntPtr hDriver, APIDef.MSIO_PHYSICAL_MEMORY_INFO MemMapInfo) { IntPtr pMpmi = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO))); APIDef.RtlZeroMemory(pMpmi, Marshal.SizeOf(typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO))); Marshal.StructureToPtr(MemMapInfo, pMpmi, true); APIDef.IO_STATUS_BLOCK isb = new APIDef.IO_STATUS_BLOCK(); UInt32 CallRes = APIDef.NtDeviceIoControlFile( hDriver, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref isb, APIDef.IOCTL_MSIO_UNMAPPHYSADDR, pMpmi, (UInt32)Marshal.SizeOf(typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO)), pMpmi, (UInt32)Marshal.SizeOf(typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO))); // Free alloc Marshal.FreeHGlobal(pMpmi); if (CallRes != APIDef.NTSTATUS_STATUS_SUCCESS) { return(false); } else { return(true); } }
public static APIDef.MSIO_PHYSICAL_MEMORY_INFO MsIoAllocatePhysicalMemory(IntPtr hDriver, IntPtr BaseAddress, UInt32 Size) { APIDef.MSIO_PHYSICAL_MEMORY_INFO mpmi = new APIDef.MSIO_PHYSICAL_MEMORY_INFO(); mpmi.ViewSize = (UIntPtr)(BaseAddress.ToInt64() + Size); IntPtr pMpmi = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO))); APIDef.RtlZeroMemory(pMpmi, Marshal.SizeOf(typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO))); Marshal.StructureToPtr(mpmi, pMpmi, true); APIDef.IO_STATUS_BLOCK isb = new APIDef.IO_STATUS_BLOCK(); UInt32 CallRes = APIDef.NtDeviceIoControlFile( hDriver, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref isb, APIDef.IOCTL_MSIO_MAPPHYSTOLIN, pMpmi, (UInt32)Marshal.SizeOf(typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO)), pMpmi, (UInt32)Marshal.SizeOf(typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO))); if (CallRes != APIDef.NTSTATUS_STATUS_SUCCESS) { // Free alloc Marshal.FreeHGlobal(pMpmi); // Make sure baseaddress is null mpmi.BaseAddess = IntPtr.Zero; return(mpmi); } else { // Ptr->Struct mpmi = (APIDef.MSIO_PHYSICAL_MEMORY_INFO)Marshal.PtrToStructure(pMpmi, typeof(APIDef.MSIO_PHYSICAL_MEMORY_INFO)); // Free alloc Marshal.FreeHGlobal(pMpmi); return(mpmi); } }