Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ShellExecute (my program is minimised? why?)

Status
Not open for further replies.

Bamben

Programmer
Jul 22, 2009
70
0
0
IT
Does any one else have this happen to their applications when using ShellExecute? : my program will work fine but, It will be never show (always minimised).


Code:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure Delay(sec: longint);
    procedure FormShow(Sender: TObject);
    function KillTask(ExeFileName: string): Integer;

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Delay(sec: longint);
var
 tc: longint;
begin
 tc :=GetTickCount;
 repeat
  Application.ProcessMessages;
  until ((GetTickCount-tc) >= sec);
end;
function TForm1.KillTask(ExeFileName: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  Result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

  while Integer(ContinueLoop) <> 0 do
  begin
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
      UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
      UpperCase(ExeFileName))) then
      Result := Integer(TerminateProcess(
                        OpenProcess(PROCESS_TERMINATE,
                                    BOOL(0),
                                    FProcessEntry32.th32ProcessID),
                                    0));
     ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
KillTask('PHONE.exe');
Label1.Caption := 'REBOOT MONEYPHONE IN 5 SECONDS';
Delay(1000);
Label1.Caption := 'REBOOT PHONE IN 4 SECONDS';
Delay(1000);
Label1.Caption := 'REBOOT PHONE IN 3 SECONDS';
Delay(1000);
Label1.Caption := 'REBOOT PHONE IN 2 SECONDS';
Delay(1000);
Label1.Caption := 'REBOOT PHONE IN 1 SECOND';
Delay(1000);
ShellExecute(Handle, 'open', 'C:\Documents and Settings\Administrator\Desktop\PHONE\PHONE.exe', nil, nil, SW_SHOWNORMAL) ;
application.terminate;
end;

end.
 
NOTE: it is the exe using this code that remains minimised the entire time not the PHONE.exe
 
You are terminating this app you show us upon showing the form. Which means it's not minimized, it's terminated. This also means it wouldn't show any form at all.

It may still be showing the app on the taskbar due to the resources being held upon show.

Is PHONE.EXE yours or someone else's application? Does it show a form?

Measurement is not management.
 
the phone is my app. I am restarting it. example:

1) the phone.exe is open
2) phone.exe opens (with shellexecute) reboot.exe
3) reboot.exe closes phone.exe instantly
4) reboot.exe waits 5 seconds
5) reboot exe opens phone.exe
6) reboot.exe terminates it self

(the code above is reboot.exe)

You are terminating this app you show us upon showing the form. Which means it's not minimized, it's terminated. This also means it wouldn't show any form at all. It may still be showing the app on the taskbar due to the resources being held upon show.

err, no dude...you misunderstood me; phone.exe gets terminated fine (I found a function on the net called KillTask wich kills the process in the windows task manager, so trust me it gets terminated like wiped out!,
Reboot.exe is the one that remains minimised.
 
I just fixed it... Form1 onShow event was set to do the procedure 'FormShow' so I deleted the event and left OnShow blank, Instead I put the FormShow procedure (Still as an event for Form1) in OnActivate and now it works A.O.K! :D
 
bambem, for a simple task like this, you can write an application without the need of a form. Just put the code in the .dpr file. and if you really *need* a form but you don't want to show it, set it .Visible property to false and put the line ShowMainForm := False as the first line of code in your .dpr file.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
My honest opinion is that you need to be able to walk before you can run.

I think that you should be mastering Pascal first then if you feel competent with that, and only when you do, move on to object programming. Look at the links to learning resources that the others have posted.


Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
I like delphi, lol and running! isnt delphi pascal anyway!?
 
thanks Daddy I wondered what dpr files were
 
Code:
program HelloWorld;
{$APPTYPE CONSOLE}

const
  MyStr: array[0..8] of string = ('one.', 'are', 'I', 'now', 'progamer,', 'spel', 'culdn''t', 'I', 'Onest');
var
  n: integer;
begin
  writeln('Hello World!');
  for n:= length(MyStr)-1 downto 0 do write(#32, MyStr[n]);
  readln;
end.


Roo
Delphi Rules!
 
^^^^^^^^^
[thumbsup]

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top