VirtualAlloc和VirtualCopy的蕴含知识点

来源:互联网 发布:怎样主动联系淘宝客 编辑:程序博客网 时间:2024/04/29 09:09

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 );
}

原创粉丝点击