windowsdelphi-7show-hidedelphi-2009

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).

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

Solution

  • 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.