I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Open external application with passing parameters using delphi application

Actually I have one custom application and wanted to pass some values to that .exe and after that it should get executed on some event ex. it should generate files automatically.

unit fExecuteExe;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  filename: String;
begin
  filename := 'C:\Testsrc\MojaveD5\Tools\AliasToDataview\Alias2DV.exe';
  ShellExecute(handle,'open',PChar(filename), '','',SW_SHOWNORMAL);
end;

end.

Delphi Form

object Form1: TForm1
  Left = 179
  Top = 116
  Width = 495
  Height = 294
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 176
    Top = 96
    Width = 113
    Height = 25
    Caption = 'Run Alias2Dataview'
    TabOrder = 0
    OnClick = Button1Click
  end
end

Is there any way that I can open this application (image provided below) and pass some values to its textboxes and click it's buttons?enter image description here

This application is not supporting command line. And unfortunately I only have it's .exe, not code.

Upvotes: 0

Views: 9690

Answers (1)

Edijs Kolesnikovičs
Edijs Kolesnikovičs

Reputation: 1695

Firs of all do not use ShellExecute, because you do not know the result. You could use

// Runs application and returns PID. 0 if failed.
function RunApplication(const AExecutableFile, AParameters: string;
  const AShowOption: Integer = SW_SHOWNORMAL): Integer;
var
  _SEInfo: TShellExecuteInfo;
begin
  Result := 0;
  if not FileExists(AExecutableFile) then
    Exit;

  FillChar(_SEInfo, SizeOf(_SEInfo), 0);
  _SEInfo.cbSize := SizeOf(TShellExecuteInfo);
  _SEInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  // _SEInfo.Wnd := Application.Handle;
  _SEInfo.lpFile := PChar(AExecutableFile);
  _SEInfo.lpParameters := PChar(AParameters);
  _SEInfo.nShow := AShowOption;
  if ShellExecuteEx(@_SEInfo) then
  begin
    WaitForInputIdle(_SEInfo.hProcess, 3000);
    Result := GetProcessID(_SEInfo.hProcess);
  end;
end;

If application supports no command line parameters, you could automatize what you want.

For example: Run application want wait for its window to appear. Then enumerate child windows, then when you have handles, you can do everything: set text, click buttons, etc.

Upvotes: 4

Related Questions