主程序执行shellExecute这一条代码后,不会等待被shellexecute所调用的exe文件执行完,就继续执行后面的代码,我的主程序根本不能对它的执行情况进行控制。
我要使 被shellexecute所调用的exe文件执行完后 ,我的主程序才执行后面的代码,我应该怎样做??(在C语言中好像有个wait?可以实现这个功能)
------------------------
期待您得回应!
5 个解决方案
#1
用CreateProcess 创建进程
WaitForMultipleObjects 等待进程结束
WaitForMultipleObjects 等待进程结束
#2
执行外部程序一直到它结束
㈠ 编程语言Delphi 1.0,操作系统 Window3.1
以下是一个执行外部程序ARJ.EXE的例子的部分代码
相信看过就会用了。
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;
㈠ 编程语言Delphi 1.0,操作系统 Window3.1
以下是一个执行外部程序ARJ.EXE的例子的部分代码
相信看过就会用了。
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;
#3
function WinExecAndWait32(FileName: string; Visibility: integer): Cardinal;
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, nil, nil, true,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
nil, nil, StartupInfo, ProcessInfo) then
Result := INFINITE
else begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
例:
WinExecAndWait32('notepad.exe', SW_SHOW);//当记事本退出后才会执行下面的语句
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, nil, nil, true,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
nil, nil, StartupInfo, ProcessInfo) then
Result := INFINITE
else begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
例:
WinExecAndWait32('notepad.exe', SW_SHOW);//当记事本退出后才会执行下面的语句
#4
Function WinExecExW(cmd,workdir:pchar;visiable:integer = SW_SHOWNORMAL):DWORD;
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),#0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow:=visiable;
if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then
result:=0
else
begin
waitforsingleobject(processinfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),#0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow:=visiable;
if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then
result:=0
else
begin
waitforsingleobject(processinfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
#5
在ShellExecute(...)语句后可以加上对你所执行的外部程序的句柄进行检测的语句,掌握它的执行情况,也就是说,当你得到这个外部程序关闭时的句柄时,在往下执行!具体实例,网上找!
#1
用CreateProcess 创建进程
WaitForMultipleObjects 等待进程结束
WaitForMultipleObjects 等待进程结束
#2
执行外部程序一直到它结束
㈠ 编程语言Delphi 1.0,操作系统 Window3.1
以下是一个执行外部程序ARJ.EXE的例子的部分代码
相信看过就会用了。
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;
㈠ 编程语言Delphi 1.0,操作系统 Window3.1
以下是一个执行外部程序ARJ.EXE的例子的部分代码
相信看过就会用了。
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;
#3
function WinExecAndWait32(FileName: string; Visibility: integer): Cardinal;
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, nil, nil, true,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
nil, nil, StartupInfo, ProcessInfo) then
Result := INFINITE
else begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
例:
WinExecAndWait32('notepad.exe', SW_SHOW);//当记事本退出后才会执行下面的语句
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, nil, nil, true,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
nil, nil, StartupInfo, ProcessInfo) then
Result := INFINITE
else begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
例:
WinExecAndWait32('notepad.exe', SW_SHOW);//当记事本退出后才会执行下面的语句
#4
Function WinExecExW(cmd,workdir:pchar;visiable:integer = SW_SHOWNORMAL):DWORD;
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),#0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow:=visiable;
if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then
result:=0
else
begin
waitforsingleobject(processinfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),#0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow:=visiable;
if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then
result:=0
else
begin
waitforsingleobject(processinfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
#5
在ShellExecute(...)语句后可以加上对你所执行的外部程序的句柄进行检测的语句,掌握它的执行情况,也就是说,当你得到这个外部程序关闭时的句柄时,在往下执行!具体实例,网上找!