首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

vb里怎么根据程序的路径杀掉正在运行的程序

2012-03-23 
vb里如何根据程序的路径杀掉正在运行的程序我在做自动升级程序,自动升级程序要关掉正在运行的主程序,但我

vb里如何根据程序的路径杀掉正在运行的程序
我在做自动升级程序,自动升级程序要关掉正在运行的主程序,但我照往上的资料做了不行,比较郁闷.分不够再加

[解决办法]
汗,干嘛发两个
[解决办法]
'杀掉正在运行的进程应该要用API函数,如:TerminateProcess
'以下代码可以关闭进程explorer.exe,我用过的,你可以参考一下
'以下模块代码
Option Explicit
Public Const sEndProess As String = "explorer.exe " '注意必须小写
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32 " (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function Process32First Lib "kernel32 " (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32 " (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function CloseHandle Lib "kernel32 " (ByVal hObject As Long) As Long
Public Declare Function OpenProcess Lib "kernel32 " (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long

Public Declare Function TerminateProcess Lib "kernel32 " (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long

Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 1024
End Type

Public Const TH32CS_SNAPHEAPLIST = &H1
Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPTHREAD = &H4
Public Const TH32CS_SNAPMODULE = &H8
Public Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Public Const TH32CS_INHERIT = &H80000000
' '

Public Sub Exitexplorer()
Dim pid As Long
Dim pname As String
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim flag As Boolean
Dim mName As String
Dim i As Integer

l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then '遍历第一个进程
Do
i = InStr(1, my.szExeFile, Chr(0))
mName = LCase(Left(my.szExeFile, i - 1))
If mName = LCase(sEndProess) Then
pid = my.th32ProcessID
pname = mName
Dim mProcID As Long
mProcID = OpenProcess(1&, -1&, pid)
TerminateProcess mProcID, 1&
flag = True
Exit Sub
Else
flag = False
End If
Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False
End If
l1 = CloseHandle(l)
End If
End Sub

'如下调用
Call Exitexplorer
[解决办法]
不过楼主的意图在你另一个帖子中我回的那样应该就可以了
[解决办法]
杀进程应该主要是针对其他程序和软件. 用于自己产品的升级,应该以进程间通讯的方式做更好.即主程序调用升级程序,升级程序发现新版本则通知主程序退出,主程序收到通知自行退出(此时还可检测是否有什么工作需要保留以提醒用户保存工作),升级程序完成下载更新后,再启动主程序.

热点排行