Jump to content

hWnd, PID and Exe under cursor


KaFu
 Share

Recommended Posts

For sure :D , but here's my little example... needed the func for an app.

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include<GUIConstants.au3>
Opt('GuiOnEventMode', 1)

$hGui = GUICreate("hWnd, PID and Exe under cursor", 350, 67, 244, 160, BitOR($WS_POPUP, $WS_BORDER, $WS_SIZEBOX), BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$label = GUICtrlCreateLabel("", 0, 0, 350, 200, 0, $GUI_WS_EX_PARENTDRAG)

GUICtrlSetBkColor(-1, 0xEEF1F6)
GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW)

While Sleep(50)

    $hWndCtrl = HWnd(_WindowFromPoint())
    $hWndWin = _WinAPI_GetAncestor($hWndCtrl, $GA_ROOTOWNER)
    $hWndPid = WinGetProcess($hWndWin)
    $hWndExeLoc = _ProcessGetLocation($hWndPid)
    $hWndExe = StringRight($hWndExeLoc, StringLen($hWndExeLoc) - StringInStr($hWndExeLoc, "\", 0, -1))

    GUICtrlSetData($label, "$hWndCtrl " & @TAB & $hWndCtrl & @CRLF _
             & "$hWndWin " & @TAB & $hWndWin & @CRLF _
             & "$hWndPid " & @TAB & $hWndPid & @CRLF _
             & "$hWndExe " & @TAB & $hWndExe & @CRLF _
             & "$hWndExeLoc " & @TAB & $hWndExeLoc)

WEnd

Func _WindowFromPoint()
    ; from _GUICtrl_SetOnHover
    ; http://www.autoitscript.com/forum/index.php?s=&showtopic=55120
    ; by G.Sandler a.k.a MrCreatoR (CreatoR's Lab, http://creator-lab.ucoz.ru)

    Local $Old_Opt_MCM = Opt("MouseCoordMode", 1)

    Local $aRet = DllCall("User32.dll", "int", "WindowFromPoint", _
            "long", MouseGetPos(0), _
            "long", MouseGetPos(1))

    ;$aRet = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $aRet[0])

    Opt("MouseCoordMode", $Old_Opt_MCM)

    Return $aRet[0]
EndFunc   ;==>_WindowFromPoint


Func _ProcessGetLocation($iPID)
    Local $aProc = DllCall('kernel32.dll', 'hwnd', 'OpenProcess', 'int', BitOR(0x0400, 0x0010), 'int', 0, 'int', $iPID)
    If $aProc[0] = 0 Then Return SetError(1, 0, '')
    Local $vStruct = DllStructCreate('int[1024]')
    DllCall('psapi.dll', 'int', 'EnumProcessModules', 'hwnd', $aProc[0], 'ptr', DllStructGetPtr($vStruct), 'int', DllStructGetSize($vStruct), 'int_ptr', 0)
    Local $aReturn = DllCall('psapi.dll', 'int', 'GetModuleFileNameEx', 'hwnd', $aProc[0], 'int', DllStructGetData($vStruct, 1), 'str', '', 'int', 2048)
    If StringLen($aReturn[3]) = 0 Then Return SetError(2, 0, '')
    Return $aReturn[3]
EndFunc   ;==>_ProcessGetLocation

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...