有没有类似WinExecAndWait这样的函数 ( 积分: 20 )

  • 主题发起人 主题发起人 xj307
  • 开始时间 开始时间
X

xj307

Unregistered / Unconfirmed
GUEST, unregistred user!
1。即可以等待远程程序执行完毕,在等待的同时WinExecAndWait内部可以响应其他消息,
不至于整个程序死在那里,点击后像个白板
2。执行WinExecAndWait()后才去执行dosomething;
WinExecAndWait();
dosomething;
 
1。即可以等待远程程序执行完毕,在等待的同时WinExecAndWait内部可以响应其他消息,
不至于整个程序死在那里,点击后像个白板
2。执行WinExecAndWait()后才去执行dosomething;
WinExecAndWait();
dosomething;
 
{the first example, based on WaitForSingleObject}
function WinExecAndWait32_v1(FileName: string
Visibility: integer):
Cardinal
{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 }
true, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name, PChar}
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) { pointer to PROCESS_INF }
then Result := INFINITE {-1} else
begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE)

GetExitCodeProcess(ProcessInfo.hProcess, Result)

CloseHandle(ProcessInfo.hProcess)
{ to prevent memory leaks }
CloseHandle(ProcessInfo.hThread)

end

end;
 
好像没有这样的函数
但是你可以通过其它方法实现这个功能
使用CreateProcess创建进程,获得进程句柄后
使用WaitForSingleObject函数进入等待
只有进程结束才会返回
 
{the second example, based on PeekMessage(msg,,,,PM_REMOVE)->DispatchMessage(msg)}
function WinExecAndWait32_v2(FileName: string
Visibility: integer):
Cardinal
{integer}
var
zAppName: array[0..512] of char

zCurDir: array[0..255] of char

WorkDir: string

StartupInfo: TStartupInfo

ProcessInfo: TProcessInformation

Msg: TagMsg

ExitCode: cardinal

begin
StrPCopy(zAppName, FileName)

GetDir(0, WorkDir)

StrPCopy(zCurDir, WorkDir)

FillChar(StartupInfo, Sizeof(StartupInfo), #0)

StartupInfo.cb := Sizeof(StartupInfo)

StartupInfo.dwFlags := STARTF_USESHOWWINDOW
// STARTF_FORCEONFEEDBACK

StartupInfo.wShowWindow := Visibility

if CreateProcess(nil, { and once more: }
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
NORMAL_PRIORITY_CLASS, { creation flags }
nil, { pointer to the new environment block }
zCurDir, { current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then { pointer to PROCESS_INF }
begin
repeat
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
begin
if (Msg.message = WM_QUIT) then
Result := Msg.wParam

TranslateMessage(msg)

Dispatchmessage(msg)

Sleep(100)
{your choice}
end

GetExitCodeProcess(ProcessInfo.hProcess, ExitCode)

until (ExitCode <> STILL_ACTIVE)

CloseHandle(ProcessInfo.hThread)

CloseHandle(ProcessInfo.hProcess)

Result := 0

end
else begin
Result := GetLastError

end

end;
 
好像还是不行,
调用了
WinExecAndWait32_v2;
dosomething;
一有响动,还是去执行dosomething了。
 
后退
顶部