TAsyncProcess doesn't start with parametrs - lazarus

I want run phantonJS executable from TProcess wuth my JS and some parametrs for it. But TAsyncProcess ignore my command line params.
PhandomJS docs says, that I must run my script in that order:
phantomjs.exe [phantom opts] jsfile.js [jsfile opts]
In code:
fProc := TAsyncProcess.Create(nil);
fProc.Options := [poNoConsole, poStderrToOutPut];
fProc.ShowWindow := swoNone;
fproc.StartupOptions := [suoUseShowWindow];
fProc.OnTerminate := #privOnProcTerminated;
fDebugFile := '';
...
fProc.CurrentDirectory := ExtractFilePath(fExecutable);
fProc.Executable := fExecutable;
fproc.Parameters.Add(fPhantomScript);
fproc.Parameters.Add(IntToStr(fPort));
fproc.Parameters.Add(fHost);
fproc.Parameters.Add(fDebugFile);
fProc.ShowWindow := swoShowNormal;
fproc.StartupOptions := fproc.StartupOptions + [suoUseShowWindow];
if not isRunning then
begin
fProc.Execute;
Logger.Send('phantonJS launched.');
Result := True;
end
The executable is launching, but I don't see, that's parametrs was applyed to process (via System Explorer), also script don't work as it must.
Why TAsyncProcess ignore my params?
How to fix that?
Lazarus 1.4.4 from web-site.
Target OS: Windows

I have tested that this generally works with Lazarus. I assume that the syntax of the parameters passed was wrong.
As #Nested Type has said: You don't need to quote parameters. TProcess does that for you.

Related

How to compile a Delphi source code file for execution from the command line with parameters?

I use Delphi 7 IDE. I want to run .exe from the command line with parameters (to be defined).
How can I know if my app is running with the CMD command?
How can I read the parameter with source code?
How can I know if my app is running with the CMD command?
You can't, nor do you ever need to. If your project is a console app, then the .exe is always run inside of a console window process. If the project is not a console app, then there is no console, but you can create one if needed using AllocConsole(). Any process, whether it is a console app or not, can receive command-line parameter, though.
How can I read the parameter with source code?
Use the ParamCount() and ParamStr() functions in the System unit, or the FindCmdLineSwitch() function in the SysUtils unit.
Or, use GetCommandLine() in the Windows unit and parse the raw command-line data yourself.
var
bHideForm = boolean ;
begin
bHideForm := False;
bHideForm := ParamCount > 0;
if bHideForm then
bMetadataExport := GetParamValue(C_MENU, sMetaDataMenu);
{--------------------- function GetParamValue ----------------------------- }
function GetParamValue(aParamName: string; out oParamValue: string): Boolean;
var
i: Integer;
s: string;
begin
Result := False;
for i := 1 to ParamCount do
begin
s := ParamStr(i);
Result := pos(aParamName, UpperCase(s)) = 1;
if Result then
begin
oParamValue := copy(s, length(aParamName)+2, MaxInt);
Exit;
end;
end;
end;
end;

Delphi: how can i get list of running applications with starting path?

