2022-10-15 16:53:24 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2022-11-13 14:39:29 +01:00
|
|
|
|
namespace CommonHelpers
|
2022-10-15 16:53:24 +02:00
|
|
|
|
{
|
2022-11-15 15:42:05 +01:00
|
|
|
|
public class InpOut
|
2022-10-15 16:53:24 +02:00
|
|
|
|
{
|
|
|
|
|
|
[DllImport("inpoutx64.dll", EntryPoint = "MapPhysToLin", CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
|
public static extern IntPtr MapPhysToLin(IntPtr pbPhysAddr, uint dwPhysSize, out IntPtr pPhysicalMemoryHandle);
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("inpoutx64.dll", EntryPoint = "UnmapPhysicalMemory", CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
|
|
|
|
public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, IntPtr pbLinAddr);
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("inpoutx64.dll", EntryPoint = "DlPortReadPortUchar", CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
|
[return: MarshalAs(UnmanagedType.U1)]
|
|
|
|
|
|
public static extern byte DlPortReadPortUchar(ushort port);
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("inpoutx64.dll", EntryPoint = "DlPortWritePortUchar", CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
|
public static extern byte DlPortWritePortUchar(ushort port, byte vlaue);
|
|
|
|
|
|
|
2022-11-15 15:42:05 +01:00
|
|
|
|
[DllImport("inpoutx64.dll", CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
|
public static extern bool GetPhysLong(IntPtr pbPhysAddr, out uint physValue);
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("inpoutx64.dll", CallingConvention = CallingConvention.StdCall)]
|
|
|
|
|
|
public static extern bool SetPhysLong(IntPtr pbPhysAddr, uint physValue);
|
|
|
|
|
|
|
2022-10-15 16:53:24 +02:00
|
|
|
|
public static byte[] ReadMemory(IntPtr baseAddress, uint size)
|
|
|
|
|
|
{
|
|
|
|
|
|
IntPtr pdwLinAddr = MapPhysToLin(baseAddress, size, out IntPtr pPhysicalMemoryHandle);
|
|
|
|
|
|
if (pdwLinAddr != IntPtr.Zero)
|
|
|
|
|
|
{
|
|
|
|
|
|
byte[] bytes = new byte[size];
|
|
|
|
|
|
Marshal.Copy(pdwLinAddr, bytes, 0, bytes.Length);
|
|
|
|
|
|
UnmapPhysicalMemory(pPhysicalMemoryHandle, pdwLinAddr);
|
|
|
|
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool WriteMemory(IntPtr baseAddress, byte[] data)
|
|
|
|
|
|
{
|
|
|
|
|
|
IntPtr pdwLinAddr = MapPhysToLin(baseAddress, (uint)data.Length, out IntPtr pPhysicalMemoryHandle);
|
|
|
|
|
|
if (pdwLinAddr != IntPtr.Zero)
|
|
|
|
|
|
{
|
|
|
|
|
|
Marshal.Copy(data, 0, pdwLinAddr, data.Length);
|
|
|
|
|
|
UnmapPhysicalMemory(pPhysicalMemoryHandle, pdwLinAddr);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|