VB中关闭shell打开的外部EXE程序

来源:互联网 发布:打击网络犯罪新闻评论 编辑:程序博客网 时间:2024/05/17 17:14

Private   Declare   Function   TerminateProcess   Lib   "kernel32"   _   
  (ByVal   hProcess   As   Long,   ByVal   uExitCode   As   Long)   As   Long   
    
  Const   PROCESS_QUERY_INFORMATION   =   &H400   
  Private   hProcess   As   Long   
  -----------------在程序中------------------   
          Dim   pid   As   Long   
            
          pid   =   Shell("notepad.exe",   vbNormalFocus)   
          hProcess   =   OpenProcess(PROCESS_QUERY_INFORMATION,   0,   pid)   
  ----------------如果要关闭-----------------   
  If   hProcess   <>   0   Then   
  aa   =   TerminateProcess(hProcess,   3838)   
  End   If   

 ***********************************************************************************************************

ExitProcess 

VB声明 
Declare Sub ExitProcess Lib "kernel32" Alias "ExitProcess" (ByVal uExitCode As Long) 
说明 
中止一个进程 
参数表 
参数 类型及说明 
uExitCode Long,指定想中断的那个进程的一个退出代码 
在VB中使用 
应尽量避免用该函数来关闭进程。不要在自己的VB程序中使用它。此时,应试着向要关闭的那个程序的主窗口投递一条WM_CLOSE消息 
***************************************************************************************************************
 Option   Explicit   
    
  Private   Declare   Function   TerminateProcess   Lib   "kernel32"   (ByVal   hProcess   As   Long,   ByVal   uExitCode   As   Long)   As   Long   
  Private   Declare   Function   OpenProcess   Lib   "kernel32"   _   
        (ByVal   dwDesiredAccess   As   Long,   ByVal   bInheritHandle   As   Long,   _   
          ByVal   dwProcessID   As   Long)   As   Long   
  Private   Const   PROCESS_TERMINATE   =   &H1   
  Private   hProcess   As   Long   
  '打开进程   
  Private   Sub   Command1_Click()   
          Dim   pid   As   Long   
          pid   =   Shell("c:/winnt/System32/calc.exe",   vbNormalFocus)   
          If   pid   =   0   Then   
                  MsgBox   "没有打开程序"   
          Else   
                  hProcess   =   OpenProcess(PROCESS_TERMINATE,   0,   pid)   
          End   If   
  End   Sub   
  '关闭进程   
  Private   Sub   Command2_Click()   
          Dim   l   As   Long   
          l   =   TerminateProcess(hProcess,   1)   
          If   l   <>   0   Then   
                  MsgBox   "成功关闭"   
          Else   
                  MsgBox   "未关闭"   
          End   If   
  End   Sub   

0 0