Call external EXE file

The method
      uses the WinProcs;    
   ...... 
     the begin    
      WinExec (......);    
     ...... 
      End .    
    And Pascal library function call is no different.   
Example: WinExec ( ' D: \ GoldPacs \ GoldConfig.exe ' , sw_normal); 


Method II 

using ShellExecute better USES added SHELLAPI in section, such as the use of: 
   performing "c: \ myapp \ myapp.exe" parameter " - S " 
   the ShellExecute (handle, ' open ' , ' C: \ MyApp \ MyApp.exe ' , ' -s ' , '' , SW_SHOWNORMAL); 
   first parent window handle parameter; 
   second parameter is open (oPEN , PRINT, two);nil ) 
   The fifth parameter is the initial implementation of the file directory starts running; the 
   sixth parameter for the executable file for the operating mode (SW_HIDE, SW_MAXIMIZE, SW_MINIMIZE, 
SW_RESTORE, SW_SHOW, SW_SHOWDEFAULT, SW_SHOWMAXIMIZED, SW_SHOWMINIMIZED, 
SW_SHOWMINNOACTIVE, SW_SHOWNA, SW_SHOWNOACTIVATE, SW_SHOWNORMAL ); 
   see specific help. Further, if a character string or a variable which contains the necessary operations with PCHAR () function. 

Transfer: HTTP: // www.cnread.net/cnread1/dnwl/cxsj/delphi/ztwd/cxyy/053.htm 

ShellExecute 
ShellExecute function prototypes and parameters are as follows: 
ShellExecute ( 
HWND hwnd, // Parent window handle 
LPCSTR lpOperation, / / operation type 
LPCSTR lpFile, // // specify the parameters to be passed is when lpOperation "explore", is usually set to NULL 
LPCSTR lpDirectory, // to be a file or path operation 
LPCSTR lpParameters,Specify the default directory, usually set to NULL 
INT nShowCmd // file open way, in the usual way or maximized or minimized display 
) 
examples: 
// call calculator 
ShellExecute (NULL, "open", "calc.exe", NULL, NULL, SW_SHOWNORMAL);
 // call Notepad 
ShellExecute (NULL, "Open", "NOTEPAD.EXE", NULL, NULL, SW_SHOWNORMAL); v
 & # 9679 ; hWnd: used to specify the parent window handle. When an error occurs during a function call, it will serve as the parent window Windows message window. For example, it may be set to the main application window handle, i.e. Application.Handle, may be provided (the function obtained by GetDesktopWindow) desktop window handle.
