#include #include #include #include int main() { HANDLE parent_read = nullptr; HANDLE parent_write = nullptr; HANDLE child_read = nullptr; HANDLE child_write = nullptr; // 安全属性描述符 SECURITY_ATTRIBUTES sa_out_pipe = { 0 }; sa_out_pipe.nLength = sizeof(SECURITY_ATTRIBUTES); sa_out_pipe.lpSecurityDescriptor = NULL; sa_out_pipe.bInheritHandle = TRUE; constexpr int PipeBuffSize = 1048576; // 创建管道,父进程读-子进程写 BOOL pipe_ret1 = ::CreatePipe(&parent_read, &child_write, &sa_out_pipe, PipeBuffSize); // 创建管道,父进程写-子进程读 BOOL pipe_ret2 = ::CreatePipe(&child_read, &parent_write, &sa_out_pipe, PipeBuffSize); STARTUPINFOA startup_info = { 0 }; // 启动信息结构体 startup_info.cb = sizeof(STARTUPINFO); startup_info.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; startup_info.wShowWindow = SW_HIDE; // 重定向子进程的读写 startup_info.hStdInput = child_read; startup_info.hStdOutput = child_write; startup_info.hStdError = child_write; PROCESS_INFORMATION process_info = { 0 }; // 进程信息结构体 std::string adb = "adb -s 127.0.0.1:5555 shell"; ::CreateProcessA(NULL, const_cast(adb.c_str()), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &startup_info, &process_info); std::string pipe_str; const std::string end_flag = "__END__\r\n"; while (true) { DWORD read_num = 0; DWORD std_num = 0; DWORD write_num = 0; std::string cmd = "screencap"; cmd += "; echo " + end_flag; WriteFile(parent_write, cmd.c_str(), cmd.size(), &write_num, NULL); auto time = std::chrono::system_clock::now(); while (true) { while (::PeekNamedPipe(parent_read, NULL, 0, NULL, &read_num, NULL) && read_num > 0) { //std::cout << read_num << std::endl; char* pipe_buffer = new char[read_num]; BOOL read_ret = ::ReadFile(parent_read, pipe_buffer, read_num, &std_num, NULL); if (read_ret) { pipe_str += std::string(pipe_buffer, std_num); } delete[] pipe_buffer; } if (pipe_str.size() >= end_flag.size() && pipe_str.compare(pipe_str.size() - end_flag.size(), end_flag.size(), end_flag) == 0) { break; } } std::cout << std::chrono::duration_cast(std::chrono::system_clock::now() - time).count() << "ms" << std::endl; std::cout << "size: " << pipe_str.size() << std::endl; pipe_str.clear(); } DWORD ret = -1; ::GetExitCodeProcess(process_info.hProcess, &ret); ::CloseHandle(process_info.hProcess); ::CloseHandle(process_info.hThread); ::CloseHandle(parent_read); ::CloseHandle(parent_write); ::CloseHandle(child_read); ::CloseHandle(child_write); return 0; }