Execute a MS-DOS program and wait until finish  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 27/09/99

Tip accessed 1189 times

 


There are several tips in Trucomania that allow to execute other programs from your Deplhi application. They work correctly, only when executing programs MS-DOS, there are two problems: the first one that it don't allow us to give a title to the window where the program, is executed and the second, that there are some programs MS-DOS that when finished, they leave opened the console where they were executed.
To avoid these two problems, it would be enough with creating a file .PIF pointing to the program of MS-DOS that we want to execute, giving him the parameters that we want.
Is this very well if we know the program ahead of time MS-DOS that we will execute but... and if it is to execute any program of MS-DOS?
This tip is here for this.

Operation:
It is to create a temporal PIF file, pointing to the program MS-DOS that we want to execute. The idea is to leave of the data of a file PIF in which we have defined that it closes the window when finishing, and on him, to apply the changes to our pleasure: Title of the window, name of the executable one, directory of work, etc.
The function DOSExe assumes that the work directory is the same one that the directory where it is the file TWO to execut


 procedure TForm1.Button1Click(Sender: TObject);

 Function DOSExe(Fichero,Parametros,Titulo:String):Boolean;

     function WinExecAndWait32(FileName:String; Visibility:integer):integer;
     var
       zAppName:array[0..512] of char;
       zCurDir:array[0..255] of char;
       WorkDir:String;
       StartupInfo:TStartupInfo;
       ProcessInfo:TProcessInformation;
       Resultado,exitCode: DWord;
       Titulo:Array [0..200] of char;
       kk:DWord;
     begin
       StrPCopy(zAppName,FileName);
       GetDir(0,WorkDir);
       StrPCopy(zCurDir,WorkDir);
       FillChar(StartupInfo,Sizeof(StartupInfo),#0);
       StartupInfo.cb := Sizeof(StartupInfo);

       StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
       StartupInfo.wShowWindow := Visibility;
       CreateProcess(nil,
         zAppName,                      { pointer to command line string }
         nil,                           { pointer to process security attributes}
         nil,                           { pointer to thread security attributes}
         false,                         { handle inheritance flag }
         CREATE_NEW_CONSOLE or          { creation flags }
         NORMAL_PRIORITY_CLASS,
         nil,                           { pointer to new environment block }
         nil,                           { pointer to current directory name }
         StartupInfo,                   { pointer to STARTUPINFO }
         ProcessInfo);

       {Espera a que termine la ejecucion}
       {Wait until execution finish}
       Repeat
         exitCode := WaitForSingleObject( ProcessInfo.hProcess,1000);
         Application.ProcessMessages;
         GetExitCodeThread(ProcessInfo.hProcess,kk);

       Until (exitCode <> WAIT_TIMEOUT);
       GetExitCodeProcess(ProcessInfo.hProcess,Resultado);
       MessageBeep(0);
       CloseHandle(ProcessInfo.hProcess );
       Result:=Resultado;
     end; {WinExecAndWait32}

   var {DOSExe}
     f          :file;
     PathPif,
     DirTrabajo,
     DP         :string;  {Datos del Pif}
     ErrorIO       :integer;

   begin
     {Datos de un fichero PIF preparado para Cerrar al Terminar}
     {A PIF file data prepared to 'Close at End'}
     DP:=
     #0#120#84#69#83#84#68#79#126#49#32#32#32#32#32#32#32#32#32#32+
     #32#32#32#32#32#32#32#32#32#32#32#32#128#2#0#0#68#58#92#116#101+
     #115#116#100#111#115#112#114#111#103#114#97#109#116#104#105#110+
     #103#46#101#120#101#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#16#0#101#58#92#116#101#109#112+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#45#101#120+
     #32#100#58#92#116#101#115#116#32#100#58#92#42#46#42#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#1#0#255#25#80#0#0#7#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#77#73+
     #67#82#79#83#79#70#84#32#80#73#70#69#88#0#135#1#0#0#113#1#87#73+
     #78#68#79#87#83#32#51#56#54#32#51#46#48#0#5#2#157#1#104#0#128+
     #2#0#0#100#0#50#0#255#255#0#0#255#255#0#0#2#0#2#0#159#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#45#101#120#32#100#58#92#116#101+
     #115#116#32#100#58#92#42#46#42#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#87+
     #73#78#68#79#87#83#32#86#77#77#32#52#46#48#0#255#255#27#2#172+
     #1#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#80#73#70+
     #77#71#82#46#68#76#76#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#2#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#129#0#0#0#0#0#0#0#0#0#0#0#1#0#0#0#5#0#25#0#3#0#200#0+
     #232#3#2#0#10#0#1#0#0#0#0#0#0#0#28#0#0#0#0#0#0#0#8#0#12#0#84#101+
     #114#109#105#110#97#108#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#76#117#99#105#100#97#32#67#111#110#115#111#108#101+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#3#0#0#0#80#0#25#0#128+
     #2#44#1#0#0#0#0#22#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
     #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#1#0;

     Titulo:=Titulo+StringOfChar(' ',30-Length(Titulo));
     Titulo:=Copy(Titulo,1,30)+#0;
     move(Titulo[1],DP[$02+1],30);

     Fichero:=copy(Fichero,1,63)+#0;
     move(Fichero[1],DP[$24+1],length(Fichero));

     Parametros:=copy(Parametros,1,63)+#0;
     move(Parametros[1],DP[$a5+1],length(Parametros));
     move(Parametros[1],DP[$1c5+1],length(Parametros));

     DirTrabajo:=Copy(extractfilepath(Fichero),1,63)+#0;
     move(DirTrabajo[1],DP[$65+1],length(DirTrabajo));

     Result:=false;
     PathPif:=ExtractFilePath(Application.ExeName)+'UnPif.pif';
     assignfile(f,PathPif);
     rewrite(f,1);
     ErrorIO:=ioresult;
     blockwrite(f,DP[1],length(DP),ErrorIO);
     closefile(f);
     if ErrorIO<>length(DP) then exit;

     WinExecAndWait32(PathPif,Sw_ShowNormal);

     DeleteFile(PathPif);
     Result:=TRUE;
   end; {DOSExe}

 begin
   DOSExe('c:\tools\q.exe','c:\autoexec.bat','Ejemplo de llamada del QEdit');
 end;