下面是一个系统函数重定位的例子,从<<计算机世界日报>>上Copy出来的.<br> //源程序 relocate.c<br> #include <WINDOWS.H><br> #include <dos.h><br> BOOL WINAPI MyExtTextOut(HDC hDC, int x, int y, UINT nIn<br>t1, const RECTFAR*p Rect,LPCSTR lpStr, UINT nInt2, int FAR* <br>lpInt);<br> WORD FAR PASCAL AllocCStoDSAlias(WORD code_sel);<br> typedef struct tagFUNC<br> {<br> FARPROC lpFarProcReplace; //替代函数地址<br> FARPROC lpFarProcWindows; //Windows函数地址<br> BYTE bOld; //保存原函数第一字节<br> LONG lOld; //保存原函数接后的四字节长值<br> }FUNC;<br> FUNC Func={MyExtTextOut,ExtTextOut};<br> //Windows主函数<br> int PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevIn<br>stance,LPSTR lpCmdi ne,int nCmdShow){<br> HANDLE hMemCode; //代码段句柄<br> WORD hMemData; //相同基址的可写数据段别名<br> WORD wOffset; //函数偏移<br> LPSTR lpStr;<br> LPLONG lpLong;<br> char lpNotice[96];<br> hMemCode=HIWORD((LONG) Func.lpFarProcWindows );<br> wOffset=LOWORD((LONG) Func.lpFarProcWindows );<br> wsprintf(lpNotice,"函数所在模块句柄 0x%4xH,偏移 0x%4xH",<br>hMemCode,wOffset);<br> MessageBox(NULL,lpNotice,"提示",MB_OK);<br> //取与代码段有相同基址的可写数据段别名<br> hMemData=AllocCStoDSAlias(hMemCode);<br> lpStr=GlobalLock(hMemData);<br> lpLong=(lpStr+wOffset+1 );<br> //保存原函数要替换的头几个字节<br> Func.bOld=*(lpStr+wOffset);<br> Func.lOld=*lpLong;<br> *(lpStr+wOffset)=0xEA;<br> *lpLong=Func.lpFarProcReplace;<br> GlobalUnlock(hMemData);<br> MessageBox(NULL,"改为自己的函数","提示",MB_OK);<br> //将保留的内容改回来<br> hMemData=AllocCStoDSAlias(hMemCode);<br> lpStr=GlobalLock(hMemData);<br> lpLong=(lpStr+wOffset+1 );<br> *(lpStr+wOffset)=Func.bOld;<br> *lpLong=Func.lOld;<br> GlobalUnlock(hMemData);<br> MessageBox(NULL,"改回原Windows函数","提示",MB_OK);<br> return 1;<br> }<br> //自己的替代函数<br> BOOL WINAPI MyExtTextOut(HDC hDC, int x, int y, UINT nIn<br>t1, const RECT FAR lpRect, LPCSTR lpStr, UINT nInt2, int FAR<br>* lpInt){<br> BYTE NameDot[96]={<br> 0x09, 0x00, 0xfd, 0x08, 0x09, 0x08, 0x09, 0x10, 0x09, 0x<br>20,<br> 0x79, 0x40, 0x41, 0x04, 0x47, 0xfe, 0x41, 0x40, 0x79, 0x<br>40,<br> 0x09, 0x20, 0x09, 0x20, 0x09, 0x10, 0x09, 0x4e, 0x51, 0x<br>84,<br> 0x21, 0x00, 0x02, 0x00, 0x01, 0x04, 0xff, 0xfe, 0x00, 0x<br>00,<br> 0x1f, 0xf0, 0x10, 0x10, 0x10, 0x10, 0x1f, 0xf0, 0x00, 0x<br>00,<br> 0x7f, 0xfc, 0x40, 0x04, 0x4f, 0xe4, 0x48, 0x24, 0x48, 0x<br>24,<br> 0x4f, 0xe4, 0x40, 0x0c, 0x10, 0x80, 0x10, 0xfc, 0x10, 0x<br>88,<br> 0x11, 0x50, 0x56, 0x20, 0x54, 0xd8, 0x57, 0x06, 0x54, 0x<br>20,<br> 0x55, 0xfc, 0x54, 0x20, 0x55, 0xfc, 0x5c, 0x20, 0x67, 0x<br>fe,<br> 0x00, 0x20, 0x00, 0x20, 0x00, 0x20<br> };<br> HBITMAP hBitmap,hOldBitmap;<br> HDC hMemDC;<br> BYTE far *lpDot;<br> int i;<br> for ( i=0;i<3;i++ )<br> {<br> lpDot=(LPSTR)NameDot+i*32;<br> hMemDC=CreateCompatibleDC(hDC);<br> hBitmap=CreateBitmap(16,16,1,1,lpDot);<br> SetBitmapBits(hBitmap,32L,lpDot);<br> hOldBitmap=SelectObject(hMemDC,hBitmap);<br> BitBlt(hDC,x+i*16,y,16,16,hMemDC,0,0,SRCCOPY);<br> DeleteDC(hMemDC);<br> DeleteObject(hBitmap);<br> }<br> return TRUE;<br> }<br><br> //模块定义文件 relocate.def<br> NAME RELOCATE<br> EXETYPE WINDOWS<br> CODE PRELOAD MOVEABLE DISCARDABLE<br> DATA PRELOAD MOVEABLE MULTIPLE<br> HEAPSIZE 1024<br> EXPORTS