| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4235 人关注过本帖
标题:获取不到进程句柄 pHandle,请大侠帮帮忙,看哪里出问题了
只看楼主 加入收藏
顾海洋凡
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-7-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
获取不到进程句柄 pHandle,请大侠帮帮忙,看哪里出问题了
大侠,看一下想获得QQ的进程句柄,为什么phandle总是得到0呢?   运行环境win7 32位旗舰版

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function writeprocessmemory Lib "kernel32" (ByVal hprocess As Long, ByVal ipbaseaddress As Any, ByVal ipbuffer As Any, ByVal nsize As Long, ipnumberofbyteswritten As Long) As Long
Private Declare Function readprocessmomory Lib "kernel32" (ByVal hprocess As Long, ByVal ipbaseaddress As Any, ByVal ipbuffer As Any, ByVal nsize As Long, ipnumberifbyteswritten As Long) As Long
Private Declare Function closehandle Lib "kernel32" (ByVal hobject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Sub Command1_Click()
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "QQ")  '得到窗口句柄
Label3.Caption = hwnd
If hwnd = 0 Then

Label1.Caption = "程序未运行"  '验证是否得到有效窗口句柄
Else
Label1.Caption = "程序已运行"
End If
Dim pid As Long
Dim pHandle As Long
GetWindowThreadProcessId hwnd, pid      '得到进程标识符
Label2.Caption = pid
pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid) '通过进程标识符获得进程句柄
If pHandle = 0 Then
MsgBox "couldn 't get a process handle!"
Else
Label4.Caption = pHandle

End If
End Sub



[此贴子已经被作者于2016-7-28 15:23编辑过]

搜索更多相关主题的帖子: 运行环境 旗舰 
2016-07-28 15:09
顾海洋凡
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-7-9
收藏
得分:0 
回复 楼主 顾海洋凡
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册



这是运行前和运行后的结果,
2016-07-28 15:24
xiangyue0510
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:86
帖 子:938
专家分:5244
注 册:2015-8-10
收藏
得分:10 
这个没有用过,百度了一下,可能是软件设置了内存保护
http://tieba.baidu.com/p/249145093?pid=2432010602&cid=0
2016-07-28 16:50
liuzhaoyzz
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:17
注 册:2016-7-21
收藏
得分:10 
程序代码:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function writeprocessmemory Lib "kernel32" (ByVal hprocess As Long, ByVal ipbaseaddress As Any, ByVal ipbuffer As Any, ByVal nsize As Long, ipnumberofbyteswritten As Long) As Long
Private Declare Function readprocessmomory Lib "kernel32" (ByVal hprocess As Long, ByVal ipbaseaddress As Any, ByVal ipbuffer As Any, ByVal nsize As Long, ipnumberifbyteswritten As Long) As Long
Private Declare Function closehandle Lib "kernel32" (ByVal hobject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Const PROCESS_ALL_ACCESS = &H1F0FFF                                             '这是个常量,必须要指定数值

Private Sub Command1_Click()
    Dim hwnd As Long
    '        hwnd = FindWindow(vbNullString, "QQ")         '得到窗口句柄
    hwnd = FindWindow("TXGuiFoundation", vbNullString)                          '查找软件的句柄
    Label3.Caption = hwnd
    If hwnd = 0 Then
        Label1.Caption = "程序未运行"                                           '验证是否得到有效窗口句柄
    Else
        Label1.Caption = "程序已运行"
    End If
    Dim pid As Long
    Dim pHandle As Long
    GetWindowThreadProcessId hwnd, pid                                          '得到进程标识符
    Label2.Caption = pid
    pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)                       '通过进程标识符获得进程句柄
    If pHandle = 0 Then
        MsgBox "couldn 't get a process handle!"
    Else
        Label4.Caption = pHandle
    End If
End Sub

Private Sub Command2_Click()
    End
End Sub


1、Const PROCESS_ALL_ACCESS = &H1F0FFF     '这是个常量,必须要指定数值
2、hwnd = FindWindow("TXGuiFoundation", vbNullString)    '通过类名查找软件的句柄更准确,可用SpyLite24或者viewwizard查到。
获得句柄.rar (5.79 KB)


[此贴子已经被作者于2016-8-1 17:34编辑过]

2016-08-01 17:33
liuzhaoyzz
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:17
注 册:2016-7-21
收藏
得分:0 
by the way,findwindow不支持模糊查找,如果要模糊查找,可以参考以下代码:
程序代码:
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Sub getprocessname(title, hwnd)
    Dim lngDeskTopHandle As Long
    Dim lngHand As Long
    Dim strName As String * 255
    Dim tiele As String
    lngDeskTopHandle = GetDesktopWindow()
    lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
    Do While lngHand <> 0
        GetWindowText lngHand, strName, Len(strName)
        lngHand = GetWindow(lngHand, GW_HWNDNEXT)
        If Left$(strName, 1) <> vbNullChar Then
            If InStr(strName, title) Then
                hwnd = FindWindow(vbNullString, CStr(strName))
                '此处hwnd即所需句柄,您可以在此处中断并获取该句柄,或将句柄数据写入数组以获取所有包含title的窗口句柄
            End If
        End If
    Loop
End Sub

