Mega Code Archive

 
Categories / Delphi / Examples
 

Winexec - wait - çalıştır ve bekle

Q : How do I execute a program and have my code wait until it is finished? A: uses Wintypes,WinProcs,Toolhelp,Classes,Forms; Function WinExecAndWait(Path : string; Visibility : word) : word; var InstanceID : THandle; begin InstanceID := WinExec(Pchar(Path),Visibility); if InstanceID < 32 then { a value less than 32 indicates an Exec error } WinExecAndWait := InstanceID else begin Repeat Application.ProcessMessages; until Application.Terminated or (GetModuleUsage(InstanceID) = 0); WinExecAndWait := 32; end; end; {* executes a named external application under windows and waits for the application to terminate before resuming execution. Any parameters to the exe file are also included in the filename string} Function TForm1.WinExecute32Wait(FileName : AnsiString) : integer; var zAppName:array[0..512] of char; zCurDir:array[0..255] of char; WorkDir:String; StartupInfo:TStartupInfo; ProcessInfo:TProcessInformation; res : DWord; begin StrPCopy(zAppName,FileName); GetDir(0,WorkDir); StrPCopy(zCurDir, ExtractFilePath(FileName)); FillChar(StartupInfo, Sizeof(StartupInfo),#0); StartupInfo.cb := Sizeof(StartupInfo); StartupInfo.dwFlags := STARTF_USESHOWWINDOW; StartupInfo.wShowWindow := SW_HIDE; // hide the application window // StartupInfo.wShowWindow := SW_SHOW; if not CreateProcess(nil, zAppName, { pointer to command line string } nil, { pointer to process security attributes} nil, { pointer to thread security attributes } false, { handle inheritance flag } CREATE_NEW_CONSOLE or { creation flags } NORMAL_PRIORITY_CLASS, nil, { pointer to new environment block } nil, { pointer to current directory name } StartupInfo, { pointer to STARTUPINFO } ProcessInfo) then Result := 0 { pointer to PROCESS_INF } else begin WaitforSingleObject(ProcessInfo.hProcess,INFINITE); GetExitCodeProcess(ProcessInfo.hProcess, res); result := res; end; end;