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

此功能是在看 TCPMP 代码时发现的,感觉以后的工作中可能用到此部分功能,所以记录下来。

 1 #include "windef.h"  
 2 #include "windows.h"  
 3 /* 
 4  * 功能: 进程可访问的代码页 
 5  * 参数:  pPtr(in) 进程中一函数的指针 
 6             ppucMin,ppucMax(out) 输出进程可访问地址的最小/最大值 
 7             puiPageSize(in/out) 页面大小设置与输出 
 8 */  
 9 void CodeAddrFindPages(void *pPtr,unsigned char **ppucMin,unsigned char **ppucMax,unsigned int *puiPageSize)  
10 {  
11     unsigned char *pucMin = NULL;  
12     unsigned char *pucMax = NULL;  
13     unsigned int uiPageSize =   
14     #if defined(MIPS)  
15         1024;  
16     #else  
17         4096;  
18     #endif  
19     if(puiPageSize)  
20         *puiPageSize = uiPageSize;  
21   
22     pucMin = pucMax = (unsigned char *)((unsigned int)pPtr & (~(uiPageSize - 1)));      // ~ 的优先级高于位操作符 &  
23     // Leo: IsBadCodePtr - Determines whether the calling process has read access to the memory at the specified address.  
24     while(!IsBadCodePtr((FARPROC)(pucMin - uiPageSize)))  
25         pucMin -= uiPageSize;  
26     while(!IsBadCodePtr((FARPROC)pucMax))  
27         pucMax += uiPageSize;  
28   
29     *ppucMin = pucMin;  
30     *ppucMax = pucMax;  
31 #ifdef _USE_WINDOWS_CE_PLATFORM  
32     RETAILMSG(1,(L"[CodeAddr]min = 0x%X; max = 0x%X
",pucMin,pucMax));  
33 #else  
34     printf("[CodeAddr]min = 0x%X; max = 0x%X
",pucMin,pucMax);  
35 #endif  
36 }  

运行结果:

[CodeAddr]min = 0x11000; max = 0x195000  
原文地址:https://www.cnblogs.com/91program/p/5205095.html