# & ; Operation: specifies an operation to be performed. Where the "open" operation indicates execution specified by the FileName parameter program, or open specified by the FileName parameter file or folder; "print" operation indicates that the print specified by the FileName parameter file; "explore" operation indicates the browser specified by the FileName argument folder. When the parameter is set to nil, the default action that the implementation of "open".
# & 9679 ; FileName: used to specify the name of the file to be opened, the file name of the program to be executed or to browse the folder name.
& 9679; Parameters: If the FileName parameter is an executable program, this parameter specifies the command-line parameters, otherwise this parameter should be nil or PChar ( 0 ).
# & 9679 ; Directory: used to specify the default directory.
# & 9679 ; ShowCmd: if the parameter FileName is an executable program, the initial This parameter specifies the window display mode, otherwise this parameter should be set to 0. 
If ShellExecute function succeeds, the value of the execution of the program instance handle is returned. If the return value is less than 32, then an error occurred. 
The foregoing is merely a function ShellExecute standard usage, it will be described below the special usage. 
2 NT registry http protocol handler (Protocols Handler) settings determine which browser to start. 
Formats: HTTP: // domain. 
Such as: ShellExecute (handle, 'Open', HTTP: // ; 
www.neu.edu.cn ', nil , nil , SW_SHOWNORMAL); 
format two: HTTP: // domain / web page file name. 
Such as: the ShellExecute (handle, 'Open', HTTP: // ; . Special usage 
If the FileName parameter is set to "http:" protocol format, the function will open your default browser and link to the specified URL. If multiple users browser installed in the machine, and the function according to 9x the Windows /
www.neu.edu.cn/ default .htm ', nil , nil , 
SW_SHOWNORMAL); 
If the FileName parameter is set to "mailto:" protocol format, the function will start the default mail client, such as Microsoft Outlook (including Microsoft Outlook Express) or Netscape Messanger. If a plurality of mail client user machine installed, according to the function 9x the Windows / to determine which mail client startup settings NT registry mailto protocol handler. 
Formats: mailto:
Such as: the ShellExecute (handle, 'Open', 'mailto:', nil , nil , SW_SHOWNORMAL); new message window opens. 
Two formats: mailto: user account @ mail server address 
such as: the ShellExecute (handle, 'Open', 'mailto: [email protected]', nil , nil , SW_SHOWNORMAL); open a new message window, and automatically fill the recipient's address. If you specify a plurality of recipient addresses, between the recipient addresses must be separated by a comma or a semicolon (below). 
Three formats: mailto:? User account @ mail server address Subject = email subject & body = body of the message
Such as: the ShellExecute (handle, 'Open', 'mailto:? [email protected] Subject 
ShellExecute (handle, = & Body = the Hello This IS A Test', nil , nil , SW_SHOWNORMAL); open a new message window, closed and autofill addressee, the message subject and message body. If the message body include multiple lines of text, it must include newline escape character% 0a between each line of text. 
Examples (delphi): 
in an application calls the C: \ Project1.exe; ' Open ' , ' C: \ Project1.exe ' , ' string content ' , nil , SW_SHOWNORMAL); 
can be called in Project1.exe: 
Procedure TForm1.FormCreate (Sender: TObject);
 var I: Integer;
 the begin 
for I: = . 1  to ParamCount do
 IF ParamStr (I) <> ''  the then ShowMessage (ParamStr (I));
 End ; 
 
final parameter that specifies a command window for visibility aspect. 
Please use any of the following a constant 
SW_HIDE hidden window, to make a window active 
SW_MINIMIZE minimized window, to make a window active 
SW_RESTORE display a window with the original size and position, so that it enters the active state while
SW_SHOW display a window with the current size and position, so that it enters the active state while 
SW_SHOWMAXIMIZED maximized window, and activate 
SW_SHOWMINIMIZED minimize the window, and activate 
SW_SHOWMINNOACTIVE a window is minimized, while not changing the active window 
SW_SHOWNA with the current displaying a window size and position, without changing the active window 
SW_SHOWNOACTIVATE display a window with the size and position of the latest, while not changing the active window 
SW_SHOWNORMAL the same SW_RESTORE
View Code

Use parameters to call external executables

{ ************************************************* ******** } 
{                                                                                                } 
{          use ShellExecute Exe external file called with arguments       } 
{                                                                                                } 
{                       Copyright (c) 2005,2008 Jiangzhi Yong              } 
{                   E_Mail: [email protected]                      } 
{              Msn: jiangzhiyong2008 @ MSN .com                    } 
{                                 QQ: 89,461,298                                     } 
{                                                                                              } 
{ *************************************** ******************************************************** } 
 
If Exe file to a compiler, which works in the following code wrote: 
pROJECT,Program Project1;
 uses 
  Forms, 
  Unit1 in  ' Unit1.pas '  { the Form1 } ;
 { $ R & lt * .res } 
var 
 Str: String ;
 the begin 
  Application.Initialize; 
  Application.CreateForm (TForm1, the Form1); 
  Str: = ParamStr ( . 1 ) ;  // read at runtime with the parameter value if there is: 'jiang', if not the end itself. 
     IF Str <> ' Jiang '  the then 
     the begin 
      Application.Terminate; 
      the Exit; 
    End ; 
  the Application.Run; 
End.
 
工程二的调用:program Project2;
procedure TForm1.Button1Click(Sender: TObject);
var
   sFileName,sParam,sFilePath: string;
begin
   sFileName:=ExtractFilePath(Application.ExeName)+'Project1.exe';
   sFilePath:='ExtractFilePath(Application.ExeName)';
   sParam:='jiang';
   ShellExecute(0, 'Open', PChar(sFileName), PChar(sParam), PChar(sFilePath), SW_SHOW);
end;
 
A project by project to call before they can open, if you open alone is not enough. Because there is no parameter passing. Of course, during parameter passing can be encrypted.
View Code

Execution of the program, and wait for its return value System.exitCode

// execute the program, and waits for its return value System.exitCode 
function WinExecAndWait32 (sexe: String): DWord; OVERLOAD;
 function WinExecAndWait32 (sexe: String; 
        the Visibility: Integer; var mOutputs: String ): Cardinal; OVERLOAD; 

// == ====================== specific implementation class and the function code segment ===================== // == 
Implementation 

// execution of the program, and wait for its return value System.exitCode 
// http://delphi.ktop.com.tw/board.php?cid=168&fid=914&tid=36163 
function WinExecAndWait32 (sexe: String) : DWord;
 var RT, n-: DWORD; 
    Si: the STARTUPINFO; 
    PI: the PROCESS_INFORMATION; 
    P: PAnsiChar;
begin
    result:=0;
    //
    zeroMemory(@si, sizeOf(si));
    si.cb:=sizeOf(si);
    zeroMemory(@pi, sizeOf(pi));
    if not CreateProcess(nil, pChar(sExe), nil, nil, FALSE, CREATE_NO_WINDOW, nil, nil, si, pi) then begin
        raise exception.create('CreateProcess failed!');
        exit;
    end;
    repeat
        rt:=WaitForSingleObject(pi.hProcess, 0); 
        Application.ProcessMessages by (); 
        Sleep ( 100 );
     an until RT <> WAIT_TIMEOUT;
     IF (the GetExitCodeProcess (pi.hProcess, RT)) the then Result: = RT;
     //
     the CloseHandle (pi.hProcess); 
    the CloseHandle (pi.hThread ); 
End ;
 // program execution and waits for its return value, comprising a series of output 
// http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=27232 
function WinExecAndWait32 (sexe: String; the Visibility: Integer; var mOutputs: String ): Cardinal;
 var 
SA: TSecurityAttributes; 
hReadPipe, hWritePipe: THandle;
ret:BOOL;
strBuff:array[0..255] of char;
lngBytesread:DWORD;

WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(sa,Sizeof(sa),#0);
sa.nLength := Sizeof(sa);
sa.bInheritHandle := True;
    sa.lpSecurityDescriptor := nil;     //lpSecurityDeforbiddenor
ret := CreatePipe(hReadPipe, hWritePipe, @sa, 0);

WorkDir:=ExtractFileDir(Application.ExeName);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb:=Sizeof(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow:=Visibility;

StartupInfo.hStdOutput:=hWritePipe;
StartupInfo.hStdError:=hWritePipe;

if not CreateProcess(nil,
        PChar(sExe),          { pointer to command line string }
        @sa,                  { pointer to process security attributes }
        @sa,                  { pointer to thread security attributes }
        True,                 { handle inheritance flag }
        // CREATE_NEW_CONSOLE or { creation flags }
        NORMAL_PRIORITY_CLASS,
        nil,                  { pointer to new environment block }
        PChar(WorkDir),       { pointer to current directory name, PChar}
        StartupInfo,          { pointer to STARTUPINFO }
        ProcessInfo)          { pointer to PROCESS_INF }
      then
        Result := INFINITE {-1}
    else begin
      // Form1.Hide;
      // FileOpen(FileName,fmShareExclusive);
      // SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
      ret:=CloseHandle(hWritePipe);
      mOutputs:='';
      while ret do begin
          FillChar(strBuff,Sizeof(strBuff),#0);
          ret := ReadFile(hReadPipe, strBuff, 256, lngBytesread, nil);
          mOutputs := mOutputs + strBuff;
      end;
        //
      Application.ProcessMessages;
      WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
      GetExitCodeProcess(ProcessInfo.hProcess, Result);
      CloseHandle(ProcessInfo.hProcess); { to prevent memory leaks }
      CloseHandle(ProcessInfo.hThread);
      // Form1.Close; { exit application }
      ret := CloseHandle(hReadPipe);
end;
end;
View Code

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11453858.html