C#,VirtualCopy的方式读写物理内存有问题,请指教
class WinCE_IO_Accessor { enum PageAccessFlags : uint { PAGE_NOACCESS = 0x01, PAGE_READONLY = 0x02, PAGE_READWRITE = 0x04, PAGE_WRITECOPY = 0x08, PAGE_EXECUTE = 0x10, PAGE_EXECUTE_READ = 0x20, PAGE_EXECUTE_READWRITE = 0x40, PAGE_EXECUTE_WRITECOPY = 0x80, PAGE_GUARD = 0x100, PAGE_NOCACHE = 0x200, PAGE_PHYSICAL = 0x400, PAGE_WRITECOMBINE = 0x400 } enum MemUsageFlags : uint { MEM_COMMIT = 0x1000, MEM_RESERVE = 0x2000, MEM_DECOMMIT = 0x4000, MEM_RELEASE = 0x8000, MEM_FREE = 0x10000, MEM_PRIVATE = 0x20000, MEM_MAPPED = 0x40000, MEM_RESET = 0x80000, MEM_TOP_DOWN = 0x100000, MEM_AUTO_COMMIT = 0x200000, MEM_4MB_PAGES = 0x80000000 } [DllImport("Coredll.dll", EntryPoint = "VirtualAlloc", SetLastError = true)] private static extern IntPtr VirtualAlloc( IntPtr lpAddress, uint dwSize, MemUsageFlags flAllocationType, PageAccessFlags flProtect); [DllImport("Coredll.dll", EntryPoint = "VirtualCopy", SetLastError = true)] private static extern bool VirtualCopy( IntPtr lpvDest, IntPtr lpvSrc, uint cbSize, PageAccessFlags fdwProtect); [DllImport("Coredll.dll", EntryPoint = "VirtualFree", SetLastError = true)] private static extern bool VirtualFree( IntPtr lpAddress, uint cbSize, MemUsageFlags dwFreeType); const uint DRIVER_GLOBALS_PHYSICAL_MEMORY_START = 0xac059000; const uint BASE_ADDR = DRIVER_GLOBALS_PHYSICAL_MEMORY_START / 256; const uint SIZE = 8; public IntPtr buf; public WinCE_IO_Accessor() { buf = VirtualAlloc(IntPtr.Zero, SIZE, MemUsageFlags.MEM_RESERVE, PageAccessFlags.PAGE_NOACCESS); if (buf == IntPtr.Zero) { throw new Exception("VirtualAlloc Fail"); } if (!VirtualCopy(buf, (IntPtr)BASE_ADDR, SIZE, PageAccessFlags.PAGE_READWRITE | PageAccessFlags.PAGE_NOCACHE | PageAccessFlags.PAGE_PHYSICAL)) { throw new Exception("VirtualCopy Fail"); } byte a = 1; Marshal.WriteByte(buf, a); byte b = Marshal.ReadByte(buf); //这里读出的b=0 VirtualFree(buf, 0, MemUsageFlags.MEM_RELEASE); } }
VirtualFree(pNewBuffer,0,MEM_RELEASE);
}
文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/c++/cppsl/2008521/117523.html
[解决办法]
顶一下,
[解决办法]
带着个标志PAGE_PHYSICAL的话
貌似你的BASEADDR需要做移位操作,好像是左移8位吧,具体你看看msdn。