취미는 놀기

[MFC] Consol Redirection 본문

개발 (Develop)/Windows Programmming(MFC)

[MFC] Consol Redirection

취미는놀기 2018. 8. 7. 11:05

Java에서 IO Stream을 이용해서 Consol Redirection을 구현 한적이 있습니다.

 

Source   첨부 드립니다.

 

추후에 제가 사용 할 수 도 있어서 정리 해놓습니다.

 
Atprogram MFC Consol Redirection example

 

void Multi_DownloaderDlg::OnBnClickedBtnDownload()
{
        // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
        Go((LPCTSTR)"atprogram");
}
HANDLE SpawnAndRedirect(LPCTSTR commandLine, HANDLE *hStdOutputReadPipe, LPCTSTR lpCurrentDirectory)
{
        HANDLE hStdOutputWritePipe, hStdOutput, hStdError;
        CreatePipe(hStdOutputReadPipe, &hStdOutputWritePipe, NULL, 0);      // create a non-inheritable pipe
        DuplicateHandle(GetCurrentProcess(), hStdOutputWritePipe,
               GetCurrentProcess(), &hStdOutput,     // duplicate the "write" end as inheritable stdout
               0, TRUE, DUPLICATE_SAME_ACCESS);
        DuplicateHandle(GetCurrentProcess(), hStdOutput,
               GetCurrentProcess(), &hStdError,      // duplicate stdout as inheritable stderr
               0, TRUE, DUPLICATE_SAME_ACCESS);
        CloseHandle(hStdOutputWritePipe);                                                         // no longer need the non-inheritable "write" end of the pipe
        PROCESS_INFORMATION pi;
        STARTUPINFO si;
        ZeroMemory(&si, sizeof(STARTUPINFO));
        si.cb = sizeof(STARTUPINFO);
        si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
        si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);       // (this is bad on a GUI app)
        si.hStdOutput = hStdOutput;
        si.hStdError = hStdError;
        si.wShowWindow = SW_HIDE;                                           // IMPORTANT: hide subprocess console window
        TCHAR commandLineCopy[] = _T("atprogram -t atmelice -s J41800090028 -i updi -d attiny1616 program -f D:\project\program.elf");
        //TCHAR commandLineCopy[1024];                                      // CreateProcess requires a modifiable buffer
        //_tcscpy(commandLineCopy, commandLine);
        if (!CreateProcess(NULL, commandLineCopy, NULL, NULL, TRUE,
               CREATE_NEW_CONSOLE, NULL, lpCurrentDirectory, &si, &pi))
        {
               CloseHandle(hStdOutput);
               CloseHandle(hStdError);
               CloseHandle(*hStdOutputReadPipe);
               *hStdOutputReadPipe = INVALID_HANDLE_VALUE;
               return NULL;
        }
        CloseHandle(pi.hThread);
        CloseHandle(hStdOutput);
        CloseHandle(hStdError);
        return pi.hProcess;
}
void Multi_DownloaderDlg::Go(LPCTSTR commandLine)
{
        HANDLE hOutput, hProcess;
        hProcess = SpawnAndRedirect(commandLine, &hOutput, NULL);
        if (!hProcess) return;
        // (in a real program, you would put the following in a separate thread so your GUI thread is not blocked)
        CHAR buffer[1024];
        DWORD read;
        while (ReadFile(hOutput, buffer, 64, &read, NULL))
        {
               buffer[read] = '\0';
               TRACE(buffer);
        }
        CloseHandle(hOutput);
        CloseHandle(hProcess);
}
Comments