Using Delphi (windows app) i want to get list of other applications running currently. Here How to check if a process is running using Delphi? i've found great tutorial about geting filenames/names of running application, however it gives names only process name (for example NOTEPAD.EXE). I've used naturally part with
UpperCase(ExtractFileName(FProcessEntry32.szExeFile))
and
UpperCase(ExtractFilePath(FProcessEntry32.szExeFile))
and just
UpperCase(FProcessEntry32.szExeFile)
but obviously FProcessEntry32.szExeFile does not have a path to file/process
Is there a simply way of getting list with paths? Here's How to get the list of running processes including full file path? solution with JclSysInfo library, but i cant use it in place of work in project.
I looked at what I could in Google and what I found usually concerned just the application that is running or the application that is active, but I can't just find a list of all running applications. Maybe i'm missing something obvious?
I'm not looking for any complex procedures, I'm not much interested in process parrent, or if there is no access to the process path, I don't have it and don't bother.
Any simple hint?
OK, due to helpfull comment from #TLama i've combined topics above to take name and path of process:
function processExists(exeFileName: string): Boolean;
var
ContinueLoopP, ContinueLoopM: BOOL;
FSnapshotHandle1, FSnapshotHandle2: THandle;
FProcessEntry32: TProcessEntry32;
FMODULEENTRY32: TMODULEENTRY32;
begin
FSnapshotHandle1 := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
FMODULEENTRY32.dwSize := SizeOf(FMODULEENTRY32);
ContinueLoopP := Process32First(FSnapshotHandle1, FProcessEntry32);
ContinueLoopM := Module32First(FSnapshotHandle2, FMODULEENTRY32);
Result := False;
while Integer(ContinueLoopP) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := True;
ShowMessage(FMODULEENTRY32.szExePath + FProcessEntry32.szExeFile);
ContinueLoopP := Process32Next(FSnapshotHandle1, FProcessEntry32);
ContinueLoopM := Module32Next(FSnapshotHandle2, FMODULEENTRY32);
end;
CloseHandle(FSnapshotHandle1);
CloseHandle(FSnapshotHandle2);
end;
But still FProcessEntry32.szExeFile returns empty string. What i'm doing wrong? Thank You in advance.
I cannot write comment (low score), so I need to write as "answer". Try this code,
using FProcessEntry32.th32ProcessID as parameter:
Function QueryFullProcessImageNameW(hProcess:THandle; dwFlags:Cardinal; lpExeName:PWideChar; Var lpdwSize:Cardinal) : Boolean; StdCall; External 'Kernel32.dll' Name 'QueryFullProcessImageNameW';
Function GetFullPath(Pid:Cardinal) : UnicodeString;
Var rLength:Cardinal;
Handle:THandle;
Begin Result:='';
Handle:=OpenProcess(PROCESS_QUERY_INFORMATION, False, Pid);
If Handle = INVALID_HANDLE_VALUE Then Exit;
rLength:=256; // allocation buffer
SetLength(Result, rLength+1); // for trailing space
If Not QueryFullProcessImageNameW(Handle, 0, #Result[1],rLength) Then Result:='' Else SetLength(Result, rLength);
End;
This is a simple way I think. If you want to get the loaded DLL's full name, use
FMODULEENTRY32.hModule with GetModuleFileNameW function.

Autohotkey sending mail through ComObject

Problem:
I have made a script that sends a mail through ComObject and it works like a charm when i use it as user, but not if i execute it with Task Scheduler.
What have i done? I have changed ComObjActive to ComObjCreate.
Code:
The body is correct and contains HTML text.
Myself = %AUMailAdress%
Recipient := Myself
Subject := Oi
Body := Ci
Recipient5 = Hidden#gmail.com
DocumentLocation = %SourceDir%\Tooloutput\AutoProjectsLog\Faults
olMailItem := 0
MailItem := ComObjCreate("Outlook.Application").CreateItem(olMailItem)
olFormatHTML := 2
MailItem.BodyFormat := olFormatHTML
MailItem.Subject := Subject
MailItem.HTMLBody := Body
Recipient := MailItem.Recipients.Add(Recipient)
Recipient.Type := 1
Loop, %DocumentLocation%\*.*
MailItem.Attachments.Add(A_LoopFileLongPath)
MailItem.cc := Recipient5
MailItem.Send
ExitApp
return
Issue:
The following error i get when it tries to make a ComObject.
Error: 0x80080005 - Server execution failed
Line: #
---> 674: MailItem: = ComObjCreate ("Outlook.Application"). CreateItem (olMailItem)
Continuous running the script?
I just struggled with this about 3 hours, trying many things, including this method:
MSDN Method | ",512"
At the end, I just ran Outlook as administrator and all started working properly, using this code:
outlookApp := ComObjCreate("Outlook.Application")
MailItem := outlookApp.CreateItem(0)
MailItem.Display
System: Windows 10 x64
Outlook 2019
AHK 1.1.33.02 ran as administrator

Indy HttpClient.Post gives range check error when built for release but not in debug - why?

I am using Delphi 2009, Indy ver 10.5498 together with libeay32.dll and ssleay32.dll from the zip file openssl-1.0.2r-i386-win32 at https://indy.fulgan.com/SSL/.
Range checking is turned on in project options and not turned off anywhere.
Using the code below, which I generated with Remy's help from this post, I can upload data to an API on a server via https when running in the IDE with debugging turned on or from a compiled exe generated with debugging tuned on.
However, if I build a release version then whether I run it through the IDE or as an exe I get a range check error on the line result := HttpClient.Post(THE_URL, FormData);
The params list simply contains the to, from, subject, body etc and there is no attachment in the filenames list ie filenames.Count = 0. U_GeneralRoutines.TheFileStoreFolder is simply a folder inside ProgramData where the SSL DLL's are stored.
As the debugger didn't catch this I put in the two showmessage lines before and after the call. When built as debug, both messages get shown and the post succeeds. When built as release the first one gets displayed and then I get the range check error.
I don't suppose there is a bug in the POST code, so what can be going wrong?
function UploadToAPI(params, filenames: TStrings): string;
var
HttpClient: TIdHttp;
IdSSLIOHandler: TIdSSLIOHandlerSocketOpenSSL;
FormData : TIdMultiPartFormDataStream;
i : integer;
PathToSSLlibraries : string;
begin
FormData := TIdMultiPartFormDataStream.Create;
HttpClient:= TIdHttp.Create;
IdSSLIOHandler:= TIdSSLIOHandlerSocketOpenSSL.Create;
PathToSSLlibraries := IncludeTrailingPathDelimiter(U_GeneralRoutines.TheFileStoreFolder);
IdOpenSSLSetLibPath(PathToSSLlibraries); //set path to libeay32.dll and ssleay32.dll in the common ProgramData folder
HttpClient.IOHandler := IdSSLIOHandler;
HttpClient.Request.CustomHeaders.FoldLines := true ;
try
for i := 0 to params.Count - 1 do
FormData.AddFormField(params.Names[i], params.ValueFromIndex[i]);
for i := 0 to filenames.Count - 1 do
FormData.AddFile('attachment', filenames[i]); //works with ver 10.5498 but not with 10.2.5
//add authorisation header
HttpClient.Request.CustomHeaders.Add('Authorization:Basic ' + ATHORISATION_STR); //byte64 encoding of the api key
HttpClient.ProtocolVersion := pv1_1; //get the full server response which allows for just one try-except
HttpClient.HTTPOptions := HttpClient.HTTPOptions + [hoKeepOrigProtocol, hoNoProtocolErrorException, hoWantProtocolErrorContent];
try
showmessage('about to post');
result := HttpClient.Post(THE_URL, FormData); //post to the api
showmessage('posted');
except
on E: Exception do
begin
result := E.ClassName + ': ' + E.message;
raise;
end;
end; //try
finally
FormData.Free;
IdSSLIOHandler.free;
HttpClient.free;
end;
I appreciate that this type of scenario is often caused by unintialised variables in the release version that would get automatically initialised when in the IDE / debug. But all the variables in my procedure do seem to be getting initialised before the call to POST.

Using windows command line from Pascal

I'm trying to use some windows command line tools from within a short Pascal program. To make it easier, I'm writing a function called DoShell which takes a command line string as an argument and returns a record type called ShellResult, with one field for the process's exitcode and one field for the process's output text.
I'm having major problems with some standard library functions not working as expected. The DOS Exec() function is not actually carrying out the command i pass to it. The Reset() procedure gives me a runtime error RunError(2) unless i set the compiler mode {I-}. In that case i get no runtime error, but the Readln() functions that i use on that file afterwards don't actually read anything, and furthermore the Writeln() functions used after that point in the code execution do nothing as well.
Here's the source code of my program so far. I'm using Lazarus 0.9.28.2 beta, with Free Pascal Compiler 2.24
program project1;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, StrUtils, Dos
{ you can add units after this };
{$IFDEF WINDOWS}{$R project1.rc}{$ENDIF}
type
ShellResult = record
output : AnsiString;
exitcode : Integer;
end;
function DoShell(command: AnsiString): ShellResult;
var
exitcode: Integer;
output: AnsiString;
exepath: AnsiString;
exeargs: AnsiString;
splitat: Integer;
F: Text;
readbuffer: AnsiString;
begin
//Initialize variables
exitcode := 0;
output := '';
exepath := '';
exeargs := '';
splitat := 0;
readbuffer := '';
Result.exitcode := 0;
Result.output := '';
//Split command for processing
splitat := NPos(' ', command, 1);
exepath := Copy(command, 1, Pred(splitat));
exeargs := Copy(command, Succ(splitat), Length(command));
//Run command and put output in temporary file
Exec(FExpand(exepath), exeargs + ' >__output');
exitcode := DosExitCode();
//Get output from file
Assign(F, '__output');
Reset(F);
Repeat
Readln(F, readbuffer);
output := output + readbuffer;
readbuffer := '';
Until Eof(F);
//Set Result
Result.exitcode := exitcode;
Result.output := output;
end;
var
I : AnsiString;
R : ShellResult;
begin
Writeln('Enter a command line to run.');
Readln(I);
R := DoShell(I);
Writeln('Command Exit Code:');
Writeln(R.exitcode);
Writeln('Command Output:');
Writeln(R.output);
end.
At a quick look I see that you try to split command based on space. What if:
I try execute something without parameters, like fpc? (answer: exepath will be empty)
I try execute something with full path and with space in it like C:\Program Files\Edit Plus 3\editplus.exe?
I tried Exec() and it seems to work when you give it full path to executable you want to run, but output redirection does not work. Look at: Command line redirection is performed by the command line interpreter. However you can execute .bat file that does redirection (create temporary .bat file with command user gives + redirection, and run that batch).
You can use that:
uses sysutils;
begin
ExecuteProcess('cmd','/c dir C:\foo');
ExecuteProcess('C:\foo\bar.exe','param1 param2');
end.
If you want to get output of command, you may want to see this post. http://wiki.freepascal.org/Executing_External_Programs#TProcess
Do not use dos.exec, it is limited to a short (255 char) command line. Use sysutils.executeprocess.
However Michal's comments probably touch the main issue. When executing via kernel (not shell) functions, one should always provide a complete path. Also, using kernel functions one can't use shell commands like redirection.
In general, I suggest you try to use the TProcess class in the process unit. It abstracts all of this and more, and is also used by Lazarus to call external tools.

Resources