Board index » delphi » Executing a windows application from a DOS prompt

Executing a windows application from a DOS prompt

Hello,

I am developing a Windows application in the Delphi 5 programming
language.  I would like to have the behavior such that when
I call/execute the program from a DOS box and the Windows application
terminates, that it return back to the text mode (80x25) that
the DOS program was in prior to the call. My windows program
is not a console application, but has a GUI.  Is there any Win32
call that can be made on exit that returns back to the DOS prompt.
Otherwise the user will have to press Alt-Tab to regain focus
of the DOS prompt.

Thanks

Mike McWhinney
elja, Inc.

 

Re:Executing a windows application from a DOS prompt


Quote
Mike McWhinney wrote in message

<2aff3eb5.0306111603.58395...@posting.google.com>...

Quote
>I am developing a Windows application in the Delphi 5 programming
>language.  I would like to have the behavior such that when
>I call/execute the program from a DOS box and the Windows application
>terminates, that it return back to the text mode (80x25) that
>the DOS program was in prior to the call. My windows program
>is not a console application, but has a GUI.  Is there any Win32
>call that can be made on exit that returns back to the DOS prompt.
>Otherwise the user will have to press Alt-Tab to regain focus
>of the DOS prompt.

Easiest solution: don't run your console programs full-screen.
Otherwise, add some extra code somewhere so that when one random
program terminates, focus is set to some other random program.
You may find it quite difficult to achieve this, because as far
as Windows is concerned, both _are_ random programs.

Groetjes,
Maarten Wiltink

Re:Executing a windows application from a DOS prompt


Quote
> Easiest solution: don't run your console programs full-screen.
> Otherwise, add some extra code somewhere so that when one random
> program terminates, focus is set to some other random program.
> You may find it quite difficult to achieve this, because as far
> as Windows is concerned, both _are_ random programs.

Your dos program is a real dos program, or is it written in dephi as a
console app?

If it's a console app, it can call windows api functions like CreateProcess
and wait on the process to finish (see http://kleahy.dyndns.org/delphi -
waitfor.zip).  Then, you can set focus back to your own dos window (I don't
remember which function you need to call to get the console windows' window
handle - maybe GetCurrentConsole... or something like that?).
Unfortunately, you probably can't do this in newer versions of windows,
since they only allow the app that had the focus to set the focus to another
window, iirc.

Kelly

Re:Executing a windows application from a DOS prompt


Quote
"Kelly Leahy" <kellyle...@swbell.net> wrote in message <news:Bd0Ga.1813$3q.109@newssvr32.news.prodigy.com>...
> > Easiest solution: don't run your console programs full-screen.
> > Otherwise, add some extra code somewhere so that when one random
> > program terminates, focus is set to some other random program.
> > You may find it quite difficult to achieve this, because as far
> > as Windows is concerned, both _are_ random programs.

> Your dos program is a real dos program, or is it written in dephi as a
> console app?

> If it's a console app, it can call windows api functions like CreateProcess
> and wait on the process to finish (see http://kleahy.dyndns.org/delphi -
> waitfor.zip).  Then, you can set focus back to your own dos window (I don't
> remember which function you need to call to get the console windows' window
> handle - maybe GetCurrentConsole... or something like that?).
> Unfortunately, you probably can't do this in newer versions of windows,
> since they only allow the app that had the focus to set the focus to another
> window, iirc.

> Kelly

My DOS program is a real mode DOS program.  I tried writing a Delphi
console application which calls the graphical interface program also
written in
Delphi.  It still does not return control to the DOS text mode.  If I
run it in "windowed" mode instead of full screen, then it works
correctly.

Maybe there is a way to stuff the keyboard buffer with a VK_ENTER so
that
the keypress is sent to Windows and therefore bringing back the active
Windows application to focus. I know it's a kludge, but I cannot think
of a better
way right now.

Any other ideas?

Mike

Re:Executing a windows application from a DOS prompt


Quote
Mike McWhinney wrote in message

<2aff3eb5.0306121229.6b821...@posting.google.com>...
[...]

Quote
>My DOS program is a real mode DOS program.

How awful.

