|
From: Elias P. <eli...@ao...> - 2001-06-18 21:17:27
|
> I apologize for not being clear. I am coding a small IDE that invokes the GCC compiler, just
> like Dev-C++ does. Dev-C++ catches the output produced by GCC and displays the errors,
> warnings, and other GCC output at the bottom of the program. I would like to know how to do
> just that: invoke GCC from my program, catch the output, and display it. I know how to invoke
> GCC and display any output, but I do not know how to catch the output. Could someone point me
> in the right direction? Thanks in advance.
Hi, i don't know why the source code you posted doesn't work. I'm using basically the same code in my programs. The program below works fine for me under win98, i'm using the code to catch output for another program, but it seems to work with 'gcc' just the same.
I didn't look at your code in detail, but it's almost the same procedure:
Cerate a temporary file, redirect the output from the called command into that file, then read the contents of the temporary file and delete the file.
I compiled with 'gcc -mwindows -O3 -s -o catch.exe catch.c', but should of course work with a dev-c++ project as well.
(Warning: This code may contain all sort of mistakes and bugs)(but i hope it does work)
Elias Pschernig
-- catch.c
#include <windows.h>
void execute(char *command) {
char tempdir[1024];
char tempfile[1024];
STARTUPINFO sui;
PROCESS_INFORMATION pi;
HANDLE capture;
GetTempPath(1024, tempdir);
GetTempFileName(tempdir, "bla", 0, tempfile);
capture=CreateFile(tempfile,GENERIC_READ|GENERIC_WRITE,0,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY,NULL);
memset(&sui,0,sizeof sui);
sui.cb = sizeof sui;
sui.lpTitle = "my redir";
sui.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
sui.hStdOutput = capture;
sui.hStdError = capture;
sui.hStdInput = NULL;
sui.wShowWindow = SW_HIDE;
if(CreateProcess(NULL, command,
NULL, NULL, TRUE, 0, NULL, tempdir, &sui, &pi)) {
char *buffer;
DWORD size;
WaitForSingleObject(pi.hProcess, INFINITE);
SetFilePointer(capture, 0, NULL, FILE_BEGIN);
size = GetFileSize(capture, NULL);
buffer = alloca(size+1);
ReadFile(capture, buffer, size, &size, NULL);
buffer[size] = 0;
CloseHandle(capture);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
DeleteFile(tempfile);
MessageBox(NULL,buffer,"Captured Output",MB_OK);
} else {
MessageBox(NULL,command,"Error: Could not execute",MB_OK);
}
}
int WINAPI WinMain(HINSTANCE hinst,HINSTANCE prev,LPSTR arg,int show) {
execute("gcc -v");
return 0;
}
|