#include <windows.h>
#include <stdio.h>
int main() {
HANDLE hPipeRead, hPipeWrite;
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
if (!CreatePipe(&hPipeRead, &hPipeWrite, &sa, 0)) {
return GetLastError();
}
STARTUPINFO si = { sizeof(STARTUPINFO) };
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdOutput = hPipeWrite;
PROCESS_INFORMATION pi;
if (!CreateProcess(NULL, "your_command_here", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
return GetLastError();
}
CloseHandle(hPipeWrite);
char buffer[4096];
DWORD bytesRead;
std::string output;
while (ReadFile(hPipeRead, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0) {
output.append(buffer, bytesRead);
}
CloseHandle(hPipeRead);
// `output` に取り込んだ画面出力が格納されています
// プロセスの終了を待つ
WaitForSingleObject(pi.hProcess, INFINITE);
// ハンドルを閉じる
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}