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
Retrieved from http://hyacinth.byus.net/moniwiki/wiki.php/C++/(MFC)stdout redirect
last modified 2018-11-02 15:38:39