C++/(MFC)stdout redirect Edit Diff Refresh Backlink Random Search History Help Setting Hide Show a.py print ("ABCD 1234") .cpp CString cmd = L"C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe a.py"; CString str; str = RunStdoutRedirect(cmd); AfxMessageBox(str); CString RunStdoutRedirect(CString cmd) { // stdout/stderr 리디렉션 HANDLE hWrite; HANDLE hRead; SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; if (!CreatePipe(&hRead, &hWrite, &sa, 0)) { OutputDebugString(_T("Fail to open pipe.")); return L""; } STARTUPINFO si; memset(&si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdOutput = hWrite; // 표준출력(stdout) 리디렉션 si.hStdError = hWrite; // 표준에러(stderr) 리디렉션 PROCESS_INFORMATION pi; if (!CreateProcess(NULL, cmd.GetBuffer(0), NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &si, &pi)) { OutputDebugString(_T("Fail to create process.")); return L""; } CloseHandle(hWrite); CHAR buffer[512] = {0,}; DWORD bytesRead = 0; CString strStdout; while (ReadFile(hRead, buffer, sizeof(buffer) - 1, &bytesRead, NULL) && bytesRead) { buffer[bytesRead] = '\0'; strStdout = buffer; } CloseHandle(hRead); return strStdout; } ABCD 1234 C++ MFC stdout 이 글에는 0 개의 댓글이 있습니다. Please enable JavaScript to view the comments powered by Disqus. comments powered by Disqus