Link to home
Start Free TrialLog in
Avatar of SCOTT78
SCOTT78Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Program

Can anyone show me how to run an exe from a delphi program.
Lets say the user clicks a button and it runs Windows Notepad or it runs another program that youve developed.
Any help would be great.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of f15iaf
f15iaf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of geobul
geobul

Hi,

Another way:

uses ShellApi;
...
filename := 'notepad.exe'; // or 'c:\test.txt'
parameters := '';
DefaultDirectory := 'c:\';
ShellExecute(Handle, 'open', PChar(filename), PChar(parameters), PChar(DefaultDirectory), SW_SHOWNORMAL);

Regards, Geo
Replace Handle with 0 (zero):

ShellExecute(0, 'open', PChar(filename), nil, nil, SW_SHOWNORMAL);
Avatar of Mohammed Nasman
Hello

  winexec is old and include with windows 95 and above for compatibly, you can use the CreateProcess to run a program, it's the preferred way to run programs in win32 windows

here's an example with 2 Buttons, one will run the notepad and the other will close it

..
var
  Form1: TForm1;
  ProcInfo   : TProcessInformation;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  StartInfo  : TStartupInfo;
begin
  FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
  StartInfo.cb := SizeOf(TStartupInfo);

  CreateProcess(nil,'c:\windows\notepad.exe', nil, nil,False,
              CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
              nil, nil, StartInfo, ProcInfo);
end;

// to close the program you ran
procedure TForm1.Button2Click(Sender: TObject);
begin
  TerminateProcess(ProcInfo.hProcess,0);
  CloseHandle(ProcInfo.HProcess);
  CloseHandle(ProcInfo.hThread);
end;

Best regards
Mohammed Nasman
well, that are all possibilities, i guess
As far as I know ShellExecute uses CreateProcess internally to do its job.

There is ShellExecuteEx API also.

Regards, Geo
>> you can use the CreateProcess to run a program, it's the preferred way to run programs in win32 windows

Once I said something similar, but then another guy corrected me, saying that CreateProcess does not respect policies, so one should only use it if you need the additional control (compared to ShellExecute(Ex)) urgently, in all other cases one should use ShellExecute(Ex). Not sure whether I do agree to that, but I wanted to mention that anyway...

Regards, Madshi.

I just said that because I saw that in msdn site when i saw looking there

"WinExec
The WinExec function runs the specified application.

Note  This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function."

so i guessed it's the preferred way, but seems that i'm not right

I'm not sure. Microsoft is contradicting themselves, sometimes. I'm using both CreateProcess and ShellExecute(Ex).
It's normal, it's Micro$oft ;)
http://tomflorence.uni.cc/program.jpeg tyhis is the program exe i made.



Code ******************************


implementation

uses ShellAPI;

{$R *.dfm}

procedure TfrmProgramLaunch.btnAArmyClick(Sender: TObject);
begin
  if (edtUser.Text = 'admin') then begin
    if (edtPass.Text = 'admin') then begin
      ShellExecute(frmProgramLaunch.Handle, nil, 'C:\Program Files\Army Operations\System\ArmyOps.exe',
      nil, nil, SW_SHOWNORMAL);
      close;
    end
    else begin
      Application.MessageBox('Sorry wrong Password!','Wrong Login',MB_OK);
    end;
  end
  else begin
    Application.MessageBox('Sorry wrong username!','Wrong Login',MB_OK);
  end;
end;

*********************8 Above is for a password protected open**********************

***********8Below to exe another program***************

procedure TfrmProgramLaunch.btnRb6Click(Sender: TObject);
begin
  ShellExecute(frmProgramLaunch.Handle, nil, 'C:\Program Files\Red Storm Entertainment\RainbowSix.lnk',
  nil, nil, SW_SHOWNORMAL);
  close;
end;
SCOTT78:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept f15iaf's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer