技术标签:挂起方式创建进程 内核句柄表 安全属性
程序工能说明:通过process.exe创建出两个进程(run.exe ie.exe),实现通过run.exe控制ie。将run.cpp编译后将生成的exe路径复制process.cpp的相应位置,编译process.cpp并运行完成整个Demo测试,观察运行的结果。(一个cpp一个项目不要写到一个项目内)
工具: vs 2008
*字符集:使用多字节字符集(不用会报错)
// process.cpp #include "stdafx.h" #include <Windows.h> int _tmain(int argc, _TCHAR* argv[]) { char szBuffer[256] = {0}; char szHandle[8] = {0}; SECURITY_ATTRIBUTES ie_sa_p = {0};// ie_sa_p.nLength = sizeof(ie_sa_p); ie_sa_p.lpSecurityDescriptor = NULL; ie_sa_p.bInheritHandle = TRUE;//默认为flase 设置为true则句柄表可继承 SECURITY_ATTRIBUTES ie_sa_t = {0}; ie_sa_t.nLength = sizeof(ie_sa_t); ie_sa_t.lpSecurityDescriptor = NULL; ie_sa_t.bInheritHandle = TRUE; STARTUPINFO ie_si = {0}; PROCESS_INFORMATION ie_pi; ie_si.cb = sizeof(ie_si); TCHAR szCmdLine[] = TEXT("C:\Program Files (x86)\Internet Explorer\iexplore.exe www.baidu.com"); //第三与第四个属性为安全属性,可以简单的理解为未设置为true时当前创建线程、进程的内核句柄表不可被继承 //打开IE CreateProcess( NULL, szCmdLine, &ie_sa_p,//进程安全属性 进程表可被继承 &ie_sa_t,//线程安全属性 线程表可被继承 FALSE,//为true时代表此进程可被继承 CREATE_NEW_CONSOLE, NULL, NULL, &ie_si, &ie_pi); sprintf(szHandle, "%x %x", ie_pi.hProcess, ie_pi.hThread); sprintf(szBuffer, TEXT("run.exe %s"), szHandle);//拼接cmdLine 将进程和线程的参数拼接到run.exe STARTUPINFO si = {0}; PROCESS_INFORMATION pi = {0}; si.cb = sizeof(si); //创建进程run.exe控制之前创建的ie CreateProcess( NULL, szBuffer, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); return 0; }
// run.cpp #include "stdafx.h" #include <Windows.h> int _tmain(int argc, _TCHAR* argv[]) { DWORD dwProcessHandle = -1; DWORD dwThreadHandle = -1; char szBuffer[256] = {0}; //接收命令行参数 memcpy(szBuffer, argv[2], 8); sscanf(szBuffer, "%x", &dwThreadHandle); memcpy(szBuffer, argv[1], 8); sscanf(szBuffer, "%x", &dwProcessHandle); printf("获取IE主线程句柄 %0x ", dwThreadHandle); printf("获取IE进程句柄 %0x ", dwProcessHandle); Sleep(5000); //挂起主线程 ::SuspendThread((HANDLE)dwThreadHandle); printf("挂起主线程 %x ", dwThreadHandle); Sleep(10000); //恢复主线程 ::ResumeThread((HANDLE)dwThreadHandle); printf("恢复主线程 "); Sleep(5000); //关闭ID进程 ::TerminateProcess((HANDLE)dwProcessHandle, 1); ::WaitForSingleObject((HANDLE)dwProcessHandle, INFINITE); printf("ID进程已经关闭。。。。 "); Sleep(1000); return 0; }
---建议先了解下多线程与创建进程在学习此文章