function WinExecAndWait32(FileName:String; Visibility :integer):integer; var AResult : cardinal; 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 NORMAL_PRIORITY_CLASS, { creation flags } 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,AResult); Result := AResult; end; end;procedure TForm1.FormClick(Sender: TObject); begin WinExecAndWait32('E:\temp\graphpro.exe',0); showmessage('aaa'); end;
相信看过就会用了。var
sCommandLine: string;
bCreateProcess: boolean;
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
sCommandLine := 'ARJ.EXE /?';
bCreateProcess := CreateProcessA(nil,
PChar(sCommandLine),
nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,
lpStartupInfo, lpProcessInformation);
if bCreateProcess then
WaitForSingleObject(lpProcessInformation.hProcess,
INFINITE);
end;
㈡ 编程语言Delphi3.0,操作系统 Window95
同样是上面的例子的部分代码 var
pWindowsList: pointer;
hActiveWindow: HWnd;
hExeHandle: THandle;
begin
pWindowsList := DisableTaskWindows(0);
hActiveWindow := GetActiveWindow;
try
hExeHandle := WinExec('arj.exe /?',SW_SHOWNORMAL);
while GetModuleUsage(hExeHandle) <> 0 do
Application.ProcessMessages;
finally
EnableTaskWindows(pWindowsList);
SetActiveWindow(hActiveWindow);
end;
end;
// 相信你明白了。
题外话:如果执行的是 MSDOS 外部程序,如何能让它的窗口不显示
出来呢? [ 接上例 ]:
TStartupInfo 这个结构中有一个 sShowWindow 栏位, 将之设为
SW_HIDE即可,
同时, dwFlags 标志中至少需含有 STARTF_USESHOWWINDOW, 否则
CreateProcess
时, sShowWindow 栏位的设定会无效, 以下是修改过的程式:var
sCommandLine: string;
bCreateProcess: boolean;
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
// sCommandLine 的内容请视您的情况修改
sCommandLine :='Xcopy d:\temp\temp1\*.* d:\temp\temp2
/v/y';
lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
lpStartupInfo.wShowWindow := SW_HIDE;
bCreateProcess := CreateProcess(nil,
PChar(sCommandLine),nil,nil,True,
HIGH_PRIORITY_CLASS, nil,
nil,lpStartupInfo, lpProcessInformation);
if bCreateProcess then
WaitForSingleObject(lpProcessInformation.hProcess,
INFINITE);
end;
注:这个问题不用高手也会,因为只要粘贴就行了!
var
AResult : cardinal;
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 NORMAL_PRIORITY_CLASS,
{ creation flags }
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,AResult);
Result := AResult;
end;
end;procedure TForm1.FormClick(Sender: TObject);
begin
WinExecAndWait32('E:\temp\graphpro.exe',0);
showmessage('aaa');
end;
如果把DOS运行后的提示给显示出来呢?
你的方法是不错,但怎么把
Winexec(cmdline, SW_SHOWNORMAL);改成WinExecAndWait32('E:\temp\graphpro.exe',0);
你这个是运行一个EXE文件呀,我的不是,我是调用DOS