delphi - How do I write to StdOut from a GUI app started from the command line? -


i writing standard windows app in delphi 7.

if writing console app, can call following output cmd line or output file.

writeln('some info'); 

if standard gui app have started command line error.

i/o error 105 

there must simple solution problem. want app have 2 modes, gui mode , non-gui mode. how set correctly can write cmd window?

this question similar (if not same) trying accomplish. wanted detect if app executed cmd.exe , send output parent console, otherwise display gui. answers here helped me solve issue. here code came experiment:

parentchecker.dpr

program parentchecker;  uses   vcl.forms,   sysutils,   psapi,   windows,   tlhelp32,   main in 'main.pas' {frmparentchecker};  {$r *.res}  function attachconsole(dwprocessid: integer): boolean; stdcall; external 'kernel32.dll'; function freeconsole(): boolean; stdcall; external 'kernel32.dll';  function getparentprocessname(): string; const   buffersize = 4096; var   handlesnapshot: thandle;   entryparentproc: tprocessentry32;   currentprocessid: thandle;   handleparentproc: thandle;   parentprocessid: thandle;   parentprocessfound: boolean;   parentprocpath: string; begin   parentprocessfound:=false;   handlesnapshot:=createtoolhelp32snapshot(th32cs_snapprocess,0);   if handlesnapshot<>invalid_handle_value   begin     entryparentproc.dwsize:=sizeof(entryparentproc);     if process32first(handlesnapshot,entryparentproc)     begin       currentprocessid:=getcurrentprocessid();       repeat         if entryparentproc.th32processid=currentprocessid         begin           parentprocessid:=entryparentproc.th32parentprocessid;           handleparentproc:=openprocess(process_query_information or process_vm_read,false,parentprocessid);           if handleparentproc<>0           begin             parentprocessfound:=true;             setlength(parentprocpath,buffersize);             getmodulefilenameex(handleparentproc,0,pchar(parentprocpath),buffersize);             parentprocpath:=pchar(parentprocpath);             closehandle(handleparentproc);           end;           break;         end;       until not process32next(handlesnapshot,entryparentproc);     end;     closehandle(handlesnapshot);   end;   if parentprocessfound result:=parentprocpath   else result:=''; end;  function isprime(n: integer): boolean; var   i: integer; begin   result:=false;   if n<2 exit;   result:=true;   if n=2 exit;   i:=2;   while i<(n div + 1)   begin     if (n mod i)=0     begin       result:=false;       exit;     end;     inc(i);   end; end;  var   i: integer;   parentname: string;  begin   parentname:=getparentprocessname().tolower;   delete(parentname,1,parentname.lastindexof('\')+1);   if parentname='cmd.exe'   begin     attachconsole(-1);     writeln('');     i:=1 100 if isprime(i) writeln(inttostr(i)+' prime');     freeconsole();   end   else   begin     application.initialize;     application.mainformontaskbar:=true;     application.createform(tfrmparentchecker, frmparentchecker);     frmparentchecker.label1.caption:='executed '+parentname;     application.run;   end; end. 

main.pas (form label):

unit main;  interface  uses   winapi.windows, winapi.messages, system.sysutils, system.variants, system.classes, vcl.graphics,   vcl.controls, vcl.forms, vcl.dialogs, vcl.stdctrls, rzlabel;  type   tfrmparentchecker = class(tform)     label1: tlabel;   private     { private declarations }   public     { public declarations }   end;  var   frmparentchecker: tfrmparentchecker;  implementation  {$r *.dfm}  end. 

this allows me run gui app command prompt , display output same console app launched. otherwise, run full gui part of app.

example output console window:

i:\delphi\tests , demos\parentchecker\win32\debug>start /wait parentchecker.exe  2 prime 3 prime 5 prime 7 prime 11 prime 13 prime 17 prime 19 prime 23 prime 29 prime 31 prime 37 prime 41 prime 43 prime 47 prime 53 prime 59 prime 61 prime 67 prime 71 prime 73 prime 79 prime 83 prime 89 prime 97 prime  i:\delphi\tests , demos\parentchecker\win32\debug> 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -