vc++調用exe獲取輸出信息
阿新 • • 發佈:2019-02-06
fail == stdout 使用 put nload 調用exe VS2017 標準
目的
調用命令行程序,返回結果。
思路
把命令行結果輸入到管道中,exe的輸出信息都存在了strOutput這個變量裏。
實現代碼
CString strCmd = L"yara64.exe C:\\Users\\Windows32\\Downloads\\yara-master\\yara-master\\windows\\vs2017\\x64\\Debug\\yara.txt ";//cmd命令 strCmd = strCmd + pid; SECURITY_ATTRIBUTES sa; HANDLE hRead, hWrite; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; //使用系統默認的安全描述符 sa.bInheritHandle = TRUE; //創建的進程繼承句柄 if (!CreatePipe(&hRead, &hWrite, &sa, 0)) //創建匿名管道 { MessageBox(L"CreatePipe Failed!", L"提示", MB_OK | MB_ICONWARNING); return 0; } STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); GetStartupInfo(&si); si.hStdError = hWrite; si.hStdOutput = hWrite; //新創建進程的標準輸出連在寫管道一端 si.wShowWindow = SW_HIDE; //隱藏窗口 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; if (!CreateProcess(NULL, strCmd.GetBuffer(), NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi)) //創建子進程 { MessageBox(L"CreateProcess Failed!", L"提示", MB_OK | MB_ICONWARNING); return 0; } CloseHandle(hWrite); //關閉管道句柄 char buffer[4096] = { 0 }; CString strOutput; DWORD bytesRead; while (true) { if (ReadFile(hRead, buffer, 4095, &bytesRead, NULL) == NULL) //讀取管道 break; strOutput += buffer; UpdateWindow(); Sleep(100); } CloseHandle(hRead);
vc++調用exe獲取輸出信息