Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

Execute an external program from a delphi application

I need to know what code should I use to run an external windows application from a delphi program

ex: if a user click on a button, then the application will launch wordpad for exemple or anyother program...

Can you help me? I already tried with
ExecProgram('wordpad.exe', 1);

but it doesn't work... or I must use a library I don't know...

Thanks

Patrick
0
pasta
Asked:
pasta
  • 2
1 Solution
 
mheacockCommented:
Try
    WinExec (Delphi 1)
or
    CreateProcess  (Delphi 2+)
0
 
mheacockCommented:
Did this help?  If you could grade this it would be appreciated.  Thanks.  If you need further assistance, just add a comment and I will answer shortly.
0
 
maurikCommented:
procedure TForm3.Button1Click(Sender: TObject);
var
  Inst: THandle;
  PName: PChar;
  CommandLine: String;
begin
  PName := StrAlloc(101);
{Variable CommandLine will contain the windoows exe path and name}
  CommandLine := "c:\windows\write.exe";
  StrPCopy(PName,CommadLine);
  Inst := WinExec(PName, SW_NORMAL);
  while GetModuleUsage(Inst) > 0 do begin { is app finished? }
    Application.ProcessMessages; { Don't hog the system! }
    { Anything else you might want to do goes here }
  end;
  StrDispose(PName);
end;

This work with Delphi 1.0 . With Delphi 2.0 the procedure is as follows

procedure TForm3.Button1Click(Sender: TObject);
var
  Inst: THandle;
  PName: PChar;
  CommandLine: String;
begin
  PName := StrAlloc(101);
{Variable CommandLine will contain the windoows exe path and name}
  CommandLine := "c:\windows\write.exe";
  StrPCopy(PName,CommadLine);
  Inst := WinExec(PName, SW_NORMAL);
  StrDispose(PName);
end;

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now