I’m currently working on code in C++ where I’m using WriteConsoleW to display text on the console screen. However, I am facing challenges capturing this displayed text for use or display in other locations.
Additionally, I’m having trouble trying to capture the output of another program that my code runs. Despite setting up channels for input/output redirection and creating a child process with CreateProcess, I think there is a problem in my code. (And a warning, if the main display code is changed, in that original program code I am limited to using only functions from kernel32.dll.)
Any tips on how to capture output from WriteConsoleW and the program would be greatly appreciated.
#include <iostream>
#include <Windows.h>
int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole == INVALID_HANDLE_VALUE) {
std::cerr << "Error in handle console" << std::endl;
return 1;
}
// Texto a ser escrito
const wchar_t* texto = L"Hello, Word!";
DWORD escritos;
// Escreve o texto no console
if (!WriteConsoleW(hConsole, texto, wcslen(texto), &escritos, nullptr)) {
std::cerr << "Write Error" << std::endl;
return 1;
}
system("pause");
return 0;
}