devxlogo

Executing a File Selected in Filelistbox

Executing a File Selected in Filelistbox

Question:
I am trying to work out the coding to execute a file that I select in my filelistbox in Delphi 4. Can you help me?

Answer:
You can refer to the online help for ShellExecute, or for a more robust methodology, use CreateProcess. Here’s a wrapper for that:

procedure ExecNewProcess(ProgramName : String;                          Wait : Boolean);var  StartInfo  : TStartupInfo;  ProcInfo   : TProcessInformation;  CreateOK   : Boolean;begin  { fill with known state }  FillChar(StartInfo,SizeOf(TStartupInfo),#0);  FillChar(ProcInfo,           SizeOf(TProcessInformation), #0);  with StartInfo do begin    cb := SizeOf(TStartupInfo);    dwX := 10000;    dwY := 10000;    dwFlags := dwFlags OR STARTF_USEPOSITION;  end;  CreateOK := CreateProcess(nil,               PChar(ProgramName), nil, nil,False,              CREATE_DEFAULT_ERROR_MODE AND (NOT               CREATE_NEW_CONSOLE) OR              CREATE_NEW_PROCESS_GROUP OR               HIGH_PRIORITY_CLASS,              nil, nil, StartInfo, ProcInfo);  { check to see if successful }  if CreateOK then    if Wait then      //may or may not be needed. Usually wait       //for child processes      WaitForSingleObject(ProcInfo.hProcess,                           INFINITE);end;

devx-admin

Share the Post: