Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFFFFFF
Option ExplicitPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long'  分别声明  Process  ID  及  Process  Handle  变数
Dim pId   As Long, pHnd    As Long
Dim Flag   As BooleanPrivate Sub Command2_Click()
    '  Shell传回Process  ID,运行的程序是CALC.EXE
    pId = Shell("C:\aa.bat", 1)
    '  取得  Process  Handle
    pHnd = OpenProcess(SYNCHRONIZE, 0, pId)
    '  TerminateProcess  所传入的是  Process  Handle
    
    Call TerminateProcess(pHnd, 0) '关闭"C:\aa.bat"
    Call CloseHandle(pHnd)
End Sub

解决方案 »

  1.   

    用这个按钮调用的:ShellExecute 0, "open", "c:\exam.bat", vbNullString, vbNullString, SW_SHOWMINNOACTIVE ,这个虽然看不到DOS窗口,但是关机是总要提示有个窗口还没关闭.
    还有能不能给个简单的答案或者例子发给我[email protected],因为我刚学用VB,很多都看不动.
      

  2.   

    @http://www.codeoftheweek.com/bonus/mstipoftheweek.htmlMay 13, 1997
    Closing a DOS prompt window
    by Chuck Kraatz
    After you run a DOS application in Windows 95, the MS-DOS Prompt window doesn't close. To prevent this behavior, you can use the API to find the Window handle for the DOS prompt window, wait for the program to finish running, then zap the DOS prompt window into oblivion.
    This technique doesn't require any forms. It's just a simple VB 4.0 DLL with two properties: the EXE name of the DOS program and the text that will appear as the caption of the DOS prompt window displaying this application. The core of this app lies in three API calls. Place the following code in a standard module: Declare Function FindWindow& Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String)
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongDeclare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Public Const WM_CLOSE = &H10
    The rest of the code goes in the following class module, named Cclose:     Private m_sEXEName As String
         Private m_sDosCaption As String
         Public Sub RunDosApp()
         Dim vReturnValue As Variant
         Dim lRet As Long
         Dim i As Integer     vReturnValue = Shell(m_sEXEName, 1) ' Run EXE
         AppActivate vReturnValue ' Activate EXE Window     Do
          Sleep (10000)
          lRet = FindWindow(vbNullString, m_sDosCaption)
          If (lRet <> 0) Then
              vReturnValue = SendMessage(lRet, WM_CLOSE, &O0, &O0)
              Exit Do
          End If
         Loop
         End Sub
      

  3.   

    @http://www.codeoftheweek.com/bonus/mstipoftheweek.htmlMay 13, 1997
    Closing a DOS prompt window
    by Chuck Kraatz
    After you run a DOS application in Windows 95, the MS-DOS Prompt window doesn't close. To prevent this behavior, you can use the API to find the Window handle for the DOS prompt window, wait for the program to finish running, then zap the DOS prompt window into oblivion.
    This technique doesn't require any forms. It's just a simple VB 4.0 DLL with two properties: the EXE name of the DOS program and the text that will appear as the caption of the DOS prompt window displaying this application. The core of this app lies in three API calls. Place the following code in a standard module: Declare Function FindWindow& Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String)
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongDeclare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Public Const WM_CLOSE = &H10
    The rest of the code goes in the following class module, named Cclose:     Private m_sEXEName As String
         Private m_sDosCaption As String
         Public Sub RunDosApp()
         Dim vReturnValue As Variant
         Dim lRet As Long
         Dim i As Integer     vReturnValue = Shell(m_sEXEName, 1) ' Run EXE
         AppActivate vReturnValue ' Activate EXE Window     Do
          Sleep (10000)
          lRet = FindWindow(vbNullString, m_sDosCaption)
          If (lRet <> 0) Then
              vReturnValue = SendMessage(lRet, WM_CLOSE, &O0, &O0)
              Exit Do
          End If
         Loop
         End Sub
      

  4.   

    要实现目的,可以试一下这个,看看合你的意不!
    shell("cmd /c  c:\exam.bat")
    命令串“cmd /c c:\exam.bat”是可以在DOS状态下运行的命令。你试一下就知道了(用运行对话框)。cmd命令的相关参数你可以去查系统帮助里的命令参考。
    而shell("cmd /c c:\exam.bat")可以放在某个命令事件里,如CommandButton_Click().
      

  5.   

    写成这样就报错:实时错误53
                    文件没找到
    是不是SHELL参数不能这么写?
    Private Sub Command1_Click()
        'ShellExecute 0, "open", "c:\exam.bat", vbNullString, vbNullString, SW_SHOW
        Shell ("command /c  c:\exam.bat")End Sub
      

  6.   

    ShellExecute 0, "open", "command", " /c  c:\exam.bat", vbNullString, SW_SHOW
    你的意思是这样写吗?我真的才学用,还有上面第一位写的,为什么复制过来没用,窗口仍然在。
    第二位写的没看懂,能不能中文说明一下,还有我怎么才能看懂,要看什么书才好,谢谢各位帮助一定会给分。
      

  7.   

    你要是98
    Shell ("command /c  c:\exam.bat"),vbHide你要是2000
    Shell ("cmd /c  c:\exam.bat"),vbHide
      

  8.   

    1.按住Alt不放,双击BAT文件图标,出现 XXX.BAT“属性(Properties)”;
    2.将“正常窗体(Normal windows)”改成“最小化(Minimized).”