当前位置: 首页 > 开发语言 > VB.net > vb.net如何判断shell完成,或者等待shell完成

vb.net如何判断shell完成,或者等待shell完成

时间:2021-01-03 21:27来源:未知 点击:
分享到:
 

	
  1. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  2.  
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
  3.  
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  4.  
    Const PROCESS_QUERY_INFORMATION = &H400
  5.  
    Const STILL_ALIVE = &H103
  6.  
     
  7.  
    Private Sub Command1_Click()
  8.  
    Dim pid As Long
  9.  
    pid = Shell("c:/a.bat", vbNormalFocus)
  10.  
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
  11.  
     
  12.  
    Do
  13.  
    Call GetExitCodeProcess(hProcess, ExitCode)
  14.  
    DoEvents()
  15.  
    Loop While ExitCode = STILL_ALIVE
  16.  
    Call CloseHandle(hProcess)
  17.  
    MsgBox("运行结束")
  18.  
    End Sub
VB启动/结束另一程序(Shell 等待程序运行结束)       VB 中,常以Shell指令来执行外部程式,然而它在Create该外部process 後,立刻就会回到vb 的下一行程式,无法做到等待该Process结束时,才执行下一行指令,或是说,无法得知该Process是否已结束,甚者,该Process执行到一半,又该如何中止其执行等等,这些都不是Shell指令所能控制的,因此我们需使API的帮助来完成。      第一个问题,如何等待shell所Create的process结束後才往後执行vb的程式。首先要知道的是,每个Process有唯一的一个ProcessID,这是OS给定的,用来区别每个 Process,这个Process ID(PID)主要可用来取得该Process相对应的一些资讯,然而要对该Process的控制,却大多透过 Process Handle(hProcess)。     VB Shell指令的传回值是PID,而非hProcess,所以我们需透过OpenProcess这个API来取得 hProcess而OpenProcess()的第一个叁数,指的是所取得的hProcess所具有的能力,像 PROCESS_QUERY_INFORMATION 便是让GetExitCode()可取得hProcess所指的process之状态,而PROCESS_TERMINATE,便是让TerminateProcess(hProcess..)的指令能够生效,也就是说,不同叁数设定,使hProcess所具有的权限、能力有所不同。

    取得 hProcess後便可以使用WaitForSingleObject()来等待hProcess状态的改变,也就是说,它会等待 hProcess所指的process执行完,这个指令才结束,它第二个叁数所指的是 WaitForSingleObject()所要等待的时间(in milliseconds ),如果超过所指的时间,就TimeOut而结束WaitForSingleObject()的等待。若要它无限的等下去,就设定为INFIN99vE。

 


	
  1.  
    pid = Shell("C:/tools/spe3/pe2.exe", vbNormalFocus)
  2.  
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
  3.  
    ExitEvent = WaitForSingleObject(hProcess, INFIN99vE)
  4.  
    Call CloseHandle(hProcess)
上例会无限等待shell指令create之process结束後,才再做後面的vb指令。有时觉得那会等太久,所以有第二个解决方式:

 

     等process结束时再通知vb 就好,即:设定一个公用变数(isDone),当它变成True时代表Shell所Create的Process已结束。

    当Process还在执行时,GetExitCodeProcess会传&H103给其第二个叁数,直到结束时才传另外的数值,如果程式正常结束,那Exitcode = 0,否则就得看它如何结束了。

     或许有人在其他地方看到 loop的地方是Loop while Exitcode <> 0,那有一点危险,如果以这程子来看,您不是用F4来离开pe2而是用右上方 X 的结束dos window那麽,会因为ExitCode的值永远不会是0,而进入无穷的回圈。 


	
  1.  
    Dim pid As Long
  2.  
    pid = Shell("C:/tools/spe3/pe2.exe", vbNormalFocus)
  3.  
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
  4.  
    isDone = False
  5.  
     
  6.  
    Do
  7.  
    Call GetExitCodeProcess(hProcess, ExitCode)
  8.  
    Debug.Print ExitCode
  9.  
    DoEvents
  10.  
    Loop While ExitCode = STILL_ALIVE
  11.  
     
  12.  
    Call CloseHandle(hProcess)
  13.  
    isDone = True
另外,如果您的shell所Create的程式,有视窗且为立刻Focus者,可另外用以下的方式:

 

 


	
  1.  
    Dim pid As Long
  2.  
    Dim hwnd5 As Long
  3.  
    pid = Shell("c:/tools/spe3/pe2.exe", vbNormalFocus)
  4.  
    hwnd5 = GetForegroundWindow()
  5.  
    isDone = False
  6.  
     
  7.  
    Do While IsWindow(hwnd5)
  8.  
    DoEvents
  9.  
    Loop
  10.  
    isDone = True
