Results 1 to 7 of 7

Thread: window handle from process id

  1. #1

    Thread Starter
    Hyperactive Member ravi15481's Avatar
    Join Date
    Aug 2002
    Location
    INDIA
    Posts
    421

    Red face window handle from process id

    hi friends,

    how to get the window handle from the process id.

    thanks
    A good friend...

  2. #2
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    I am not sure you can as it's a one to many relationship

    1 process can have many handles
    1 handle has only 1 process

    Do you know the caption/title of the window in question?

    Woka

  3. #3

    Thread Starter
    Hyperactive Member ravi15481's Avatar
    Join Date
    Aug 2002
    Location
    INDIA
    Posts
    421
    i have the exe name
    A good friend...

  4. #4

  5. #5

    Thread Starter
    Hyperactive Member ravi15481's Avatar
    Join Date
    Aug 2002
    Location
    INDIA
    Posts
    421

    Red face

    i want to kill that process..
    A good friend...

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Do a search on VBF for TerminateProcess...
    I was involved in a thread a few weeks back that was soley on killing a process, however, I am not sure how to get the ProcessID from the process name...

    Here's what the API guide has, not sure if it helps much though:
    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    2. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    3. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    4. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    5. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    6. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    7. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    8. Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    9. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    10. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    11. Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    12. Const GW_HWNDNEXT = 2
    13. Dim mWnd As Long
    14. Function InstanceToWnd(ByVal target_pid As Long) As Long
    15.     Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
    16.     'Find the first window
    17.     test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    18.     Do While test_hwnd <> 0
    19.         'Check if the window isn't a child
    20.         If GetParent(test_hwnd) = 0 Then
    21.             'Get the window's thread
    22.             test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
    23.             If test_pid = target_pid Then
    24.                 InstanceToWnd = test_hwnd
    25.                 Exit Do
    26.             End If
    27.         End If
    28.         'retrieve the next window
    29.         test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    30.     Loop
    31. End Function
    32. Private Sub Form_Load()
    33.     'KPD-Team 1999
    34.     'URL: [url]http://www.allapi.net/[/url]
    35.     'E-Mail: [email][email protected][/email]
    36.     Dim Pid As Long
    37.     'Lock the window update
    38.     LockWindowUpdate GetDesktopWindow
    39.     'Execute notepad.Exe
    40.     Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
    41.     If Pid = 0 Then MsgBox "Error starting the app"
    42.     'retrieve the handle of the window
    43.     mWnd = InstanceToWnd(Pid)
    44.     'Set the notepad's parent
    45.     SetParent mWnd, Me.hwnd
    46.     'Put the focus on notepad
    47.     Putfocus mWnd
    48.     'Unlock windowupdate
    49.     LockWindowUpdate False
    50. End Sub
    51. Private Sub Form_Unload(Cancel As Integer)
    52.     'Unload notepad
    53.     DestroyWindow mWnd
    54.     'End this program
    55.     TerminateProcess GetCurrentProcess, 0
    56. End Sub
    Woka

  7. #7

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