如何执行外部程序[并一直等到它结束](谢谢)(50分)

  • 主题发起人 主题发起人 heihei_76
  • 开始时间 开始时间
H

heihei_76

Unregistered / Unconfirmed
GUEST, unregistred user!
如何执行外部程序[并一直等到它结束]
我找到一个例子:
var
pWindowsList: pointer;
hActiveWindow: HWnd;
hExeHandle: THandle;
begin
pWindowsList := DisableTaskWindows(0);
hActiveWindow := GetActiveWindow;
try
hExeHandle := WinExec('arj.exe -x c:/temp/temp1/ITEM.ARJ c:/temp/temp2/QYJSCX.mdb',SW_SHOWNORMAL);
while GetModuleUsage(hExeHandle) <> 0 do
Application.ProcessMessages;
finally
EnableTaskWindows(pWindowsList);
SetActiveWindow(hActiveWindow);
但是我不知道GetModuleUsage要use哪个pas文件
请问还要别的解决方法吗?谢谢
 
uses Windows
或者调用我写的函数
function Run(sCommandLine: string): Boolean;
var
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
FillChar(lpStartupInfo, Sizeof(TStartupInfo), #0);
lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
lpStartupInfo.wShowWindow := SW_SHOW;
Result := CreateProcess(nil, PChar(sCommandLine),
nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,
lpStartupInfo, lpProcessInformation) and
(WaitForSingleObject(lpProcessInformation.hProcess, 3600000) <> WAIT_FAILED);
end;

Run(''arj.exe -x c:/temp/temp1/ITEM.ARJ c:/temp/temp2/QYJSCX.mdb');

-----
http://www.8421.org
 
function WinExecAndWait32(FileName:String; Visibility : integer):integer;
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;

StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
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 := -1 { pointer to PROCESS_INF }

else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
 
多人接受答案了。
 
我很同意qdyoung的代码
 
后退
顶部