| 发表于:2007-04-05 23:02:401楼 得分:0 |
#include "iostream.h " #include "windows.h " // ========== 定义一个代码结构,本例为一个对话框============ struct mydata { char sz[64]; // 对话框显示内容 dword dwmessagebox; // 对话框的地址 }; // ========== 远程线程的函数 ============================== dword __stdcall rmtfunc(mydata *pdata) { typedef int(__stdcall*mmessagebox)(hwnd,lpctstr,lpctstr,uint); mmessagebox msgbox = (mmessagebox)pdata-> dwmessagebox; msgbox(null, pdata-> sz, null, mb_ok); return 0; } int main(int argc, char* argv[]) { // ===== 获得需要创建remotethread的进程句柄 =============================== hwnd hwnd =::findwindow(null, "换线工具 "); // 选取一个进程做为例子 int x=::getlasterror(); dword dwprocessid; ::getwindowthreadprocessid(hwnd, &dwprocessid); handle hprocess = ::openprocess( process_all_access, false, dwprocessid); x=::getlasterror(); // ========= 代码结构 ================================================ mydata data; zeromemory(&data, sizeof (mydata)); strcat(data.sz, "对话框的内容. "); hinstance huser = loadlibrary( "user32.dll "); if (! huser) { cout < < "can not load library\n "; return 0; } data.dwmessagebox = (dword)getprocaddress(huser, "messageboxa "); freelibrary(huser); if (! data.dwmessagebox) return 0; // ======= 分配空间 =================================================== void *premotethread = virtualallocex(hprocess, 0, 1024*4, mem_commit ¦mem_reserve, page_EXECute_readwrite); x=::getlasterror(); if (! premotethread) return 0; if (! writeprocessmemory(hprocess, premotethread, &rmtfunc, 1024*4, 0)) return 0; mydata *pdata = (mydata*)virtualallocex(hprocess, 0, sizeof (mydata), mem_commit, page_readwrite); if (!pdata) return 0; if (! writeprocessmemory(hprocess, pdata, &data, sizeof (mydata), 0)) return 0; // =========== 创建远程线程 =========================================== handle hthread = createremotethread(hprocess, 0, 0, (lpthread_start_routine)premotethread, pdata, 0, 0); if (! hthread) { cout < < "远程线程创建失败\n "; return 0; } ::waitforsingleobject(hthread,infinite); closehandle(hthread); virtualfreeex(hprocess, premotethread, 1024*3, mem_release); virtualfreeex(hprocess, pdata, sizeof (mydata), mem_release); closehandle(hprocess); cout < < "hello world!\n "; return 0; } | | |
|