您的位置:首页 > 编程语言

WinCE 下进程可访问的代码页的地址获取

2014-04-02 19:18 357 查看
此功能是在看 TCPMP 代码时发现的,感觉以后的工作中可能用到此部分功能,所以记录下来。

#include "windef.h"
#include "windows.h"
/*
 * 功能: 进程可访问的代码页
 * 参数:  pPtr(in) 进程中一函数的指针
 		    ppucMin,ppucMax(out) 输出进程可访问地址的最小/最大值
 		    puiPageSize(in/out) 页面大小设置与输出
*/
void CodeAddrFindPages(void *pPtr,unsigned char **ppucMin,unsigned char **ppucMax,unsigned int *puiPageSize)
{
	unsigned char *pucMin = NULL;
	unsigned char *pucMax = NULL;
	unsigned int uiPageSize = 
	#if defined(MIPS)
		1024;
	#else
		4096;
	#endif
	if(puiPageSize)
		*puiPageSize = uiPageSize;

	pucMin = pucMax = (unsigned char *)((unsigned int)pPtr & (~(uiPageSize - 1)));		// ~ 的优先级高于位操作符 &
	// Leo: IsBadCodePtr - Determines whether the calling process has read access to the memory at the specified address.
	while(!IsBadCodePtr((FARPROC)(pucMin - uiPageSize)))
		pucMin -= uiPageSize;
	while(!IsBadCodePtr((FARPROC)pucMax))
		pucMax += uiPageSize;

	*ppucMin = pucMin;
	*ppucMax = pucMax;
#ifdef _USE_WINDOWS_CE_PLATFORM
	RETAILMSG(1,(L"[CodeAddr]min = 0x%X; max = 0x%X\r\n",pucMin,pucMax));
#else
	printf("[CodeAddr]min = 0x%X; max = 0x%X\r\n",pucMin,pucMax);
#endif
}

运行结果:
[CodeAddr]min = 0x11000; max = 0x195000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: