标准输出(stdout)重定向公司有个同事用了一个很老的DLL,其信息都是输出到控制台窗口(console),问能不能将其输出到MFC的view中? 我们知道,标准输出默认情况下是输出到console中。 //将stdout重定向到文件中
FILE *stream ; if((stream = freopen("file.txt", "w", stdout)) == NULL) exit(-1); printf("this is stdout output "); //重新将stdout重定向到console stream = freopen("CON", "w", stdout); printf("And now back to the console once again ");
HANDLE hOutputReadTmp = 0;
HANDLE hOutputWrite = 0; void DisplayError(char* szMsg) { TRACE(szMsg); TRACE(" "); } DWORD WINAPI ReadStdout(LPVOID lpvThreadParam) { CHAR lpBuffer[256]; DWORD nBytesRead; while(TRUE) { if (!ReadFile(hOutputReadTmp, lpBuffer,sizeof(lpBuffer), &nBytesRead,NULL) || !nBytesRead) { if (GetLastError() == ERROR_BROKEN_PIPE) break; // pipe done - normal exit path. else DisplayError("ReadFile"); // Something bad happened. } if (nBytesRead >0 ) { //获取到printf的输出 lpBuffer[nBytesRead] = 0; //replace your code here. Do it yourself. DisplayInView(lpBuffer); } } return 0; } void StdoutRedirect() { if (!CreatePipe(&hOutputReadTmp,&hOutputWrite,0,0)) DisplayError("CreatePipe"); int hCrt; FILE *hf; //AllocConsole(); hCrt = _open_osfhandle( (long)hOutputWrite, _O_TEXT ); hf = _fdopen( hCrt, "w" ); *stdout = *hf; int i = setvbuf( stdout, NULL, _IONBF, 0 ); // Launch the thread that gets the input and sends it to the child. DWORD ThreadID; HANDLE hThread = ::CreateThread(NULL,0,ReadStdout, 0,0,&ThreadID); }
|
|
来自: oskycar > 《c\vc\opencv》