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

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   

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值