vb 通过窗口标题获取对应进程的所有线程失败 求高手解答原因或给一份可用代码 50
模块代码PublicDeclareFunctionFindWindowLib"user32"Alias"FindWindowA"(ByVallpClassNameAsSt...
模块代码
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
'Public Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lpte As PROCESSENTRY32) As Boolean
'Public Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lpte As PROCESSENTRY32) As Boolean
Public Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Boolean
Public Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Boolean
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Type THREADENTRY32
dwsize As Long
cntusage As Long
th32threadID As Long '线程ID
th32OwnerProcessID As Long '对比这个值是否为指定进程的PID
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type
Public ThreadInfo As THREADENTRY32
Public MySnapHandle As Long
部分窗体代码
Hwnds = FindWindow(vbNullString, Text1.Text)
If Hwnds <> 0 Then
Call GetWindowThreadProcessId(Hwnds, PID)
If PID <> 0 Then
MySnapHandle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)
If MySnapHandle <> 0 Then
ThreadInfo.dwsize = Len(ThreadInfo)
Call Thread32First(MySnapHandle, ThreadInfo)
Do
If ThreadInfo.th32OwnerProcessID = PID Then
List1.AddItem ThreadInfo.th32threadID
End If
Loop While Thread32Next(MySnapHandle, ThreadInfo) <> 0
Call CloseHandle(MySnapHandle)
End If
End If
End If
text1.text存着窗口标题 到最后list1中只会出现一个0 测试好像是因为Thread32First失败 展开
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
'Public Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lpte As PROCESSENTRY32) As Boolean
'Public Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lpte As PROCESSENTRY32) As Boolean
Public Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Boolean
Public Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Boolean
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Type THREADENTRY32
dwsize As Long
cntusage As Long
th32threadID As Long '线程ID
th32OwnerProcessID As Long '对比这个值是否为指定进程的PID
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type
Public ThreadInfo As THREADENTRY32
Public MySnapHandle As Long
部分窗体代码
Hwnds = FindWindow(vbNullString, Text1.Text)
If Hwnds <> 0 Then
Call GetWindowThreadProcessId(Hwnds, PID)
If PID <> 0 Then
MySnapHandle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)
If MySnapHandle <> 0 Then
ThreadInfo.dwsize = Len(ThreadInfo)
Call Thread32First(MySnapHandle, ThreadInfo)
Do
If ThreadInfo.th32OwnerProcessID = PID Then
List1.AddItem ThreadInfo.th32threadID
End If
Loop While Thread32Next(MySnapHandle, ThreadInfo) <> 0
Call CloseHandle(MySnapHandle)
End If
End If
End If
text1.text存着窗口标题 到最后list1中只会出现一个0 测试好像是因为Thread32First失败 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询