而如何强迫shell所Create的process结束呢,那便是 

	
  1.  
    Dim aa As Long
  2.  
    If hProcess <> 0 Then
  3.  
    aa = TerminateProcess(hProcess, 3838)
  4.  
    End If
hProcess便是先前的例子中所取得的那个Process Handle, 3838所指的是传给GetExitCodeProcess()中的第二叁数,这是我们任意给的,但最好不要是0,因为0一般是代表正常结束,当然这样设也不会有错。当然不可设&H103,以这个例子来看,如果程式正处於以下的LOOP 

 

 


	
  1.  
    Do
  2.  
    Call GetExitCodeProcess(hProcess, ExitCode)
  3.  
    Debug.Print ExitCode
  4.  
    DoEvents
  5.  
    Loop While ExitCode = STILL_ALIVE
  6.  
     
  7.  
    Debug.print ExitCode
    而执行了 TerminateProcess(hProcess, 3838)那会看到ExitCode = 3838。

 

    然而,这个方式在win95没问题,在NT中,可能您要在OpenProcess()的第一个叁数要更改成 PROCESS_QUERY_INFORMATION Or PROCESS_TERMINATE 这样才能Work。不过良心的建议,非到最後关头,不要使用TerminateProcess(),因不正常的结束,往往许多程式结束前所要做的事都没有做,可能造成Resource的浪费,甚者,下次再执行某些程式时会有问题,例如:本人常使用MS-dos Shell Link 的方式执行一程式,透过Com port与大电脑的联结,如果Ms-dos Shell Link 不正常结束,下次再想Link时,会发现too Many Opens,这便是一例。  另外,有人使用Shell来执行.bat档,即:

 

    pid = Shell("c:/aa.bat", vbNormalFocus)
   可是却遇上aa.bat结束了,但ms-dos的Window却仍活着,那可以用以下的方式来做

 

       pid = Shell("c:/command.com /c c:/aa.bat", vbNormalFocus) 


    那是执行Command.com,而Command.com指定执行c:/aa.bat 而且结束时自动Close,所有程式如下:

 


	
  1.  
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  2.  
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  3.  
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  4.  
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
  5.  
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
  6.  
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
  7.  
    Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
  8.  
     
  9.  
    Const PROCESS_QUERY_INFORMATION = &H400
  10.  
    Const STILL_ALIVE = &H103
  11.  
    Const INFIN99vE = &HFFFF
  12.  
     
  13.  
    Private ExitCode As Long
  14.  
    Private hProcess As Long
  15.  
    Private isDone As Long
  16.  
     
  17.  
    Private Sub Command1_Click()
  18.  
    Dim pid As Long
  19.  
    pid = Shell("C:/tools/spe/pe2.exe", vbNormalFocus)
  20.  
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
  21.  
    isDone = False
  22.  
    Do
  23.  
    Call GetExitCodeProcess(hProcess, ExitCode)
  24.  
    Debug.Print ExitCode
  25.  
    DoEvents()
  26.  
    Loop While ExitCode = STILL_ALIVE
  27.  
    Call CloseHandle(hProcess)
  28.  
    isDone = True
  29.  
    End Sub
  30.  
     
  31.  
    Private Sub Command2_Click()
  32.  
    Dim pid As Long
  33.  
    Dim ExitEvent As Long
  34.  
    pid = Shell("C:/tools/spe3/pe2.exe", vbNormalFocus)
  35.  
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
  36.  
    ExitEvent = WaitForSingleObject(hProcess, INFIN99vE)
  37.  
    Call CloseHandle(hProcess)
  38.  
    End Sub
  39.  
     
  40.  
    Private Sub Command3_Click()
  41.  
    Dim aa As Long
  42.  
    If hProcess <> 0 Then
  43.  
    aa = TerminateProcess(hProcess, 3838)
  44.  
    End If
  45.  
     
  46.  
    End Sub
  47.  
     
  48.  
    Private Sub Command4_Click()
  49.  
    Dim pid As Long
  50.  
    Dim hwnd5 As Long
  51.  
    pid = Shell("c:/tools/spe3/pe2.exe", vbNormalFocus)
  52.  
    hwnd5 = GetForegroundWindow()
  53.  
    isDone = False
  54.  
    Do While IsWindow(hwnd5)
  55.  
    DoEvents()
  56.  
    Loop
  57.  
    isDone = True
  58.  
    End Sub
  59.  
     
  60.  
    Private Sub Command5_Click()
  61.  
    Dim pid As Long
  62.  
    'pid = Shell("c:/windows/command/xcopy c:/aa.bat a:", vbHide)
  63.  
    pid = Shell("c:/command.com /c c:/aa.bat", vbNormalFocus)
  64.  
    End Sub