Private Sub Command1_Click()
    getprocessname "QQ", hwnd
    Label1 = hwnd
End Sub

vb模糊查找标题.rar (5.68 KB)


[此贴子已经被作者于2016-8-2 06:41编辑过]

2016-08-02 06:39
顾海洋凡
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2016-7-9
收藏
得分:0 
回复 4楼 liuzhaoyzz
用您的方法已经解决了——非常感谢
2016-08-02 15:23
liuzhaoyzz
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:17
注 册:2016-7-21
收藏
得分:0 
以类名为主,部分标题查找为辅来获取QQ主窗口句柄
hwnd = FindWindow("TXGuiFoundation", "QQ International")  虽然能一步到位,但是不支持不同的QQ版本,例如国内版/国际版。
通过类名查找QQ窗口句柄,Hwnd = FindWindow("TXGuiFoundation", vbNullString) ,结果发现结果不对,和spylite或者viewwizard探测出来的结果不同,用spy++看了下,QQ后台有很多类名为"TXGuiFoundation"的进程,标题为空,FindWindow找到的是第一个类名为"TXGuiFoundation"的窗口句柄,不是QQ的主窗口,而QQ的主窗口标题为"QQ"/"QQ International"之类的。然后修改了代码,先通过FindWindow找第一个"TXGuiFoundation"的窗口类,然后用GetWindowText这个API反查窗口标题title,再进行模糊比对,看下title中是否含有QQ或者qq的关键字,再用FindWindowEx这个API来查找同级的下一个窗口,Hwnd2 = FindWindowEx(0, Hwnd2, "TXGuiFoundation", vbNullString),再看下title中是否含有QQ或者qq的关键字,循环查找,直至找到正确的标题,这样就得到了准确的结果。这个方法有一定的通用性,就是通过以类名为主+部分标题关键字查找为辅的方法。
代码如下:
程序代码:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function writeprocessmemory Lib "kernel32" (ByVal hprocess As Long, ByVal ipbaseaddress As Any, ByVal ipbuffer As Any, ByVal nsize As Long, ipnumberofbyteswritten As Long) As Long
Private Declare Function readprocessmomory Lib "kernel32" (ByVal hprocess As Long, ByVal ipbaseaddress As Any, ByVal ipbuffer As Any, ByVal nsize As Long, ipnumberifbyteswritten As Long) As Long
Private Declare Function closehandle Lib "kernel32" (ByVal hobject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Const PROCESS_ALL_ACCESS = &H1F0FFF                                             '这是个常量,必须要指定数值
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal Hwnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

'以下函数通过hwnd反查窗口标题:
Public Function HwndToTitle(Hwnd As Long) As String
    Dim strText As String * 256
    Call GetWindowText(Hwnd, strText, 256)
    HwndToTitle = Left$(strText, InStr(1, strText, Chr(0)) - 1)
End Function

Private Sub Command1_Click()
    Dim Hwnd As Long
    Dim Hwnd2 As Long
    '        hwnd = FindWindow(vbNullString, "QQ")         '得到窗口句柄
    '    hwnd = FindWindow("TXGuiFoundation", "QQ International")                    '查找QQ窗口句柄
    Hwnd = FindWindow("TXGuiFoundation", vbNullString)                          '通过类名查找QQ窗口句柄
    Title = HwndToTitle(Hwnd)                                                   '通过hwnd反查标题
    Hwnd2 = 0
    Do While InStr(Title, "QQ") = 0 And InStr(Title, "qq") = 0                  '模糊查询标题是否含有QQ关键字
        Hwnd2 = FindWindowEx(0, Hwnd2, "TXGuiFoundation", vbNullString)
        Title = HwndToTitle(Hwnd2)                                              '通过hwnd反查标题
        If Hwnd2 = 0 Then Exit Do                                               '如果再也找不到"TXGuiFoundation"退出循环
    Loop
    If Hwnd2 <> 0 Then Hwnd = Hwnd2
    Label3.Caption = Hwnd
    If Hwnd = 0 Then
        Label1.Caption = "QQ程序未运行"                                         '验证是否得到有效窗口句柄
    Else
        Label1.Caption = "QQ程序已运行"
    End If
    Dim pid As Long
    Dim pHandle As Long
    GetWindowThreadProcessId Hwnd, pid                                          '得到进程标识符
    Label2.Caption = pid
    pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)                       '通过进程标识符获得进程句柄
    If pHandle = 0 Then
        MsgBox "couldn 't get a process handle!"
    Else
        Label4.Caption = pHandle
    End If
End Sub

Private Sub Command2_Click()
    End
End Sub

查找类名和部分标题获得句柄.rar (26.88 KB)

图片附件: 游客没有浏览图片的权限,请 登录注册
2016-08-03 17:40
liuzhaoyzz
Rank: 2
等 级:论坛游民
威 望:1
帖 子:12
专家分:17
注 册:2016-7-21
收藏
得分:0 
所以说来,有些帖子可能在一定的时间内显示可以结贴,但是实际上没有结贴。
随着认识的深入,总会有人有更深入的理解,写出更加高质量的代码。
2016-08-03 17:44
快速回复:获取不到进程句柄 pHandle,请大侠帮帮忙,看哪里出问题了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026958 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved