VirtualAlloc和VirtualCopy的蕴含知识点

VirtualAlloc和VirtualCopy的蕴含知识点


1.VirtualAlloc用来在进程的虚拟地址空间中保留(reserve)或者提交(commit)页。在保留时以64KB为粒度,即保留空间以64K为单位。而提交虚拟地址时,则以页(典型大小为4KB)为单位。


2.VirtualCopy用来绑定一块物理内存到当前进程虚拟地址空间。参数里的lpvSrc既可以是内核段的虚拟地址也可以是物理地址(用page_physical来标记)。同时要注意lpvSrc的右移与否。


      PAGE_PHYSICAL这个参数决定了要右移八位(除以256),不过还有一点就是使用了PAGE_PHYSICAL之后就不要使用VirtualFree 了因为,使用了也无济于事


3.使用VirtualAlloc要包含Winbase.h;使用VirtualCopy时要包含plfuncs.h.两者都要链接coredll.lib. 


4.在CE5.0之前,使用VirtualAlloc获得的虚拟地址空间分为两种情形:
(1)大小在2MB以下时,位于调用进程的虚拟空间中;
(2)大小大于2MB时,位于用户态的共享地址空间内(0x42000000-0x7E000000 )


注解:


1. 如果copy的物理地址在512M范围内,那么由于静态映射的存在,lpvSrc可以为静态映射的虚拟地址,也可以为物理地址。采用后者需要指定page_physical,同时lpvSrc右移8位。 
2. 如果copy的物理地址在512M范围外,那么由于微软的如下规定“ 
VirtualCopy also supports the PAGE_PHYSICAL flag. You must set this flag when you are mapping physical memory that resides beyond 512 MB, that is, physical memory with an address above 0x1FFFFFFF.” 
lpvSrc只能为物理地址,同时需要右移。


VirtualAlloc用来在进程的虚拟地址空间中保留(reserve)或者提交(commit)页。在保留时以64KB为粒度,即保留空间以64K为单位。而提交虚拟地址时,则以页(典型大小为4KB)为单位。 
在保留时以64KB为粒度,即保留空间以64K为单位。而提交虚拟地址时,则以页(典型大小为4KB)为单位


在程序前面加上: #include<window.h> 
这样声明: 
public static class API 



[DllImport( "Kernel32.dll", CharSet = CharSet.Ansi, EntryPoint = "VirtualAlloc" )] 
public static extern IntPtr VirtualAlloc( 
IntPtr lpAddress, 
uint dwSize, 
uint flAllocationType, 
uint flProtect ); 




[DllImport( "Coredll.dll", CharSet = CharSet.Ansi, EntryPoint = "VirtualCopy" )] 
public static extern bool VirtualCopy( 
IntPtr lpvDest, 
IntPtr lpvSrc, 
uint cbSize, 
uint fdwProtect ); 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值