Posted By

randompage on 09/24/11


Tagged


Versions (?)

Using CreateProcess to Execute Programs


 / Published in: Delphi
 

URL: http://www.delphicorner.f9.co.uk/articles/wapi4.htm

  1. procedure ExecNewProcess(ProgramName : String; Wait: Boolean);
  2. var
  3. StartInfo : TStartupInfo;
  4. ProcInfo : TProcessInformation;
  5. CreateOK : Boolean;
  6. begin
  7. { fill with known state }
  8. FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  9. FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
  10. StartInfo.cb := SizeOf(TStartupInfo);
  11. CreateOK := CreateProcess(nil, PChar(ProgramName), nil, nil,False,
  12. CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
  13. nil, nil, StartInfo, ProcInfo);
  14. { check to see if successful }
  15. if CreateOK then
  16. begin
  17. //may or may not be needed. Usually wait for child processes
  18. if Wait then
  19. WaitForSingleObject(ProcInfo.hProcess, INFINITE);
  20. end
  21. else
  22. begin
  23. ShowMessage('Unable to run '+ProgramName);
  24. end;
  25.  
  26. CloseHandle(ProcInfo.hProcess);
  27. CloseHandle(ProcInfo.hThread);
  28. end;

Report this snippet  

You need to login to post a comment.