2008/7/29 Seth Grover <[EMAIL PROTECTED]>: > hm. if i compile with -WC, then it opens up a separate console window > (even though I also have "windows gui application", -WG, checked) and > the writeln goes to that console window. > > that's not what i want, though... if i run from the command line, i > want to see the messages written to the console. if i just run the > executable from the windows gui, i don't want to see a console, but i > don't want the writeln's to raise an exception.
you can find out whether you program is run from the console by using the GetStdHandle function. it returns 0 if it's a gui app. see: http://msdn.microsoft.com/en-us/library/ms683231(VS.85).aspx it's tricky to do, but you can detect and attach to the console an app is run from, but you can't use writeln. look at this code (not tested on fpc): program guiconsole; uses Forms, Windows, SysUtils, main in 'main.pas' {Form1}; {$R *.res} const ATTACH_PARENT_PROCESS = DWORD(-1); function AttachConsole(dwProcessId: DWORD): BOOL; stdcall; external kernel32 name 'AttachConsole'; var h: THandle; dum: Cardinal; msg1: String = 'You''re running this from a command window!'; begin h := GetStdHandle(STD_OUTPUT_HANDLE); if h = 0 then begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end else if h <> INVALID_HANDLE_VALUE then begin AttachConsole(ATTACH_PARENT_PROCESS); h := GetStdHandle(STD_OUTPUT_HANDLE); if (h = 0) or (h = INVALID_HANDLE_VALUE) then MessageBox(0, PChar(Format('GetStdHandle error! %d', [h])), 'Error', MB_OK); sleep(1000); if not WriteConsole(h, pchar(msg1), length(msg1), dum, nil) then MessageBox(0, PChar(Format('WriteConsole error! %d', [h])), 'Error', MB_OK); { AllocConsole(); Writeln('You''re running this from a command window!'); } end else MessageBox(0, PChar(Format('GetStdHandle error! %d', [h])), 'Error', MB_OK); end. using writeconsole is a pain, though. i don't know if there's any way of making fpc use the attached console as stdout. i'll let you experiment... henry _______________________________________________ Lazarus mailing list Lazarus@lazarus.freepascal.org http://www.lazarus.freepascal.org/mailman/listinfo/lazarus