2018年舟山房价:VB如何结束进程

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/03 02:24:56
请问用VB如何监视进程,如果进程中有2.exe时则终止它的进程。没有的话则继续监视呢????在线等!请高手帮帮忙,谢谢!

end是不可以的,因为end函数是不能跨进程的,只能在本进程当中

使用,你可以使用vb的api函数sendmessage函数即可,sendmessage(hwnd,

WM_CLOSE,0,0)

这其中hwnd就是2.exe的窗口句柄,你可以在一个定时器当中对所有的窗口

进行列举enumwindows,得到你要关闭的程序窗口的句柄,然后发送消息。

SendMessage 一般是不会奏效的
用API 函数,先枚举

API 声明到 API 浏览器里找

Public Sub EnumProcess()
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim myProcess As Long
Dim szExename As String
Dim i as Integer

uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)

Do While rProcessFound

i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
'在这里添加判断代码
If szExename = "2.exe" then
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
TerminateProcess(myProcess,0)
end if

rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop

Call CloseHandle(hSnapshot)

End Sub

如果是 windows NT 系统,还要提升程序权限
在百度输入 “OpenProcessToken” “提升权限”,一般能搜到代码

Exit Sub 或 END

END