Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Pascal
Expertise: Beginner
Mar 19, 1999

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 Pro
 
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap