Results 1 to 16 of 16

Thread: [ReQ Help] Get Handle from PID Please HELP

  1. #1

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Unhappy [ReQ Help] Get Handle from PID Please HELP

    Hello Everyone I Just Joined Your Comunity and i was wondering if someone can help me i searched all Google and as result i stumbled up to this forum i looked up some threads and saw that the programmers that are registered here are good and realy understand what they are writing so here is my question :

    I'm Making A Program for a game i'm playing and i'm using ReadMemory to get stuff etc... and much of the players (me aswell) are using a program that renames the handle so i need to get the name of the handle from the PID (process ID) can someone make an example ? ty.
    PS: The PID (process ID is : "sro_client.exe")

    Regards:
    ~Cha0sBG
    Last edited by Cha0sBG; Jun 22nd, 2008 at 06:51 PM.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [ReQ Help] Get Handle from PID Please HELP

    Not sure know what you mean by renaming a handle but you might want to take a look at the zip file in this thread as it has some code that might help.

  3. #3

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Re: [ReQ Help] Get Handle from PID Please HELP

    no it dousnt help ok i try to explain it again (my english isn't verry good)
    at the task maneger in the process area the game is called "sro_client.exe"
    but at the taskbar the window normal called "sro_client" is renamed to difrent stuff like Troy - Cha0sBG and stuff i need a way to detect what it's called at the taskbar window

  4. #4

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Re: [ReQ Help] Get Handle from PID Please HELP

    so can anyone help with this ?

  5. #5

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Re: [ReQ Help] Get Handle from PID Please HELP

    first i whoud like to say sry for the spam
    second i found something but dousnt work ... related to my question
    vb Code:
    1. strQuery = "select * from Win32_Process"
    2.  
    3. strMoniker = "winmgmts:\\" & Trim(strComputerName)
    4. Set refWMI = GetObject(strMoniker)
    5.  
    6. Set colProcesses = refWMI.ExecQuery(strQuery)
    7.         If colProcesses.count = 0 Then
    8.         Else
    9.             For Each refProcess In colProcesses
    10.                 If refProcess.Terminate() = 0 Then
    11.                     Debug.Print "(PID " & refProcess.Handle & ") " _
    12.                     & refProcess.Name & " Terminated on - " _
    13.                     & strComputerName
    14.                 Else
    15.                     Debug.Print "Unable to terminate - " _
    16.                     & refProcess.Name & " on " _
    17.                     & strComputerName & Chr(13)
    18.                 End If
    19.             Next
    20.         End If
    21.  
    22.  
    23. End Sub

    can someone help with it ? ty.
    i like it to search the process for process called "sro_client.exe" and give the name of the handle on the taskbar of that process.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [ReQ Help] Get Handle from PID Please HELP

    That isn't renaming the handle, which you can't do anyway that I know of, it is changing the text in the window caption. Have a look at this thread for an example of how to get the caption of an external window.

  7. #7

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Re: [ReQ Help] Get Handle from PID Please HELP

    well i wanna get the handle name of a window on the taksbar from it's Process ID

  8. #8

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Re: [ReQ Help] Get Handle from PID Please HELP

    vb Code:
    1. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    2. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    3. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    4. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    5.  
    6. Private Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String
    7.  
    8. Dim i As Long
    9. Dim j As Long
    10.  
    11. i = GetForegroundWindow
    12.  
    13. If ReturnParent Then
    14. Do While i <> 0
    15. j = i
    16. i = GetParent(i)
    17.  Loop
    18. i = j
    19. End If
    20. GetActiveWindowTitle = GetWindowTitle(i)
    21. End Function
    22.  
    23. Private Function GetWindowTitle(ByVal hwnd As Long) As String
    24. Dim l As Long
    25. Dim s As String
    26. l = GetWindowTextLength(hwnd)
    27. s = Space(l + 1)
    28. GetWindowText hwnd, s, l + 1
    29. GetWindowTitle = Left$(s, l)
    30. End Function
    ok there is the code but how do i make this code to search for process "sro_client.exe" and return the handle of the active window in the task bar ?

  9. #9

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Re: [ReQ Help] Get Handle from PID Please HELP


    Cuz There Where it says "Mercury - LiGh7St0rM" Everytime is difrent name

  10. #10

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Re: [ReQ Help] Get Handle from PID Please HELP

    come on even after my pic explain noone is able to understand ?

  11. #11
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [ReQ Help] Get Handle from PID Please HELP

    Quote Originally Posted by Cha0sBG
    no it dousnt help ok i try to explain it again (my english isn't verry good)
    at the task maneger in the process area the game is called "sro_client.exe"
    but at the taskbar the window normal called "sro_client" is renamed to difrent stuff like Troy - Cha0sBG and stuff i need a way to detect what it's called at the taskbar window
    I would assum the taskbar text for a program would be the same as it's titlebar text which my example should return when you pass it sro_client.exe, but I guess it's not the same thing. Sorry couldn't help, good luck!

  12. #12

    Thread Starter
    Member Cha0sBG's Avatar
    Join Date
    Jun 2008
    Location
    C:\Windows\System32
    Posts
    43

    Re: [ReQ Help] Get Handle from PID Please HELP

    ahh i just want to get the name of the window on the taskbar cuz most time it's renamed ;(

  13. #13
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: [ReQ Help] Get Handle from PID Please HELP

    when you look on an API spy, is the Classname of the game window always the same? you can do a fast,easy, search using that if it is(it usually always is, but they can change it to whatever they like when creating it if they desire).

    from all the API i've encountered i've found ways to retrieve the exe name if i know the handle, but never the handle of the window if you know the exe name. The problem with it is that there can be alot of windows associated with an exe file so pointing to one specific window isn't easy. i was going to tell you how to do it but i figured i'd just write it myself i took this from tidbits of code i have laying around and changed it to fit my needs. hopefully this works successfully for you.

    Code:
    Option Explicit
    Private Const MAX_PATH As Long = 260
    
    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 Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
        Private Const TH32CS_SNAPPROCESS As Long = 2&
    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 Const GW_CHILD = 5
        Private Const GW_HWNDNEXT = 2
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
    
    Private Sub Form_Load()
    
    Dim MyExe As String
    Dim myProcessID As Long
    Dim MyHandle As Long
    
    MyExe = "MyExeProgram.exe" 'case sensitive unless you wanna easily change it
    myProcessID = FindProcessID(MyExe)
    MyHandle = FindtheWindow(myProcessID)
    Me.Caption = "Window Handle = " & MyHandle
    
    End Sub
    
    Private Function FindProcessID(ByVal pExename As String) As Long
    
        Dim ProcessID As Long, hSnapShot As Long
        Dim uProcess As PROCESSENTRY32, rProcessFound As Long
        Dim Pos As Integer, szExename As String
        
        ' Create snapshot of current processes
        hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        ' Check if snapshot is valid
        If hSnapShot = -1 Then
            Exit Function
        End If
        'Initialize uProcess with correct size
        uProcess.dwSize = Len(uProcess)
        'Start looping through processes
        rProcessFound = ProcessFirst(hSnapShot, uProcess)
        Do While rProcessFound
            Pos = InStr(1, uProcess.szExeFile, vbNullChar)
            If Pos Then
                szExename = Left$(uProcess.szExeFile, Pos - 1)
            End If
            If szExename = pExename Then
                'Found it
                ProcessID = uProcess.th32ProcessID
                Exit Do
              Else
                'Wrong, so continue looping
                rProcessFound = ProcessNext(hSnapShot, uProcess)
            End If
        Loop
        CloseHandle hSnapShot
        FindProcessID = ProcessID
    
    End Function
    
    Private Function FindtheWindow(ByVal ProcessID As Long) As Long
    
    Dim cHandle As Long
    Dim RetHwnd As Long
    Dim myProcessID As Long
    
    cHandle = GetDesktopWindow()
    RetHwnd = GetWindow(cHandle, GW_CHILD)
    Do While RetHwnd
        If IsWindowVisible(RetHwnd) Then
            Call GetWindowThreadProcessId(RetHwnd, myProcessID)
            If myProcessID = ProcessID Then
                Exit Do
            End If
        End If
        RetHwnd = GetWindow(RetHwnd, GW_HWNDNEXT)
    Loop
    FindtheWindow = RetHwnd
    
    End Function

  14. #14
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: [ReQ Help] Get Handle from PID Please HELP

    well i was looking at your question and seen i had forgotten to tell you how to get the caption from the Handle, i only got the handle to it..
    just add this to the code i provided above in the appropriate locations.
    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
        Private Const WM_GETTEXT As Long = &HD
        Private Const WM_GETTEXTLENGTH As Long = &HE
    
    Private Function GetText(ByVal WindowHandle As Long) As String
    'gets the text of a textbox or a caption of most controls
       
      Dim Buffer As String
      Dim TextLength As Long
       
       TextLength = SendMessage(WindowHandle, WM_GETTEXTLENGTH, 0&, 0&)
       Buffer = String$(TextLength, 0&)
       SendMessage WindowHandle, WM_GETTEXT, TextLength + 1, Buffer
       GetText = Buffer
       
    End Function
    
    Private Sub Form_Load()
    
    Me.Caption = GetText(MyHandle)
    
    End Sub

  15. #15
    New Member
    Join Date
    Mar 2021
    Posts
    1

    Re: [ReQ Help] Get Handle from PID Please HELP

    Hi Billy,

    Thank you for sharing this and for your efforts.

    I am about to use this code for my commercial application.

    Can I use it Billy ? With your kind permission ?

    Kind Regards,
    Veera Raghavan
    +91 9371074189

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: [ReQ Help] Get Handle from PID Please HELP

    Considering that they posted this 13 years ago, I would say that they won't even see your request. If you use it, just add a comment mentioning who you got it from, and where.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width