首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > WinCE >

C#VirtualCopy的方式读写物理内存有有关问题

2012-09-29 
C#,VirtualCopy的方式读写物理内存有问题,请指教C# codeclass WinCE_IO_Accessor{enum PageAccessFlags :

C#,VirtualCopy的方式读写物理内存有问题,请指教

C# code
    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);        }    }



帮别人做一个WinCE5.0下的小软件,要和硬件IO端口打交道,所以需要读写物理内存
C++很不熟悉,所以决定用C#,效率高点
读写物理内存的代码如上,写入一个字节1后,再读出来是0
现在硬件还没到位,在VS2008自带的模拟器(PPC2003)里跑的

不知道哪里出问题了,请明白人指点
非常感谢

[解决办法]
设计硬件的最好还是接上硬件调试,个人认为电脑很难仿
[解决办法]
你直接往里面写8个,然后直接打印出来看一下。
[解决办法]
UINT nNewSize = (UINT)(2048*2048*200); // 1024.0) * 1024;
 double * pNewBuffer =(double*) VirtualAlloc(NULL,nNewSize,MEM_COMMIT,PAGE_READWRITE);
 if (pNewBuffer)
 {
//测试虚拟内存。
 double Block;
 for(int i =0;i<100;i++)
{
  
Block=10.11*i;
memcpy(&pNewBuffer[i],&Block,sizeof(Block));
 
}
 double tt;
 tt=(pNewBuffer[99]);
 CString s;
 s.Format(_T("The total is %f"),tt);
 AfxMessageBox(s) ; 
 //删除分配的内存。


 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。

热点排行