vb 进程句柄

已知有3个AA.exe进程怎么分别获取这3个进程的句柄最好写个例子出来:_)谢谢了----------------网上找了些资料但不会用--||-------------... 已知有3个AA.exe进程
怎么分别获取这3个进程的句柄

最好写个例子出来 :_)
谢谢了
----------------

网上找了些资料 但不会用- -||
---------------------------
GetWindowThreadProcessId ihwnd, pid 得到进程ID
hProcess = OpenProcess(&H1F0FFF, False, pid) 获得进程句柄
大老 顺便在text2显示一下改进程句柄吧 谢谢了
展开
 我来答
帐号已注销
2009-02-21 · TA获得超过717个赞
知道小有建树答主
回答量:81
采纳率:0%
帮助的人:113万
展开全部
已经知道某程序的文件名,用VB结束该程序
如果知道该程序在任务管理器中的名字,又如何呢?

用CreateToolhelp32SnapShot创建一个SnapShot句柄,
用Process32First来获取第一个进程,
判断进程的szExeFile是不是你所指定的进程名
用TerminateProcess关闭进程(注意,不能关闭比自己级别高的进程,修改进程级别用AdjustTokenPrivileges)
用Process32Next查看下一个进程,循环.

怎么样,说的够清楚吧

能够举个例子吗?例如我要结束的进程是ABC.exe,级别比自己高的!怎么强制结束这个程序呢?谢谢!
修改进程级别用AdjustTokenPrivileges
Option Explicit
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Private Const MAX_PATH As Integer = 260
Private Const PROCESS_TERMINATE = &H1

Private 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 * MAX_PATH
End Type

Private Sub Command1_Click()
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Dim theloop As Long
Dim ret As ListItem
ListView1.ListItems.Clear '清空所有内容
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile
Set ret = ListView1.ListItems.Add(, "first" & CStr(i), exename) '将进程名添加到第一项中
ListView1.ListItems("first" & CStr(i)).SubItems(1) = proc.th32ProcessID '将进程ID添加到第二项中
i = i + 1
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap '关闭进程“快照”句柄
End Sub

'终止进程
Private Sub Command2_Click()
Dim i As Long
Dim hand As Long
hand = OpenProcess(PROCESS_TERMINATE, True, CLng(ListView1.SelectedItem.SubItems(1))) '获取进程句柄
TerminateProcess hand, 0 '关闭进程
Call Command1_Click '调用查看进程来刷新进程列表
End Sub

Private Sub Form_Load()
Dim header As ColumnHeader
ListView1.View = lvwReport
ListView1.ColumnHeaders.Clear
Set header = ListView1.ColumnHeaders.Add(, "first", "进程", 4000) '设置ListView中项目的宽度,读者也可自行设置
Set header = ListView1.ColumnHeaders.Add(, "second", "ID", 1400)
ListView1.Refresh
End Sub
zengqh0314
2009-02-04 · TA获得超过183个赞
知道小有建树答主
回答量:820
采纳率:100%
帮助的人:501万
展开全部
Private 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
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

'查找进程的函数
Private Function fun_FindProcess(ByVal ProcessName As String) As Long

Private Sub Command1_Click()
Dim hWnd As Long ' 储存 FindWindow 函数返回的句柄
Dim Pid As Long ' 储存进程标识符
Dim pHandle As Long ' 储存进程句柄
Dim str As String * 20 ' 存储显示文本
' 取得目标窗口的句柄
hWnd = FindWindow(vbNullString, "计算器")
If (hWnd = 0) Then Exit Sub
' 取得进程标识符
GetWindowThreadProcessId hWnd, Pid
' 取得进程句柄
pHandle = OpenProcess(&H1F0FFF, 0, Pid)
Print pHandle
If (pHandle = 0) Then Exit Sub
' 读取内存数据
ReadProcessMemory pHandle, &H40B181, str, 20, 0& 'PROCESS_ALL_ACCESS不知道你定义了没有,我换成直接的值,还有False换成了0
' 在文本框显示
Text2.Text = str
' 关闭进程句柄
CloseHandle hProcess
End Sub

这个例子够你明白的了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yuxuegongwu521
2009-02-19
知道答主
回答量:37
采纳率:0%
帮助的人:0
展开全部
楼上的说的很对你复制一下就行了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式