在文章《浅析Windows CE跨进程内存注入实现窗体消息挂接(上)》中,我们已经得到了这个七巧板游戏所需要的所有小板块,剩下的事就是等待我们按一定顺序将合适的板块放到合适的位置,本章我们开始进行真刀真枪的实战演练。
程序目标:捕获explore窗体(也就是程序窗体的消息并输出到WinProcInfo.txt中)
程序的执行步骤设计如下:
1、编写一个窗体消息挂接DLL,这个DLL提供一个,函数中利用setwindowlong函数将窗体的默认消息处理过程改为这个挂接DLL中定义的一个窗体过程。
2、在C#程序中利用findwindow等API函数获得exlore类窗体的句柄及窗体所属的进程,并使用performcallback4在目标进程空间中执行coredll.dll的loadLibrary函数将我们写的挂接dll放到目标进程中。
3、在C#程序中使用performcallback4在目标进程空间中执行挂接DLL提供的导出接口函数实现跨进程窗体消息截获.
一、程序的实现如下:
在VS2005中建立一个智能设备的MFC DLL,命名为HookWindowsProcMFCDLL。
在HookWindowsProcMFCDLL.cpp中进行挂接DLL的核心编码:
LRESULT CALLBACK fnHookWindowProc(HWND hwnd,UINT msg,WPARAM wparam, LPARAM lparam);
int __declspec(dllexport) WINAPI fnAttachWinProc(HWND ni_hAttatchWin,PVOID ,PVOID,PVOID);
int __declspec(dllexport) WINAPI fnDetachWinMsgProc(HWND ni_hDetachWin);
WNDPROC tpOldWindowProc;
FILE *m_pDebugOutputFile;
//将一个窗体消息处理挂接到net精简版MessageWindow对象上的代码 typedef struct { WNDPROC OldWinProc;//保留窗体原始消息处理过程的函数指针 HWND WindowHandle;//保存net精简版中对应的窗口挂接的MessageWindow对象的句柄 } DEFUDT_AttachWinInfo; //end struct
CMap<HWND,HWND,DEFUDT_AttachWinInfo,DEFUDT_AttachWinInfo> m_aAttachWinInfoMap;
//对指定的窗口进程进行挂接 int __declspec(dllexport) WINAPI fnAttachWinProc(HWND ni_hAttatchWin, PVOID ni_0, PVOID ni_1, PVOID ni_2 ) { DEFUDT_AttachWinInfo tudtAttachWinInfo; m_pDebugOutputFile = fopen('Storage CardWinProcInfo.txt', 'w'); WNDPROC tpOldWindowProc=(WNDPROC)::SetWindowLong(ni_hAttatchWin, GWL_WNDPROC,(LONG) fnHookWindowProc ); fprintf(m_pDebugOutputFile,'Attatch successfully! OldWindowProc: %08Xn',tpOldWindowProc); tudtAttachWinInfo.OldWinProc=tpOldWindowProc ; tudtAttachWinInfo.WindowHandle=ni_hAttatchWin; m_aAttachWinInfoMap.SetAt(ni_hAttatchWin,tudtAttachWinInfo); fclose(m_pDebugOutputFile); return 77;// (int)tpOldWindowProc ; }//end function
int __declspec(dllexport) WINAPI fnDetachWinMsgProc(HWND ni_hDetachWin) { DEFUDT_AttachWinInfo tudtAttachWinInfo; WNDPROC tpOldWindowProc;
//取得在ncf中消息接收窗口对应的原始消息处理函数的函数指针 m_aAttachWinInfoMap.Lookup(ni_hDetachWin,tudtAttachWinInfo) ;
//将窗体的消息处理函数设为默认的处理过程 tpOldWindowProc =(WNDPROC) SetWindowLong(ni_hDetachWin,GWL_WNDPROC , (LONG)tudtAttachWinInfo.OldWinProc);
//将挂接信息消息处理映谢类中删除 m_aAttachWinInfoMap.RemoveKey(ni_hDetachWin);
return (int)tpOldWindowProc ;
}//end function
LRESULT CALLBACK fnHookWindowProc(HWND hwnd,UINT msg,WPARAM wparam, LPARAM lparam) { DEFUDT_AttachWinInfo tudtAttachWinInfo; m_aAttachWinInfoMap.Lookup(hwnd,tudtAttachWinInfo) ; m_pDebugOutputFile = fopen('Storage CardWinProcInfo.txt', 'a'); if (m_pDebugOutputFile!=NULL) { fprintf(m_pDebugOutputFile,'HWND: %08X Msg: %08X Wparam %08X Lparam %08X n', hwnd,msg,wparam,lparam);
}//EHD IF
fclose(m_pDebugOutputFile); //tudtAttachWin=maatt LRESULT tobjResult= ::CallWindowProc(tudtAttachWinInfo.OldWinProc ,hwnd,msg,wparam,lparam); return tobjResult; }//end function |
[/page] 而在C#的主程序中,我们使用这个DLL挂接explore类的程序窗体,以下给出挂接部分的代码:
int m_hTargetWindow;//要挂接的目标窗体句柄 IntPtr m_hTargetProcess;//目标窗体所属的进程 IntPtr m_hModule; //挂接DLL的句柄
private void Form1_Load(object sender, EventArgs e) { IntPtr tpTemp = IntPtr.Zero, tpTempa = IntPtr.Zero; uint tuntApiRet;
m_hTargetWindow = (int)clsCECoreAPI.FindWindow('Explore', null );//资源管理器 0x0013e800;
//挂接指定的进程窗体消息 IntPtr thCurrentProcess = clsCECoreAPI.GetCurrentProcess(); m_hTargetProcess=IntPtr.Zero ;// (IntPtr) (unchecked((int)0xedd84e4a)); tuntApiRet= clsCECoreAPI.GetWindowThreadProcessId(new IntPtr(unchecked((int) m_hTargetWindow)), ref m_hTargetProcess);
string tstrArgument; tstrArgument = 'Program FilesprocessinjectHookWindowsProcMFCDLL.dll';// HookWindowsProcMFCDLL.dll'; IntPtr tpArg0;
int tintOriginalKMode = clsCECoreAPI.SetKMode(1); int tintOriginalProcPermission = (int)clsCECoreAPI.SetProcPermissions(0xffffffff);
IntPtr tpFuncProc = clsCECoreAPI.GetProcAddress(clsCECoreAPI.GetModuleHandle('coredll.dll'), 'LoadLibraryW');
CallBackInfo tudtCALLBACKINFO;
tpArg0 = clsCECoreAPI.MapPtrToProcess(tstrArgument, thCurrentProcess);
tudtCALLBACKINFO.hProc = m_hTargetProcess;// Proc; tudtCALLBACKINFO.pfn = clsCECoreAPI.MapPtrToProcess(tpFuncProc, m_hTargetProcess); tudtCALLBACKINFO.pvArg0 = tpArg0; m_hModule =new IntPtr(unchecked( (int) clsCECoreAPI.PerformCallBack4(ref tudtCALLBACKINFO,IntPtr.Zero,IntPtr.Zero,IntPtr.Zero ))); //clsCECoreAPI.Sleep(1000);
IntPtr thModule = clsCECoreAPI.LoadLibrary('HookWindowsProcMFCDLL.dll'); tpFuncProc = clsCECoreAPI.GetProcAddress(thModule, 'fnAttachWinProc');
tpArg0 = (IntPtr) m_hTargetWindow;// clsCECoreAPI.MapPtrToProcess(ref thTargetWindow, thCurrentProcess);
tudtCALLBACKINFO.hProc = m_hTargetProcess; tudtCALLBACKINFO.pfn = clsCECoreAPI.MapPtrToProcess(tpFuncProc, m_hTargetProcess); tudtCALLBACKINFO.pvArg0 = tpArg0 ; tuntApiRet = clsCECoreAPI.PerformCallBack4(ref tudtCALLBACKINFO,IntPtr.Zero,IntPtr.Zero,IntPtr.Zero ); //clsCECoreAPI.Sleep(5000); }
[DllImport('HookWindowsProcMFCDLL.dll')] public static extern int fnAttachWinProc(IntPtr ni_hAttatchWin);
[DllImport('HookWindowsProcMFCDLL.dll')] public static extern int fnDetachWinMsgProc(IntPtr ni_hDetachWin); |
取消挂接的代码根据上述代码很容易就可以建立,不再细叙。
注:clsCECoreAPI的函数全是封装的标准CE API,由于这些API在msdn 中都有详细的文档注释,因篇幅所限,不再将代码一一列举.
在执行这个程序时,将模拟器的共享路径设为PC机的桌面,这样模拟器的storage card目录就等同桌面了,点模拟器的开始菜单,选程序,你就可以看到explore窗体的消息都输出到桌面的WinProcInfo.txt文件中了,运行结果如下:
目前本程序只在PPC2003/wm5 for PPC测试通过,由于smartphone系统在编译时使用了和ppc系统不同的机制,内存运作不明,本程序在smartphone上无法正确运行,有好的建议的话请指教一二,谢谢.如对移动设备开发感兴趣可与本人探讨,我的联系方式: missilecat@163.com QQ:85403578,也可加入以下QQ群讨论:24123368。
|