2016-08-04 144 views
0

實際上,我有一個自定義應用程序,並希望將某些值傳遞給該.exe,之後它應該在某些事件前執行。它應該自動生成文件。使用delphi應用程序傳遞參數打開外部應用程序

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窗體

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 

有什麼辦法,我可以打開這個應用程序(下文提供的圖像),並通過一些值到它的文本框,然後點擊它的按鈕? enter image description here

此應用程序不支持命令行。不幸的是,我只有它的.exe,而不是代碼。

+3

使用UI自動化將其自動化。 –

回答

2

所有的冷杉都不使用ShellExecute,因爲你不知道結果。你可以使用

// 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; 

如果應用程序不支持命令行參數,你可以自動化你想要的東西。

例如:運行應用程序想要等待其窗口出現。然後枚舉子窗口,然後當您有句柄時,您可以執行所有操作:設置文本,單擊按鈕等。

相關問題