Jump to content

Locate Window Icon


Falcone88
 Share

Recommended Posts

Is there a way to use an hwnd or the title of a window to locate its icon file, and use that in an autoit GUI? I've been searching for a while and haven't found anything related, but it seems like I have seen something similar before....

Thanks!

Edited by Falcone88

My Code:- _TocLib - UDF for TOC protocol (The simplified one used by 3rd party AIM/ICQ clients)

Link to comment
Share on other sites

Is there a way to use an hwnd or the title of a window to locate its icon file, and use that in an autoit GUI? I've been searching for a while and haven't found anything related, but it seems like I have seen something similar before....

Thanks!

WinGetProcess() gives you the PID

then feed it into the following:

Func _ProcessListProperties($Process = "", $sComputer = ".");borrowed function
Local $sUserName, $sMsg, $sUserDomain, $avProcs
If $Process = "" Then
$avProcs = ProcessList()
Else
$avProcs = ProcessList($Process)
EndIf
If $avProcs[0][0] = 0 Then Return $avProcs
ReDim $avProcs[$avProcs[0][0] + 1][7]
$oWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputer & "\root\cimv2")
If IsObj($oWMI) Then
$colProcs = $oWMI.ExecQuery ("select * from win32_process")
If IsObj($colProcs) Then
For $oProc In $colProcs
For $n = 1 To $avProcs[0][0]
If $avProcs[$n][1] = $oProc.ProcessId Then
$avProcs[$n][2] = $oProc.ParentProcessId
If $oProc.GetOwner ($sUserName, $sUserDomain) = 0 Then $avProcs[$n][3] = $sUserDomain & "\" & $sUserName
$avProcs[$n][4] = $oProc.Priority
$avProcs[$n][5] = $oProc.ExecutablePath
ExitLoop
EndIf
Next
Next
Else
SetError(2)
EndIf
$oRefresher = ObjCreate("WbemScripting.SWbemRefresher")
$colProcs = $oRefresher.AddEnum ($oWMI, "Win32_PerfFormattedData_PerfProc_Process").objectSet
$oRefresher.Refresh
Sleep(1000)
$oRefresher.Refresh
For $oProc In $colProcs
For $n = 1 To $avProcs[0][0]
If $avProcs[$n][1] = $oProc.IDProcess Then
$avProcs[$n][6] = $oProc.PercentProcessorTime
ExitLoop
EndIf
Next
Next
Else
SetError(1)
EndIf
Return $avProcs
EndFunc

the output $variable[1][5] (I think) will give you the path of that .exe, and then use can use guictrlsetimage() with that path

is that what you had in mind?

[u]You can download my projects at:[/u] Pulsar Software
Link to comment
Share on other sites

That would probably work if I had the filename, but unfortunately I only have the window title. Is there a way to get a process' file location? I know if I really wanted to I could find the PID from the window title....

EDIT: @maqleod WOW that might be exactly what I was thinking of... I'll check it out

EDIT2: After some fiddling around I got it to work using GuiCtrlCreateIcon() Unfortunately it's quite slow, so I might fiddle around to see if I can make things more efficient.

Thanks so much! You'll probably hear from me soon :)

EDIT3: This is much faster for my purposes:

Func _ProcessGetPath($iPid)
    $oWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    if IsObj($oWMI) Then
        $colProcs = $oWMI.ExecQuery ("select * from win32_process")
        if IsObj($colProcs) Then
            For $oProc In $colProcs
                if $oProc.ProcessId == $iPid Then
                    return $oProc.ExecutablePath
                EndIf
            Next        
        EndIf
    EndIf
    
    return ""
EndFunc

I'll probably post what I used this for sometime in the near future :)

Edited by Falcone88

My Code:- _TocLib - UDF for TOC protocol (The simplified one used by 3rd party AIM/ICQ clients)

Link to comment
Share on other sites

  • 1 month later...

WinGetProcess() gives you the PID

then feed it into the following:

Func _ProcessListProperties($Process = "", $sComputer = ".");borrowed function
Local $sUserName, $sMsg, $sUserDomain, $avProcs
If $Process = "" Then
$avProcs = ProcessList()
Else
$avProcs = ProcessList($Process)
EndIf

;...

EndFunc

the output $variable[1][5] (I think) will give you the path of that .exe, and then use can use guictrlsetimage() with that path

is that what you had in mind?

Might we be missing some attribution for that code... hmmm...?

:)

No problem with copying and using it, but attribution is considered polite.

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Under NT (thus XP, Vista) there's a much more efficient way to get EXE path of running process than WMI:

Local $pid = WinGetProcess("Untitled - Notepad")

Local $hProc = DllCall("kernel32.dll", "int", "OpenProcess", "int", 0x0410, "int", False, "int", $pid)
If $hProc[0] Then
    Local $stHMod = DllStructCreate("int hMod")
    Local $stCB = DllStructCreate("dword cbNeeded")
    Local $resEnum = DllCall("psapi.dll", "int", "EnumProcessModules", "int", $hProc[0], "ptr", DllStructGetPtr($stHMod), "dword", DllStructGetSize($stHMod), "ptr", DllStructGetPtr($stCB, 1))
    If $resEnum[0] Then
        Local $resPath = DllCall("psapi.dll", "int", "GetModuleFileNameEx", "int", $hProc[0], "int", DllStructGetData($stHMod, 1), "str", "", "dword", 32768)
        MsgBox(0, "Notepad Path", "" & $resPath[3])
    EndIf
    $stHMod = 0
    $stCB = 0
    DllCall("kernel32.dll", 'int', 'CloseHandle', 'int', $hProc[0])
EndIf

TODO: error handling

UDFS & Apps:

Spoiler

DDEML.au3 - DDE Client + Server
Localization.au3 - localize your scripts
TLI.au3 - type information on COM objects (TLBINF emulation)
TLBAutoEnum.au3 - auto-import of COM constants (enums)
AU3Automation - export AU3 scripts via COM interfaces
TypeLibInspector - OleView was yesterday

Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCEĀ 

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...