- 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)里跑的
不知道哪里出问题了,请明白人指点
非常感谢
没人关注么,就算不确定给点思路也行啊,大家一起讨论一下
设计硬件的最好还是接上硬件调试,个人认为电脑很难仿
引用: 引用 3 楼 suntion 的回复:
设计硬件的最好还是接上硬件调试,个人认为电脑很难仿
有道理,不过硬件还得段时间到位,想在这之前把路走通
我感觉在模拟器上跑不通的话在硬件上也够呛
没有这方面的经验,不知道是不是这样的
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,&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
引用: 引用 7 楼 macrozhang001 的回复:
UINT nNewSize = (UINT)(2048*2048*200); // 1024.0) * 1024;
double * pNewBuffer =(double*) VirtualAlloc(NULL,nNewSize,MEM_COMMIT,PAGE_READWRITE);
if (pNewBuffer)
{
//测试虚拟内存。
double Block;
……
这个例子和我要做的事情不一样,我要做的是直接操作物理内存,所以用VirtualCopy
如果直接开一块虚拟内存来读写,是可以成功的,如下
- buf = VirtualAlloc(IntPtr.Zero, SIZE, MemUsageFlags.MEM_COMMIT, PageAccessFlags.PAGE_READWRITE);
- if (buf == IntPtr.Zero)
- {
- throw new Exception("VirtualAlloc Fail");
- }
- byte a = 1;
- Marshal.WriteByte(buf, a);
- byte b = Marshal.ReadByte(buf); //这里读出的b=1
- VirtualFree(buf, 0, MemUsageFlags.MEM_RELEASE);
带着个标志PAGE_PHYSICAL的话
貌似你的BASEADDR需要做移位操作,好像是左移8位吧,具体你看看msdn。
引用: 引用 12 楼 rushonin 的回复:
带着个标志PAGE_PHYSICAL的话
貌似你的BASEADDR需要做移位操作,好像是左移8位吧,具体你看看msdn。
const uint BASE_ADDR = DRIVER_GLOBALS_PHYSICAL_MEMORY_START / 256;
除了256,已经移过了
我拿到实际硬件了
调用板子厂家提供的API搞定
现在回想一下, 觉得应该是在模拟器里写的这块地址有问题,不允许写,所以写不进去
谢谢大家的关注和解答,结贴了
PVOID MmMapIoSpace(
PHYSICAL_ADDRESS PhysicalAddress,
ULONG NumberOfBytes,
BOOLEAN CacheEnable
);