|
| 1 | +#pragma once |
| 2 | +#include <windows.h> |
| 3 | +#include <string> |
| 4 | +#include <thread> |
| 5 | +#include <functional> |
| 6 | +#include "../string_utils.h" |
| 7 | +#pragma comment(lib, "rpcrt4.lib") |
| 8 | + |
| 9 | + |
| 10 | +// 全交互式CMD主控端封装 |
| 11 | +class TerminalClient { |
| 12 | +public: |
| 13 | + typedef std::function<void(void* buf, int len)> func_callback; |
| 14 | + typedef std::function<void()> func_failed; |
| 15 | + |
| 16 | + TerminalClient() { |
| 17 | + hCmdProcess_ = NULL; |
| 18 | + hPipeInCMD_ = NULL; |
| 19 | + hPipeOutCMD_ = NULL; |
| 20 | + hJob_ = CreateJobObject(NULL, NULL); |
| 21 | + if (hJob_) { |
| 22 | + JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 }; |
| 23 | + jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; |
| 24 | + SetInformationJobObject(hJob_, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli)); |
| 25 | + } |
| 26 | + } |
| 27 | + ~TerminalClient() { |
| 28 | + if (hCmdProcess_) { |
| 29 | + HANDLE hTmp = hCmdProcess_; |
| 30 | + hCmdProcess_ = NULL; |
| 31 | + TerminateProcess(hTmp, -1); |
| 32 | + CloseHandle(hTmp); |
| 33 | + Sleep(100); |
| 34 | + } |
| 35 | + if (hPipeInCMD_) { |
| 36 | + CloseHandle(hPipeInCMD_); |
| 37 | + } |
| 38 | + if (hPipeOutCMD_) { |
| 39 | + CloseHandle(hPipeOutCMD_); |
| 40 | + } |
| 41 | + if (hJob_) |
| 42 | + CloseHandle(hJob_); |
| 43 | + } |
| 44 | + |
| 45 | + // CmdEmulatorFullPath表示cmd模拟器全路径 |
| 46 | + // cb 表示cmd模拟器输入数据发送回调 |
| 47 | + bool init(const std::string& CmdEmulatorFullPath, const std::string& title, func_callback cb, func_failed fail) { |
| 48 | + if (!cb || CmdEmulatorFullPath.empty()) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + cb_ = cb; |
| 52 | + |
| 53 | + // 1. 创建管道 |
| 54 | + std::string randPipeName = GetGuidString(); |
| 55 | + std::string pipe = std::string("\\\\.\\pipe\\") + randPipeName + "inCMD"; |
| 56 | + HANDLE hPipe = CreateNamedPipeA( |
| 57 | + pipe.c_str(), // pipe name |
| 58 | + PIPE_ACCESS_DUPLEX, // read/write access |
| 59 | + PIPE_TYPE_MESSAGE | // message type pipe |
| 60 | + PIPE_READMODE_MESSAGE | // message-read mode |
| 61 | + PIPE_WAIT, // blocking mode |
| 62 | + PIPE_UNLIMITED_INSTANCES, // max. instances |
| 63 | + 4096, // output buffer size |
| 64 | + 4096, // input buffer size |
| 65 | + 0, // client time-out |
| 66 | + NULL); // default security attribute |
| 67 | + if (hPipe == INVALID_HANDLE_VALUE) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + hPipeInCMD_ = hPipe; |
| 71 | + |
| 72 | + pipe = std::string("\\\\.\\pipe\\") + randPipeName + "outCMD"; |
| 73 | + hPipe = CreateNamedPipeA( |
| 74 | + pipe.c_str(), // pipe name |
| 75 | + PIPE_ACCESS_DUPLEX, // read/write access |
| 76 | + PIPE_TYPE_MESSAGE | // message type pipe |
| 77 | + PIPE_READMODE_MESSAGE | // message-read mode |
| 78 | + PIPE_WAIT, // blocking mode |
| 79 | + PIPE_UNLIMITED_INSTANCES, // max. instances |
| 80 | + 4096, // output buffer size |
| 81 | + 4096, // input buffer size |
| 82 | + 0, // client time-out |
| 83 | + NULL); // default security attribute |
| 84 | + if (hPipe == INVALID_HANDLE_VALUE) { |
| 85 | + CloseHandle(hPipeInCMD_); |
| 86 | + hPipeInCMD_ = NULL; |
| 87 | + return false; |
| 88 | + } |
| 89 | + hPipeOutCMD_ = hPipe; |
| 90 | + |
| 91 | + // 2.创建进程 |
| 92 | + std::string commandLine = CmdEmulatorFullPath; |
| 93 | + commandLine += " "; |
| 94 | + commandLine += randPipeName; |
| 95 | + commandLine += " \""; |
| 96 | + commandLine += title; |
| 97 | + commandLine += "\""; |
| 98 | + PROCESS_INFORMATION pi = { 0 }; |
| 99 | + STARTUPINFOA si = { sizeof(si) }; |
| 100 | + if (CreateProcessA( |
| 101 | + NULL, |
| 102 | + (LPSTR)commandLine.c_str(), //在Unicode版本中此参数不能为常量字符串,因为此参数会被修改 |
| 103 | + NULL, |
| 104 | + NULL, |
| 105 | + FALSE, |
| 106 | + CREATE_NEW_CONSOLE, |
| 107 | + NULL, |
| 108 | + NULL, |
| 109 | + &si, |
| 110 | + &pi)) |
| 111 | + { |
| 112 | + if (hJob_) { |
| 113 | + AssignProcessToJobObject(hJob_, pi.hProcess); |
| 114 | + } |
| 115 | + CloseHandle(pi.hThread); |
| 116 | + //CloseHandle(pi.hProcess); |
| 117 | + hCmdProcess_ = pi.hProcess; |
| 118 | + std::thread t([](func_failed fail, HANDLE*h) { |
| 119 | + WaitForSingleObject(*h, INFINITE); |
| 120 | + if (*h == NULL) |
| 121 | + return; |
| 122 | + fail(); |
| 123 | + }, fail, &hCmdProcess_); |
| 124 | + t.detach(); |
| 125 | + } |
| 126 | + else { |
| 127 | + CloseHandle(hPipeInCMD_); |
| 128 | + hPipeInCMD_ = NULL; |
| 129 | + CloseHandle(hPipeOutCMD_); |
| 130 | + hPipeOutCMD_ = NULL; |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + BOOL fConnected = ConnectNamedPipe(hPipeInCMD_, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); |
| 135 | + if (!fConnected) { |
| 136 | + CloseHandle(hPipeInCMD_); |
| 137 | + hPipeInCMD_ = NULL; |
| 138 | + CloseHandle(hPipeOutCMD_); |
| 139 | + hPipeOutCMD_ = NULL; |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + fConnected = ConnectNamedPipe(hPipeOutCMD_, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); |
| 144 | + if (!fConnected) { |
| 145 | + CloseHandle(hPipeInCMD_); |
| 146 | + hPipeInCMD_ = NULL; |
| 147 | + CloseHandle(hPipeOutCMD_); |
| 148 | + hPipeOutCMD_ = NULL; |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + // 3. 创建读管道线程 |
| 153 | + HANDLE hTmp = hPipeOutCMD_; |
| 154 | + std::thread th([hTmp](func_callback cb) { |
| 155 | + while (true) { |
| 156 | + char szReadBuffer[1024] = { 0 }; |
| 157 | + DWORD cbBytesRead; |
| 158 | + BOOL fSuccess = ReadFile( |
| 159 | + hTmp, // handle to pipe |
| 160 | + szReadBuffer, // buffer to receive data |
| 161 | + 1024, // size of buffer |
| 162 | + &cbBytesRead, // number of bytes read |
| 163 | + NULL); // not overlapped I/O |
| 164 | + if (!fSuccess && GetLastError() == 0x218) { |
| 165 | + Sleep(1000); |
| 166 | + continue; |
| 167 | + } |
| 168 | + if (!fSuccess || cbBytesRead == 0) { |
| 169 | + DisconnectNamedPipe(hTmp); |
| 170 | + break; |
| 171 | + } |
| 172 | + cb(szReadBuffer, cbBytesRead); |
| 173 | + } |
| 174 | + }, cb); |
| 175 | + th.detach(); |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + // 处理远端发过来的数据 |
| 180 | + void recv(const char *buf, int len) { |
| 181 | + auto u = common::string_utils::utf8_to_unicode(std::string(buf, len)); |
| 182 | + auto x = common::string_utils::unicode_to_mutibyte(u, GetOEMCP()); |
| 183 | + DWORD cbWritten = 0; |
| 184 | + BOOL fSuccess = WriteFile( |
| 185 | + hPipeInCMD_, // pipe handle |
| 186 | + x.c_str(), // message |
| 187 | + x.length(), // message length |
| 188 | + &cbWritten, // bytes written |
| 189 | + NULL); // not overlapped |
| 190 | + if (!fSuccess || cbWritten != cbWritten) { |
| 191 | + // If failed, what should I do??? |
| 192 | + } |
| 193 | + } |
| 194 | +private: |
| 195 | + std::string GetGuidString() { |
| 196 | + UUID uuid = { 0 }; |
| 197 | + std::string guid; |
| 198 | + |
| 199 | + // Create uuid or load from a string by UuidFromString() function |
| 200 | + ::UuidCreate(&uuid); |
| 201 | + |
| 202 | + // If you want to convert uuid to string, use UuidToString() function |
| 203 | + RPC_CSTR szUuid = NULL; |
| 204 | + if (::UuidToStringA(&uuid, &szUuid) == RPC_S_OK) |
| 205 | + { |
| 206 | + guid = (char*)szUuid; |
| 207 | + ::RpcStringFreeA(&szUuid); |
| 208 | + } |
| 209 | + return guid; |
| 210 | + } |
| 211 | + |
| 212 | + HANDLE hJob_; |
| 213 | + HANDLE hPipeInCMD_; |
| 214 | + HANDLE hPipeOutCMD_; |
| 215 | + HANDLE hCmdProcess_; |
| 216 | + func_callback cb_; |
| 217 | +}; |
0 commit comments