![Avatar of SCOTT78](https://filedb.experts-exchange.com/files/public/2017/5/5/79d61216-0208-45b5-868c-2e635d30732e.png)
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
![Avatar of geobul](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
Replace Handle with 0 (zero):
ShellExecute(0, 'open', PChar(filename), nil, nil, SW_SHOWNORMAL);
ShellExecute(0, 'open', PChar(filename), nil, nil, SW_SHOWNORMAL);
![Avatar of Mohammed Nasman](https://filedb.experts-exchange.com/files/public/2017/0/7/9873c11e-fff6-42e9-b68d-c0c36680df15.jpeg)
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( TStartupIn fo),#0);
FillChar(ProcInfo,SizeOf(T ProcessInf ormation), #0);
StartInfo.cb := SizeOf(TStartupInfo);
CreateProcess(nil,'c:\wind ows\notepa d.exe', nil, nil,False,
CREATE_NEW_PROCESS_GROUP+N ORMAL_PRIO RITY_CLASS ,
nil, nil, StartInfo, ProcInfo);
end;
// to close the program you ran
procedure TForm1.Button2Click(Sender : TObject);
begin
TerminateProcess(ProcInfo. hProcess,0 );
CloseHandle(ProcInfo.HProc ess);
CloseHandle(ProcInfo.hThre ad);
end;
Best regards
Mohammed Nasman
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
var
StartInfo : TStartupInfo;
begin
FillChar(StartInfo,SizeOf(
FillChar(ProcInfo,SizeOf(T
StartInfo.cb := SizeOf(TStartupInfo);
CreateProcess(nil,'c:\wind
CREATE_NEW_PROCESS_GROUP+N
nil, nil, StartInfo, ProcInfo);
end;
// to close the program you ran
procedure TForm1.Button2Click(Sender
begin
TerminateProcess(ProcInfo.
CloseHandle(ProcInfo.HProc
CloseHandle(ProcInfo.hThre
end;
Best regards
Mohammed Nasman
![Avatar of kretzschmar](https://filedb.experts-exchange.com/files/public/2013/11/18/779586ba-5a8d-4e07-83f3-ff272118c129.png)
well, that are all possibilities, i guess
![Avatar of geobul](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
As far as I know ShellExecute uses CreateProcess internally to do its job.
There is ShellExecuteEx API also.
Regards, Geo
There is ShellExecuteEx API also.
Regards, Geo
![Avatar of Madshi](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
>> 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.
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.
![Avatar of Mohammed Nasman](https://filedb.experts-exchange.com/files/public/2017/0/7/9873c11e-fff6-42e9-b68d-c0c36680df15.jpeg)
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
![Avatar of Madshi](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
I'm not sure. Microsoft is contradicting themselves, sometimes. I'm using both CreateProcess and ShellExecute(Ex).
![Avatar of Mohammed Nasman](https://filedb.experts-exchange.com/files/public/2017/0/7/9873c11e-fff6-42e9-b68d-c0c36680df15.jpeg)
It's normal, it's Micro$oft ;)
![Avatar of TomFlorence](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
http://tomflorence.uni.cc/program.jpeg tyhis is the program exe i made.
Code ************************** ****
implementation
uses ShellAPI;
{$R *.dfm}
procedure TfrmProgramLaunch.btnAArmy Click(Send er: TObject);
begin
if (edtUser.Text = 'admin') then begin
if (edtPass.Text = 'admin') then begin
ShellExecute(frmProgramLau nch.Handle , nil, 'C:\Program Files\Army Operations\System\ArmyOps. exe',
nil, nil, SW_SHOWNORMAL);
close;
end
else begin
Application.MessageBox('So rry wrong Password!','Wrong Login',MB_OK);
end;
end
else begin
Application.MessageBox('So rry wrong username!','Wrong Login',MB_OK);
end;
end;
*********************8 Above is for a password protected open**********************
***********8Below to exe another program***************
procedure TfrmProgramLaunch.btnRb6Cl ick(Sender : TObject);
begin
ShellExecute(frmProgramLau nch.Handle , nil, 'C:\Program Files\Red Storm Entertainment\RainbowSix.l nk',
nil, nil, SW_SHOWNORMAL);
close;
end;
Code **************************
implementation
uses ShellAPI;
{$R *.dfm}
procedure TfrmProgramLaunch.btnAArmy
begin
if (edtUser.Text = 'admin') then begin
if (edtPass.Text = 'admin') then begin
ShellExecute(frmProgramLau
nil, nil, SW_SHOWNORMAL);
close;
end
else begin
Application.MessageBox('So
end;
end
else begin
Application.MessageBox('So
end;
end;
*********************8 Above is for a password protected open**********************
***********8Below to exe another program***************
procedure TfrmProgramLaunch.btnRb6Cl
begin
ShellExecute(frmProgramLau
nil, nil, SW_SHOWNORMAL);
close;
end;
![Avatar of CleanupPing](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
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.
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.
![Avatar of geobul](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
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
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
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