首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

Shell 调用程序后等候该程序结束后返回继续

2012-09-27 
Shell 调用程序后等待该程序结束后返回继续方法1:(ByVal hHandle As Long, ByVal dwMilliseconds As Long)

Shell 调用程序后等待该程序结束后返回继续

方法1:

(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpInfo As Any) As Long

Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
' Optional members
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon_OR_Monitor As Long
hProcess As Long
End Type

Private Sub Form_Load()
Dim si As SHELLEXECUTEINFO
si.cbSize = Len(si)
si.lpVerb = "open"
si.lpFile = "notepad.exe"
si.lpParameters = ""
si.lpDirectory = ""
si.nShow = 5 'SW_SHOW
si.fMask = &H40 'SEE_MASK_NOCLOSEPROCESS
ShellExecuteEx si
If si.hProcess <> 0 Then
WaitForSingleObject si.hProcess, &HFFFFFFFF ' 无限等待, 直到程式结束
CloseHandle si.hProcess
MsgBox "程序运行完毕!"
End If
End Sub


方法2:

Alias "OpenProcess" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long
Dim lngPId As Long
Dim lngPHandle As Long
lngPId = Shell("Notepad", vbNormalFocus)
lngPHandle = OpenProcess(SYNCHRONIZE, 0, lngpId)
If lngPHandle <> 0 Then
Call WaitForSingleObject(lngPHandle, INFINITE) ' 无限等待, 直到程式结束
Call CloseHandle(lngPHandle)
End If


方法3:

(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400

Const STILL_ALIVE = &H103

Private Sub Command1_Click()
Dim pid As Long
pid = Shell("c:\a.bat", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
Do
Call GetExitCodeProcess(hProcess, ExitCode)
DoEvents
Loop While ExitCode = STILL_ALIVE
Call CloseHandle(hProcess)
MsgBox ("运行结束")
End Sub

?

热点排行