Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Pascal
Expertise: Beginner
Feb 22, 1999



Application Security Testing: An Integral Part of DevOps

Running Applications From Your Own

What code do I use for Delphi to run a program and use command line parameters inputted by the user?

This code will allow you to launch other applications from your own. To use this function, simply supply a fully qualified path name in ProgramName and any arguments on the command line following the program name separated by a space. As the help file states: "If lpApplicationName is NULL, the first white space-delimited token of the command line specifies the module name..." In English, the characters before the first space encountered (or if no space is encountered as in a single program call) is interpreted as the EXE to execute. The rest of the string is the argument line.

procedure ExecNewProcess(ProgramName : String);
  StartInfo  : TStartupInfo;
  ProcInfo   : TProcessInformation;
  CreateOK   : Boolean;

  { fill with known state }
  StartInfo.cb := SizeOf(TStartupInfo);

  CreateOK := CreateProcess(nil, PChar(ProgramName), nil, nil,False,
              nil, nil, StartInfo, ProcInfo);

  { check to see if successful }
  if CreateOK then
    //may or may not be needed. Usually wait for child processes
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);

For example, let's say that the user inputted: "/a /b /c" as command line arguments, and you captured them into a string var called "args." To use ExecNewProcess, you'd simply do this:
MyProgramName := 'C:\Program Files\MyProgram\MyProgram.EXE';
ExecNewProcess(MyProgramName + ' ' + args);
Pretty simple...
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date