How to automate perl script in Delphi?

I am currently working in a delphi application. I need to run a perl script from a delphi application. To be more clear, we manually run the perl script in a unix window. Now I need to automate the execution of this perl script from the application.

I tried using ShellExecute:

ShellExecute(Handle, 'open', PChar('C:\loaderperl.bat'), nil, nil, SW_SHOW);

The window blinks for a second and closes. I do not know if the script is running or not.

Hi, Thanks for all your answers and sorry for my late reply. However, I am struggling with this automation. Now I will clearly explain the process, I am currently opening a window spacers, connecting to a remote computer and running a perl script. The perl script, in turn, calls the storage procedure and updates the table. Now I want to do the above process automatically by pressing a button. So when I click the button, it should call a function that connects to the remote computer, and then runs the perl script. Am I clear to you? Please help solve this problem. I need this code in delphi

+3
source share
4 answers

(Edited in response to the comments below.)

: -, script; -, , ( , .)

1, script:

script Delphi, ShellExecuteEx "open" () ( .) script, Perl, Perl exe script .

API ShellExecute.

ShellExecute[Ex] , . , , , , , - SEE_MASK_NOASYNC. MSDN .

2, , :

, script, , , . script, , . ( Perl, , , "" first/startup.)

  • SEE_MASK_NOCLOSEPROCESS ShellExecuteEx. hProcess , , , ( , GetLastError, , .) , ( , , ), .
  • : WaitForSingleObject(YourParamStruct.hProcess, INFINITE) ( hProcess, )
  • GetExitCodeProcess, . (, STILL_ACTIVE, , .) 0. script, , - , . , - , 1 , . ( 259, STILL_ACTIVE - Delphi , , , , , Perl script - .)

Delphi , , Perl script , - , - , ..

- , , script , . , Perl script , , , , , , . , CreateProcess . , , , , , , !

+1

rsh - unix (perl interpreter + script location + name) shellexecute?

0

(perl- , ssh smth, ). , , ( , , WaitForSingleObject ShellExecute, ).

UPDATE: script,

function ExecAndWait(const FileName, Params: ShortString; const WinState: Word): boolean; export; 
var 
  StartInfo: TStartupInfo; 
  ProcInfo: TProcessInformation; 
  CmdLine: ShortString; 
begin 
  { Put the name of file between quotes, due to spaces in names of files in system Win9x } 
  CmdLine := '"' + Filename + '" ' + Params; 
  FillChar(StartInfo, SizeOf(StartInfo), #0); 
  with StartInfo do 
  begin 
    cb := SizeOf(SUInfo); 
    dwFlags := STARTF_USESHOWWINDOW; 
    wShowWindow := WinState; 
  end; 
  Result := CreateProcess(nil, PChar( String( CmdLine ) ), nil, nil, false, 
                          CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, 
                          PChar(ExtractFilePath(Filename)),StartInfo,ProcInfo);
  { Wait the finish of program } 
  if Result then 
  begin 
    WaitForSingleObject(ProcInfo.hProcess, INFINITE); 
    { Free the Handles } 
    CloseHandle(ProcInfo.hProcess); 
    CloseHandle(ProcInfo.hThread); 
  end; 
end; 
0

ShellExecute() cmd.exe script , :

ShellExecute(Handle, 'open', 'cmd.exe', '/C C:\loaderperl.bat', nil, SW_SHOW);

You can use output redirection to write script output to a file. If you want to check if the script has completed successfully, you can (for testing purposes) add pauseto the end.

0
source

Source: https://habr.com/ru/post/1722657/


All Articles