#include "pch.h" #include "misc.h" #include "usrerr.h" #include #include void RemoveTailLineBreak(TCHAR *pInputStr, int strSize) { size_t length; if (pInputStr) { if (StringCbLength(pInputStr, strSize, &length) == S_OK && length > 0) { if (pInputStr[length - 2] == '\r' && pInputStr[length - 1] == '\n') { pInputStr[length - 2] = pInputStr[length - 1] = 0; } else if (pInputStr[length - 1] == '\n') { pInputStr[length - 1] = 0; } } } } int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) { BOOL bRet; HANDLE hReadPipe = nullptr; HANDLE hWritePipe = nullptr; STARTUPINFO si; PROCESS_INFORMATION pi; SECURITY_ATTRIBUTES securityAttributes = {}; if (pszCmd == nullptr) { SPDLOG_ERROR(TEXT("Input params Error: [{0}]"), pszCmd); return -ERR_INPUT_PARAMS; } if (pszResultBuffer && dwResultBufferSize > 0) { memset(pszResultBuffer, 0, dwResultBufferSize); } memset(&si, 0, sizeof(STARTUPINFO)); memset(&pi, 0, sizeof(PROCESS_INFORMATION)); // 设定管道的安全属性 securityAttributes.bInheritHandle = TRUE; securityAttributes.nLength = sizeof(securityAttributes); securityAttributes.lpSecurityDescriptor = nullptr; // 创建匿名管道 bRet = ::CreatePipe(&hReadPipe, &hWritePipe, &securityAttributes, 0); if (FALSE == bRet) { SPDLOG_ERROR(TEXT("CreatePipe Error")); return -ERR_SYS_CALL; } // 设置新进程参数 si.cb = sizeof(si); si.hStdError = hWritePipe; si.hStdOutput = hWritePipe; si.wShowWindow = SW_HIDE; si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; // 创建新进程执行命令, 将执行结果写入匿名管道中 bRet = ::CreateProcess(nullptr, (pszCmd), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi); if (FALSE == bRet) { SPDLOG_ERROR(TEXT("CreateProcess Error")); return -ERR_CREATE_PROCESS; } // 等待命令执行结束 ::WaitForSingleObject(pi.hThread, INFINITE); ::WaitForSingleObject(pi.hProcess, INFINITE); if (pszResultBuffer) { // 从匿名管道中读取结果到输出缓冲区 ::RtlZeroMemory(pszResultBuffer, dwResultBufferSize); ::ReadFile(hReadPipe, pszResultBuffer, dwResultBufferSize, nullptr, nullptr); } // 关闭句柄, 释放内存 ::CloseHandle(pi.hThread); ::CloseHandle(pi.hProcess); ::CloseHandle(hWritePipe); ::CloseHandle(hReadPipe); RemoveTailLineBreak(pszResultBuffer, dwResultBufferSize); //pszResultBuffer[dwResultBufferSize - 1] = 0; return ERR_SUCCESS; } void ShowWindowsErrorMessage(const TCHAR *pMsgHead) { LPVOID buf; if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK, nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, nullptr)) { SPDLOG_ERROR("{0} Error({1}): {2}", pMsgHead, GetLastError(), buf); LocalFree(buf); } else { SPDLOG_ERROR("{0} Unknown Error{1}.", pMsgHead, GetLastError()); } } void StringReplaceAll(TCHAR *pOrigin, const TCHAR *pOldStr, const TCHAR *pNewStr) { using namespace std; const int maxSize = lstrlen(pOrigin); string src = string(pOrigin); for (string::size_type pos(0); pos != string::npos; pos += lstrlen(pNewStr)) { if ((pos = src.find(pOldStr, pos)) != string::npos) { src.replace(pos, lstrlen(pOldStr), pNewStr); } else { break; } } memset(pOrigin, 0, maxSize); StringCbCopyA(pOrigin, maxSize, src.c_str()); } void StringRemoveAll(TCHAR *pOrigin, const TCHAR *pString) { StringReplaceAll(pOrigin, pString, ""); }