===================================================================================

 

===================================================================================

在使用shell后,如何等待此程序完成后,程序才继续执行.我们使用 shell 调用一个外部程序的时候,通常 vb(a) 会在调用之后继续下面的语句,而不管此 shell 程序执行完成没有.有时我们需要在此 shell 执行完成之后才继续,又当如何呢?请看源程:

 


	
  1.  
    Public Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  2.  
    Public Declare Function WaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  3.  
    Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long
  4.  
     
  5.  
    Dim lngPId As Long
  6.  
    Dim lngPHandle As Long
  7.  
    lngPId = Shell("Notepad", vbNormalFocus)
  8.  
    lngPHandle = OpenProcess(SYNCHRONIZE, 0, lngpId)
  9.  
     
  10.  
    If lngPHandle <> 0 Then
  11.  
    Call WaitForSingleObject(lngPHandle, INFINITE) ' 无限等待, 直到程式结束
  12.  
    Call CloseHandle(lngPHandle)
  13.  
    End If
需要注意的是,在 shell 程序未完成前,你的程序不能做任何事,请小心为之。

 

 

=============================================================================

=============================================================================

 


	
  1.  
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  2.  
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  3.  
    Private Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpInfo As Any) As Long
  4.  
     
  5.  
    Private Type SHELLEXECUTEINFO
  6.  
    cbSize As Long
  7.  
    fMask As Long
  8.  
    hwnd As Long
  9.  
    lpVerb As String
  10.  
    lpFile As String
  11.  
    lpParameters As String
  12.  
    lpDirectory As String
  13.  
    nShow As Long
  14.  
    hInstApp As Long
  15.  
    'Optional members
  16.  
    lpIDList As Long
  17.  
    lpClass As String
  18.  
    hkeyClass As Long
  19.  
    dwHotKey As Long
  20.  
    hIcon_OR_Monitor As Long
  21.  
    hProcess As Long
  22.  
    End Type
  23.  
     
  24.  
    Private Sub Form_Load()
  25.  
    Dim si As SHELLEXECUTEINFO
  26.  
    si.cbSize = Len(si)
  27.  
    si.lpVerb = "open"
  28.  
    si.lpFile = "notepad.exe"
  29.  
    si.lpParameters = ""
  30.  
    si.lpDirectory = ""
  31.  
    si.nShow = 5 'SW_SHOW
  32.  
    si.fMask = &H40 'SEE_MASK_NOCLOSEPROCESS
  33.  
    ShellExecuteEx si
  34.  
    If si.hProcess <> 0 Then
  35.  
    WaitForSingleObject(si.hProcess, &HFFFFFFFF) ' 无限等待, 直到程式结束
  36.  
    CloseHandle si.hProcess
  37.  
    MsgBox "程序运行完毕!"
  38.  
    End If
  39.  
    End Sub
============================================

 

=============================================

shell命令一观:

shell "cmd /c dir",1'/c表示执行完即关闭窗口  shell "cmd /k dir",1'/k表示执行完停留  shell "cmd /c dir && pause",1'多条语句可用&&连接  关于cmd的具体用法可以在命令提示符下敲入cmd/?看看太多了,只列举一些典型的:  CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF] [[/S] [/C | /K] string]  /C         执行字符串指定的命令然后终断  /K         执行字符串指定的命令但保留  /S        在 /C 或 /K 后修改字符串处理(见下)  /Q          关闭回应  /D        从注册表中停用执行 AutoRun 命令(见下)  /A         使向内部管道或文件命令的输出成为 ANSI  /U          使向内部管道或文件命令的输出成为 Unicode  /T:fg      设置前景/背景颜色(详细信息,请见 COLOR /?)  /E:ON   启用命令扩展(见下)  /E:OFF  停用命令扩展(见下)  /F:ON   启用文件和目录名称完成字符 (见下)  /F:OFF  停用文件和目录名称完成字符(见下)  /V:ON   将 ! 作为定界符启动延缓环境变量扩展。

              如: /V:ON 会允许 !var! 在执行时允许 !var! 扩展变量 var。var 语法在输入时扩展变量,这与在一个 FOR 循环内不同。  /V:OFF  停用延缓的环境扩展。


(责任编辑:admin)
推荐教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们--网站地图          黑ICP备17004983号-1