枚举进程的线程,大家看看我写的哪错了?

m9832008 2008-03-02 01:26:56
现在可以列出所有进程,但是不会列出线程,调用ThreadFirst返回的是0,
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 ThreadFirst Lib "kernel32" Alias "Thread32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ThreadNext Lib "kernel32" Alias "Thread32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Type PROCESSENTRY32
dwSize As Long
cntUseage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
swFlags As Long
szExeFile As String * 1024
End Type
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmodule = &H8
Private Sub Command1_Click()
Call process
End Sub
Private Sub Command2_Click()
Call thread(0)
End Sub
Private Sub List1_Click()
Call thread(List1.List(List1.ListIndex))
End Sub
Private Sub thread(pro As Long)
Dim MySnapHandle As Long
Dim threadInfo As PROCESSENTRY32
threadInfo.dwSize = Len(threadInfo)
MySnapHandle = CreateToolhelpSnapshot(TH32CS_SNAPthread, pro)
id = ThreadFirst(MySnapHandle, threadInfo)
List2.Clear
List2.AddItem (id)
Do While id <> 0
List2.AddItem (threadInfo.th32ProcessID)
id = ThreadNext(MySnapHandle, threadInfo)
List2.AddItem (id)
Loop
List2.AddItem (id)
CloseHandle MySnapHandle
End Sub
Private Sub process()
Dim MySnapHandle As Long
Dim ProcessInfo As PROCESSENTRY32
MySnapHandle = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0)
ProcessInfo.dwSize = Len(ProcessInfo)
List1.Clear
id = ProcessFirst(MySnapHandle, ProcessInfo)
Do While id <> 0
List1.AddItem (ProcessInfo.th32ProcessID)
List1.AddItem (ProcessInfo.szExeFile)
id = ProcessNext(MySnapHandle, ProcessInfo)
Loop
CloseHandle MySnapHandle
End Sub

...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
m9832008 2008-03-02
  • 打赏
  • 举报
回复

这个问题找到了,线程用的是
Private Type THREADENTRY32
dwsize As Long
cntusage As Long
th32threadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type
结构。。。
m9832008 2008-03-02
  • 打赏
  • 举报
回复
我就搞不懂了,为什么进程可以列出来,线程就列不出来了???
m9832008 2008-03-02
  • 打赏
  • 举报
回复
这是人家写的VC的。
HANDLE snapshothandle=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
THREADENTRY32 threadentry;
threadentry.dwSize=sizeof(THREADENTRY32);
BOOL finded=Thread32First(snapshothandle,&threadentry);
while(finded!=NULL)
{
//threadentry中返回线程信息
finded=Thread32Next(snapshothandle,&threadentry);
}
CloseHandle(snapshothandle); );
这是我写的VB的
Private Sub thread(pro As Long)
Dim MySnapHandle As Long
Dim threadInfo As PROCESSENTRY32
threadInfo.dwSize = Len(threadInfo)
MySnapHandle = CreateToolhelpSnapshot(TH32CS_SNAPthread, pro)
id = ThreadFirst(MySnapHandle, threadInfo)
List2.Clear
List2.AddItem (id)
Do While id <> 0
List2.AddItem (threadInfo.th32ProcessID)
id = ThreadNext(MySnapHandle, threadInfo)
List2.AddItem (id)
Loop
List2.AddItem (id)
CloseHandle MySnapHandle
End Sub

1,485

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