Front End App

How do you make a front end application for an existing Win32 Console App?
PowerManAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
i suppose it depends what the console app is doing..
is it simple like copy files etc or more difficult like traceroute that returns values , or a fully interactive dos application..

can you tell us what the console app is as might help for ideas.
0
PowerManAuthor Commented:
The console app is BladeEnc.  It is a MP3 encoder.  It shows status info as it is encoding.  It can be downloaded here : http://www.hitsquad.com/smm/programs/BladeEnc_win32/ 
0
simonetCommented:
Here is a part of code which does what you want. Create form with Memo1, Panel1, btnStart, btnStop, btnGrab and give it to try. What it does is to grab the output from the console application and throws it in a TMemo. You can parse this output and do something more useful with it.

You'll have to change the code below so it uses your console EXE with it's command line parameters and whatever it needs.

If you need more info, here's a thread at EE that can hel you quite a lot:

http://www.experts-exchange.com/Q.10199810

Yours,

Alex
Athena's Place: http://www.bhnet.com.br/~simonet 



unit Main1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Panel1: TPanel;
    btnStart: TButton;
    btnStop: TButton;
    btnGrab: TButton;
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure btnGrabClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1 : TForm1;
  hConsole : THandle;
  ProcessInfo : TProcessInformation;

implementation

{$R *.DFM}

uses Console;

type
  cInfo = array[1..MAXINT div SizeOf(TCharInfo)] of TCharInfo;
  pcInfo = ^cInfo;

procedure TForm1.btnStartClick(Sender: TObject);
var
  zAppName:array[0..512] of char;
  zCurDir:array[0..255] of char;
  WorkDir:String;
  StartupInfo:TStartupInfo;
begin
  btnStart.Enabled := false;
  if AllocConsole then
  begin
    StrPCopy(zAppName,'command.com /c dir');
    WorkDir := 'c:\';
    StrPCopy(zCurDir,WorkDir);
    FillChar(StartupInfo,Sizeof(StartupInfo),#0);
    StartupInfo.cb := Sizeof(StartupInfo);
    StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
    StartupInfo.wShowWindow := SW_SHOWDEFAULT;
    if 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_SEPARATE_WOW_VDM or     // creation flags
      NORMAL_PRIORITY_CLASS,
      nil,                    // pointer to new environment block
      nil,                    // pointer to current directory name
      StartupInfo,            // pointer to STARTUPINFO
      ProcessInfo) then       // pointer to PROCESS_INF
    begin
      SetConsoleTitle(PChar('Test Console Application'));
      hConsole := GetStdHandle(STD_OUTPUT_HANDLE);
      btnStop.Enabled := true;
      btnGrab.Enabled := true;
    end
    else
    begin
      btnStart.Enabled := true;
    end;
  end;
end;

procedure TForm1.btnStopClick(Sender: TObject);
var ProcessResult : integer;
begin
  try
    WaitforSingleObject(ProcessInfo.hProcess,INFINITE);  file://wait for DOS
process
    GetExitCodeProcess(ProcessInfo.hProcess,ProcessResult);
    CloseHandle( ProcessInfo.hProcess );
    CloseHandle( ProcessInfo.hThread );
    CloseHandle(hConsole);
    FreeConsole;
    btnStop.Enabled := false;
    btnGrab.Enabled := false;
    btnStart.Enabled := true;
  except
  end;
end;

procedure TForm1.btnGrabClick(Sender: TObject);
var
  hConOut : THandle;
  Dest : TCoord;
  csbi : TConsoleScreenBufferInfo;  // used to get screen size
  i, j : integer;
  s : string;
  GrabBuff : pcInfo;
begin
  btnGrab.Enabled := false;
  Dest.X := 0;
  Dest.Y := 0;
  try
    hConOut := GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConOut, csbi);
    SetLength(s, csbi.dwMaximumWindowSize.X + 1);
    GetMem(GrabBuff, csbi.dwMaximumWindowSize.X *
csbi.dwMaximumWindowSize.Y * sizeof(TCharInfo));
    try
      // read the screen rectangle into the buffer
      ReadConsoleOutput(hConOut,   // output buffer
                        GrabBuff,  // buffer that receives data
                        csbi.dwMaximumWindowSize,  // col/row size of
destination buffer
                        Dest,      // upper-left cell to write data to
                        csbi.srWindow); // screen buffer rectangle to
read from
      file://copy text data from pCharInfo to memo
      Memo1.Clear;
      for i := 0 to csbi.dwMaximumWindowSize.Y - 1 do
      begin
        s := '';
        for j := 1 to csbi.dwMaximumWindowSize.X do
          s := s +
               GrabBuff^[i*csbi.dwMaximumWindowSize.X + j].AsciiChar;
        Memo1.Lines.Add(s);
      end;
    finally
      freemem(GrabBuff);
    end;
  finally
    btnGrab.Enabled := true;
  end;
end;

end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.