Вопрос ReadConsoleOutput

sergey3695

Ветеран
Модератор
Не пойму почему не работает должным образом.
C++:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <string>

int main()
{
//
    STARTUPINFO info = { sizeof(info) };
    PROCESS_INFORMATION processInfo;
    std::wstring str;
    WCHAR path[MAX_PATH];
    GetModuleFileName(nullptr, path, _countof(path));
    *wcsrchr(path, L'\\') = L'\0';
    wcscat_s(path, L"\\Arc.exe");
    if (CreateProcess(NULL, path, NULL, NULL, FALSE, CREATE_NEW_CONSOLE | CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo))
    {
        do {
            FreeConsole();
            if (AttachConsole(processInfo.dwProcessId)) {
                COORD  bcoord, bpos;
                DWORD num_read;
                PCHAR_INFO buf;
                SMALL_RECT wnd;
                CONSOLE_SCREEN_BUFFER_INFO cinfo;
                HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
                GetConsoleScreenBufferInfo(hConsole, &cinfo);
                wnd = cinfo.srWindow;
                bpos.X = 0;
                bpos.Y = 0;
                bcoord.X = cinfo.srWindow.Right - cinfo.srWindow.Left + 1;
                bcoord.Y = cinfo.srWindow.Bottom - cinfo.srWindow.Top + 1;

                if (buf = (PCHAR_INFO)malloc(bcoord.X * bcoord.Y * sizeof(CHAR_INFO)))
                    if (ReadConsoleOutput(hConsole, buf, bcoord, bpos, &wnd))
                    {
                        int i, j;
                        for (i = wnd.Top; i <= wnd.Bottom; i++)
                        {
                            for (j = wnd.Left; j <= wnd.Right; j++)
                                str = str + buf[i * bcoord.X + j].Char.UnicodeChar;
                        }
                    }
                WaitForSingleObject(processInfo.hProcess, INFINITE);

                TerminateProcess(processInfo.hProcess, 0);
                break;
            }
        } while (ERROR_INVALID_HANDLE == GetLastError());
        ::CloseHandle(processInfo.hProcess);
        ::CloseHandle(processInfo.hThread);
        MessageBox(0, str.c_str(), L"Caption text", MB_OK);
    }

    return 0;
}
 

Вложения

Сверху