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

169 Views Asked by At

I use Delphi 7 IDE. I want to run .exe from the command line with parameters (to be defined).

  1. How can I know if my app is running with the CMD command?
  2. How can I read the parameter with source code?
2

There are 2 best solutions below

0
Remy Lebeau On BEST ANSWER

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.

1
Nechba Med On
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;