首页
社区
课程
招聘
[求助]一段vb6.0的监视进程的代码帮忙看看哪里错误
发表于: 2015-1-4 21:32 5894

[求助]一段vb6.0的监视进程的代码帮忙看看哪里错误

2015-1-4 21:32
5894
监视某个进程是否在运行,如果正在运行,则结束,并且不让他再运行。
举个例子,一个程序的标题名称是111,就是程序窗体左上角的那个名称是111。进程名称不固定,程序重命名后进程名称就变了,可以根据程序的标题名称来查找进程名称。

VB运行后,先查看111是否在运行,如果正在运行,结束111对应的进程,并且不让111对应的进程重新运行,如果没有运行,就监视进程,发现111对应的进程运行立刻结束,并且不让111对应的进程重新运行。

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId 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 Declare Function GetModuleBaseName Lib "psapi.dll" Alias "GetModuleBaseNameA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpBaseName As String, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Command1_Click()
Dim hwnd&, Pid&, pHandle&, str As String * 20
hwnd = FindWindow(vbNullString, "计算器") '这里测试的是计算器
If (hwnd = 0) Then Exit Sub
GetWindowThreadProcessId hwnd, Pid
pHandle = OpenProcess(&H1F0FFF, 0, Pid)
If (pHandle = 0) Then Exit Sub
GetModuleBaseName pHandle, 0, str, Len(str)
MsgBox "进程名为:" & Trim(str), , "哈哈" '计算器的进程名称
CloseHandle hProcess

Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "& Trim(str),")
Debug.Print winHwnd
If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "Error posting message."
End If
Else
MsgBox "The Calculator is not open."
End If
End Sub

[注意]APP应用上架合规检测服务,协助应用顺利上架!

收藏
免费 0
支持
分享
最新回复 (11)
雪    币: 223
活跃值: (516)
能力值: ( LV13,RANK:520 )
在线值:
发帖
回帖
粉丝
2
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long


结束进程用这个就行了.

"Don't call me,I will call you."
vb对线程支持不是很好, 你可以用time控件解决.
或者直接用wmi的功能,也可以获取进程创建的消息.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId 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 Declare Function GetModuleBaseName Lib "psapi.dll" Alias "GetModuleBaseNameA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpBaseName As String, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long



Private Sub Form_Load()
    Timer1.Enabled = True
    Timer1.Interval = 500
End Sub

Private Sub Timer1_Timer()
    
    
    Dim hwnd&, Pid&, pHandle&, str As String * 20
    hwnd = FindWindow(vbNullString, "计算器")                                   '这里测试的是计算器
    If (hwnd = 0) Then Exit Sub
    GetWindowThreadProcessId hwnd, Pid
    pHandle = OpenProcess(&H1F0FFF, 0, Pid)
    If (pHandle = 0) Then Exit Sub
    GetModuleBaseName pHandle, 0, str, Len(str)
    MsgBox "进程名为:" & Trim(str), , "哈哈"                                   '计算器的进程名称
    TerminateProcess pHandle, 0
    CloseHandle hProcess
End Sub



希望能帮到你.
2015-1-4 22:26
0
雪    币: 459
活跃值: (166)
能力值: ( LV9,RANK:165 )
在线值:
发帖
回帖
粉丝
3
Dim hwnd&, Pid&, pHandle&, str As String * 20
hwnd = FindWindow(vbNullString, "计算器") '这里测试的是计算器
If (hwnd = 0) Then Exit Sub
GetWindowThreadProcessId hwnd, Pid
pHandle = OpenProcess(&H1F0FFF, 0, Pid)
If (pHandle = 0) Then Exit Sub
GetModuleBaseName pHandle, 0, str, Len(str)
MsgBox "进程名为:" & Trim(str), , "哈哈" '计算器的进程名称
CloseHandle pHandle

这段代码缺了PostMessage的API声明,并且最后一行Close的对象。。。

之后的代码问题,LZ是要循环检测,好歹给个timer或者LOOP吧,后面findwindow赋值的窗体名也不能用引号打着,后面的就建议LZ重写一遍
2015-1-4 22:30
0
雪    币: 735
活跃值: (130)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
非常感谢 OK啦
2015-1-4 22:55
0
雪    币: 735
活跃值: (130)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
感谢帮助
2015-1-4 22:56
0
雪    币: 459
活跃值: (166)
能力值: ( LV9,RANK:165 )
在线值:
发帖
回帖
粉丝
6
enen,没事,也谢谢楼上的啦,学习了
2015-1-5 12:27
0
雪    币: 735
活跃值: (130)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
还有一种情况 顺便再请教下 如果一个程序的名称和标题名称均不固定,如何获得进程名称?
2015-1-5 14:18
0
雪    币: 223
活跃值: (516)
能力值: ( LV13,RANK:520 )
在线值:
发帖
回帖
粉丝
8
你是想要做补丁吗?

方法很多,
模糊窗口名.
枚举类名.
枚举控件名或个数.
读内存特征.等等. 都能实现的.
2015-1-5 16:42
0
雪    币: 622
活跃值: (294)
能力值: ( LV13,RANK:410 )
在线值:
发帖
回帖
粉丝
9
我觉得这样的情况,直接结束进程不好,不如枚举线程,然后把每个线程都来个suspend?
2015-1-5 16:49
0
雪    币: 223
活跃值: (516)
能力值: ( LV13,RANK:520 )
在线值:
发帖
回帖
粉丝
10
互相学习哈.

有不少sdk程序,直接pending wm_close了.
所以有的postmessage也无效.
最好还是直接暴力结束  
2015-1-5 16:52
0
雪    币: 223
活跃值: (516)
能力值: ( LV13,RANK:520 )
在线值:
发帖
回帖
粉丝
11
nice, 我觉得你这个建议真不错哈.  

应该给x作者写个建议信.
让其在指定的x环境下运行时,
如果遇到了x环境有x数字,或者x星,或者x山,
就自动暂停线程.这样就很有可能就避免被x直接结束进程了.  
数日后,x作者可能会写感谢信回来,
说不定还会有精美小礼物呢.  

然后再给x数字x星x山再写一封建议信,
让其检测到x软件的时候,不要结束进程,而直接让其暂停.
让x作者误以为正常运行ing...
数日后,x数字x星x山可能会写感谢信回来,
说不定还会有精美小礼物呢.  
2015-1-5 17:01
0
雪    币: 459
活跃值: (166)
能力值: ( LV9,RANK:165 )
在线值:
发帖
回帖
粉丝
12
膜拜一下 八两。。。
哈哈,话说terminate之后也最好给文件来个不close的open,省的用timer咯
2015-1-5 22:16
0
游客
登录 | 注册 方可回帖
返回
//