远程进程遍历功能实现原理与远程目录传输完全一致,唯一的区别在于远程进程枚举中使用EnumProcess
函数枚举当前系统下所有活动进程,枚举结束后函数返回一个PROCESSENTRY32
类型的容器,其中的每一个成员都是一个进程信息,只需要对该容器进行动态遍历即可得到所有的远程主机列表。
服务端代码如下所示,首先代码中通过read_some
第一次接收到对端进程数量,接着通过第一个循环,将接收到的字符串数据强制转换为PROCESSENTRY32
类型的结构,并将结构存入vector
容器内,第二个循环则用于枚举输出我们整理好的容器列表。
#include <iostream> #include <string> #include <boost/asio.hpp> #include <vector> #include <Windows.h> #include <tlhelp32.h> #include <boost/lexical_cast.hpp>
using namespace boost; using namespace boost::asio; using namespace std;
int main(int argc, char* argv[]) { io_service io_service; ip::tcp::acceptor acceptor(io_service, ip::tcp::endpoint(ip::tcp::v4(), 6666)); ip::tcp::socket socket(io_service); acceptor.accept(socket); boost::system::error_code error_code;
char process_count[32] = { 0 }; socket.read_some(boost::asio::buffer(process_count), error_code); std::cout << "接收到的进程数量: " << process_count << std::endl;
std::vector<PROCESSENTRY32> recv_process_list;
for (int x = 0; x < lexical_cast<int>(process_count); x++) { char recv_buffer[sizeof(PROCESSENTRY32)] = { 0 }; socket.read_some(boost::asio::buffer(recv_buffer, sizeof(PROCESSENTRY32)), error_code);
PROCESSENTRY32 *ptr = (PROCESSENTRY32 *)recv_buffer; recv_process_list.push_back(*ptr); }
for (int x = 0; x < recv_process_list.size(); x++) { std::cout << "进程PID: " << recv_process_list[x].th32ProcessID << "父进程PID: " << recv_process_list[x].th32ParentProcessID << "进程名称: " << recv_process_list[x].szExeFile << std::endl; }
std::system("pause"); return 0; }
|
客户端代码如下所示,其实现原理与文件传输功能完全一致,此处只是更换了一个EnumProcess
函数,其传输方式此处就不在赘述。
#include <iostream> #include <string> #include <boost/asio.hpp> #include <vector> #include <Windows.h> #include <tlhelp32.h> #include <boost/lexical_cast.hpp>
using namespace boost; using namespace boost::asio; using namespace std;
std::vector<PROCESSENTRY32> EnumProcess() { std::vector<PROCESSENTRY32> process_info;
PROCESSENTRY32 pe32 = { 0 }; pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (INVALID_HANDLE_VALUE != hProcessSnap) { BOOL bRet = Process32First(hProcessSnap, &pe32); while (bRet) { process_info.push_back(pe32);
bRet = Process32Next(hProcessSnap, &pe32); } CloseHandle(hProcessSnap); } return process_info; }
int main(int argc, char * argv[]) { io_service io_service; ip::tcp::socket socket(io_service); ip::tcp::endpoint ep(ip::address_v4::from_string("127.0.0.1"), 6666);
boost::system::error_code error_code; socket.connect(ep, error_code);
std::vector<PROCESSENTRY32> send_process_list = EnumProcess();
std::string process_count = lexical_cast<std::string>(send_process_list.size()); std::cout << "当前进程数量: " << process_count << std::endl; socket.write_some(boost::asio::buffer(process_count));
for (int x = 0; x < send_process_list.size(); x++) { char send_buffer[sizeof(PROCESSENTRY32)] = { 0 };
memcpy(send_buffer, &send_process_list[x], sizeof(PROCESSENTRY32)); socket.write_some(boost::asio::buffer(send_buffer, sizeof(PROCESSENTRY32))); }
std::system("pause"); return 0;
}
|
读者可自行编译上述代码片段,并依次运行服务端与客户端,此时读者可清晰的看到目标主机中所运行的完整进程列表。