Quote
>                                            I tried writing a Delphi
>console application which calls the graphical interface program also
>written in
>Delphi.  It still does not return control to the DOS text mode.  If I
>run it in "windowed" mode instead of full screen, then it works
>correctly.

That's what I tried to tell you. You can start one program from another,
but Windows doesn't want the latter program to know how it was started.
And Windows most certainly does not want to go to full-screen mode by
anything else than explicit user request, so it simply won't.

Quote
>Maybe there is a way to stuff the keyboard buffer with a VK_ENTER so
>that
>the keypress is sent to Windows and therefore bringing back the active
>Windows application to focus. I know it's a kludge, but I cannot think
>of a better
>way right now.

>Any other ideas?

There are two ways to say this. The first is to work _with_ the system,
not against it. The other is more succinct. Don't pee into the wind.

It's just not how Windows works. You can fix it one way (from the
calling application), but not the other way. You can try to hack it,
and predictably have to hack it anew with every new version of
Windows, or you can accept the fact that for better or for worse,
Windows is a graphical environment.

Groetjes,
Maarten Wiltink

Re:Executing a windows application from a DOS prompt


Quote
"Mike McWhinney" <elja...@ameritech.net> wrote in message

news:2aff3eb5.0306121229.6b821a93@posting.google.com...

Quote

> My DOS program is a real mode DOS program.  I tried writing a Delphi
> console application which calls the graphical interface program also
> written in
> Delphi.  It still does not return control to the DOS text mode.  If I
> run it in "windowed" mode instead of full screen, then it works
> correctly.

> Maybe there is a way to stuff the keyboard buffer with a VK_ENTER so
> that
> the keypress is sent to Windows and therefore bringing back the active
> Windows application to focus. I know it's a kludge, but I cannot think
> of a better
> way right now.

Straddling dos and windows is always a problem, they just
try and pretend neither exists. Is it really necessary?

Redirecting focus and foreground is also a problem, users will find any
choice
wrong at some time and user actions will cause something to go wrong
at some time.

It is always annoying when you type or click and some app pops up
and runs away with the input.

This seems to work fairly reliably, for me, on friday 13th, with a full
moon.

program NEWSTART;
{$apptype console}
uses
  windows;

  {$R *.RES}

// Run program and wait for it to finish, return exitcode
// from FAQ??
function RunAndWait(const FileName: String): DWORD;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  Result := STILL_ACTIVE;
  GetStartupInfo(StartupInfo);
  StartupInfo.wShowWindow := SW_SHOWMINNOACTIVE;
  if not CreateProcess(nil, PChar(FileName), nil, nil, IsConsole,
    NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
    writeln('error creating process '+filename);
    //RaiseLastWin32Error;
  try
    if WaitForSingleObject(ProcessInfo.hProcess, INFINITE) = WAIT_OBJECT_0
then
      GetExitCodeProcess(ProcessInfo.hProcess, Result);
  finally
    CloseHandle(ProcessInfo.hThread);
    CloseHandle(ProcessInfo.hProcess);
  end;
end;

procedure help;
begin
  writeln('NEWSTART:');
  writeln('  Launch app, wait until closed then return errorcode');
  writeln(' NEWSTART  [ ? | \? | /? ]  -> help');
  writeln(' NEWSTART   \e  #        -> return # as errorlevel');
  writeln(' NEWSTART   executable  <params> -> run (normal) and wait');
  writeln(' NEWSTART   NEWSTART  \e 3  -> example');

end;

var s:string;
var i,code:integer;
var w:thandle;
var wasfullscreen:boolean;
begin
  exitcode:=0;
  if paramcount=0
  then
    help
  else
    begin
      s:=paramstr(1);
      if (s='?') or (s='/?') or (s='\?')
      then
        begin
           help;
        end
      else
        if (s='\E') or (s='\e')
          or(s='/E') or (s='/e')
        then
        begin
          val(paramstr(2),i,code);
          if code = 0 then exitcode:=i else exitcode:=0;
        end
        else
        begin
          w:=getforegroundwindow;
          wasfullscreen:=isiconic(w);
          exitcode:=runandwait(s);
          setforegroundwindow(w);
          if wasfullscreen
          then begin
                openicon(w);
                showwindow(w,SW_RESTORE);
             end;
        end;
    end;
end.

Other Threads