Support Forums > General Board

Why do I always get the wrong HWND back?

(1/3) > >>

jermy:
Hi Dear people,

Something goes wrong with this code, I can not get a correct HWND back


--- Code: ---'--------------------------------------------------------------------------------
Function FORM1_COMMAND1_BN_CLICKED ( _
                                   ControlIndex     as Long, _      ' index in Control Array
                                   hWndForm         as HWnd, _      ' handle of Form
                                   hWndControl      as HWnd, _      ' handle of Control
                                   idButtonControl  as Long   _     ' identifier of button
                                   ) as Long
Dim hProcessID as Long 'DWORD
Dim hWndApp as HWnd   
 
hProcessID = GetCurrentProcessId()

hWndApp = GethWndFromProcessID(hProcessID)

 Print "hProcessID "; hProcessID

 Print " hWndApp "; hWndApp
  '   SetWindowText(hWndApp, "test 1 ")  ' & Cast(String, hWndApp)
 Print " HWND_FORM1 "; HWND_FORM1
   '  SetWindowText(HWND_FORM1, "test 2 ")  ' & Cast(String, hWndApp)

   Function = 0   ' change according to your needs
End Function


Function GethWndFromProcessID(ByVal hProcessIDToFind as Long) as HWnd

    Dim hWndDesktop as HWnd
    Dim hWndChild as HWnd
    Dim hWndChildProcessID as Long
   
    On Local Error Goto GethWndFromProcessID_Error
   
   'get the handle to the desktop
    hWndDesktop = GetDesktopWindow()
   
   'get the first child under the desktop
    hWndChild = GetWindow(hWndDesktop, GW_CHILD)
   
   'hwndchild will = 0 when no more child windows are found
    Do While hWndChild <> 0
   
       'get the ThreadProcessID of the window
         GetWindowThreadProcessId(hWndChild, @hWndChildProcessID)
       
       'if it matches the target, exit returning that value
        If hWndChildProcessID = hProcessIDToFind Then
            Exit Do
        End If
       
       'not found, so get the next hwnd
        hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
       
    Loop
   
    Function = hWndChild   ' change according to your needs             

Exit Function

GethWndFromProcessID_Error:
    GethWndFromProcessID = 0
    Exit Function
   
End Function

--- End code ---

anyone an idea why it goes wrong?

Josť Roca:
It works using my AfxGethWndFromPID wrapper function:


--- Code: ---hWndApp = AfxGethWndFromPID(GetCurrentProcessId)

--- End code ---

raymw:
Hi Jermy,
I don't really know what the correct hwnd should be, but your sw gives a result that looks OK. The values change if I open other windows before running the code.

jermy:

--- Quote from: raymw on January 28, 2019, 02:56:51 PM ---Hi Jermy,
I don't really know what the correct hwnd should be, but your sw gives a result that looks OK. The values change if I open other windows before running the code.

--- End quote ---

the value hWndApp should be the same as HWND_FORM1
if you use SetWindowText with hWndApp you will see that nothing happens, with HWND_FORM1 you change the caption


--- Code: ---      SetWindowText(hWndApp, "test 1 " & hWndApp)
 
    ' SetWindowText(HWND_FORM1, "test 2 " & HWND_FORM1) 

--- End code ---

jermy:
finally found it,

he returned the HWND from the first child window that he found, with GetParent in between he works


--- Code: ---Function GethWndFromProcessID(ByVal hProcessIDToFind as Long) as HWnd

    Dim as HWnd hWndDesktop, hWndChild
    Dim hWndChildProcessID as Long
   
   'get the handle to the desktop
    hWndDesktop = GetDesktopWindow()
   
   'get the first child under the desktop
    hWndChild = GetWindow(hWndDesktop, GW_CHILD)
   
   'hwndchild will = 0 when no more child windows are found
    Do While hWndChild <> 0
       'See if this window has a parent. If not it is a top-level window.
     If GetParent(hWndChild) = 0 Then

       'get the ThreadProcessID of the window
         GetWindowThreadProcessId(hWndChild, @hWndChildProcessID)
       
       'if it matches the target, exit returning that value
        If hWndChildProcessID = hProcessIDToFind Then
            Exit Do
        End If

     End If 
       'not found, so get the next hwnd
        hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
    Loop
   
    Function = hWndChild   ' change according to your needs             
   
End Function

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version