2 years ago

#38975

test-img

DarkoNaito_09

CreateConsoleScreenBuffer returning invalid but not INVALID_HANDLE_VALUE handle

I'm creating a new console screen buffer and trying to set it as the active screen buffer of a new console created via CreateProcessA(), but when the new console is created it shows something like "Unable to write on specified device."; also, the main console window gets so small that you can only see it's titlebar.

STARTUPINFOA start_info {};

SECURITY_ATTRIBUTES sec_attr {};

sec_attr.bInheritHandle = TRUE;
sec_attr.nLength = sizeof(sec_attr);
sec_attr.lpSecurityDescriptor = NULL;

HANDLE handle = CreateConsoleScreenBuffer(
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_WRITE | FILE_SHARE_READ,
    &sec_attr,
    CONSOLE_TEXTMODE_BUFFER,
    NULL
);

CONSOLE_SCREEN_BUFFER_INFOEX buffer_info {};

buffer_info.cbSize = sizeof(buffer_info);

buffer_info.dwSize.X = 120;
buffer_info.dwSize.Y = 120;

buffer_info.srWindow.Top = 0;
buffer_info.srWindow.Left = 0;

buffer_info.srWindow.Bottom = 119;
buffer_info.srWindow.Right = 119;

SetConsoleScreenBufferInfoEx(
    handle,
    &buffer_info
);

start_info.cb = sizeof(start_info);
start_info.lpTitle = "prova";
start_info.dwFlags = STARTF_USESTDHANDLES;
start_info.hStdOutput = handle;

PROCESS_INFORMATION process_info;

CreateProcessA(
    NULL,
    "cmd",
    &sec_attr,
    NULL,
    TRUE,
    CREATE_NEW_CONSOLE,
    NULL,
    NULL,
    &start_info,
    &process_info
);

c++

windows

winapi

console-application

handle

0 Answers

Your Answer

Accepted video resources