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

canca7年前 (2017-12-14)VB331

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   

相关文章

在VB中实现MD5算法

——————————————————————————————————————Option Explicit Dim w1 As String, w2 As String, w3 As Strin...

VB小作品--成语大全

       两三年没用过VB开发过啦!今天我用VB开发了个《成语大全》感觉还OK吧!本来想用.Net开发的,它那个框架这么吓人,...

VB BHO

以前一直不知道有BHO这个名字,但一直对IE中的"加载项"编程念念不忘. 前不久一网友让我做一个小程序,功能就是替换网页中指定的内容,但是不启动软件,直接用IE看的话就是原文.感觉有点意思的,因为不知...

就类似于3721的插件一样,当用户浏览我的网站的时候,提示下载运行插件

实现方法如下: 首先需要获得IObjectWithSite接口的定义,你下载个olelib.tlb,包含了接口定义。然后创建一个ActiveX DLL工程, 在工程中引用这个tlb文件,...

我的网友第一个bho作品

转自:http://hi.baidu.com/zzjwl/blog/item/60ecf8de3bf4bf5c95ee3725.html 去年用vb做bho,没有成功,最近偶然到网络搜索,有那么多资料...

VB 控制QQ发消息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''  程序功能...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。