Return process name from window handle...

Share your advanced PureBasic knowledge/code with the community.
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Return process name from window handle...

Post by Hi-Toro »

Just a stripped down version of something I posted in another thread (the basic functionality was buried in another program)...

For this demo, just point at various other windows on the screen, making sure you can see this window's title bar!

Code: Select all

; -----------------------------------------------------------------------------
; Public domain -- Hi-Toro 2003
; -----------------------------------------------------------------------------
; Return a window's process name from its handle...
; -----------------------------------------------------------------------------

; IMPORTANT! You must paste the following section of code (from here to the
; demo section) at the top of your code, AND paste the part at the bottom
; (the 'GetProcessList' sub-routine) at the bottom of your code. The reason the
; sub-routine is required (rather than a procedure) is that the Win32 function
; 'Process32Next' seems to fail on Windows 9x when called from inside a procedure...

; Note that you should always call 'GetProcessList' before trying to retrieve a window's process name...

; -----------------------------------------------------------------------------
; Paste at top of your code...
; -----------------------------------------------------------------------------

#TH32CS_SNAPHEAPLIST = $1
#TH32CS_SNAPPROCESS = $2
#TH32CS_SNAPTHREAD = $4
#TH32CS_SNAPMODULE = $8
#TH32CS_SNAPALL = #TH32CS_SNAPHEAPLIST | #TH32CS_SNAPPROCESS | #TH32CS_SNAPTHREAD | #TH32CS_SNAPMODULE
#TH32CS_INHERIT = $80000000
#INVALID_HANDLE_VALUE = -1
#MAX_PATH = 260
#PROCESS32LIB = 9999

Structure PROCESSENTRY32
    dwSize.l
    cntUsage.l
    th32ProcessID.l
    *th32DefaultHeapID.l
    th32ModuleID.l
    cntThreads.l
    th32ParentProcessID.l
    pcPriClassBase.l
    dwFlags.l
    szExeFile.b [#MAX_PATH]
EndStructure

; List used to store processes on 'Gosub GetProcessList'...

NewList Process32.PROCESSENTRY32 ()

; Returns process name from window handle...
; IMPORTANT! You should 'Gosub GetProcessList' before calling this!

Procedure.s FindWindowProcessName (window)
    ResetList (Process32 ())
    While NextElement (Process32 ())
        GetWindowThreadProcessId_ (window, @pid)
        If pid = Process32 ()\th32ProcessID
            exe$ = GetFilePart (PeekS (@Process32 ()\szExeFile))
            LastElement (Process32 ())
        EndIf
    Wend
    ProcedureReturn exe$
EndProcedure

; Returns Process ID from window handle...

Procedure.l FindWindowProcessID (window)
    GetWindowThreadProcessId_ (window, @pid)
    ProcedureReturn pid
EndProcedure

; -----------------------------------------------------------------------------
; D E M O...
; -----------------------------------------------------------------------------

window = OpenWindow (0, 0, 0, 320, 200, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "Test window")

; Update every 100 ms...

SetTimer_ (WindowID (), 0, 100, 0)

Repeat
    Select WaitWindowEvent ()
        Case #PB_Event_CloseWindow
            End
        Case #WM_TIMER
            ; Get process list...
            Gosub GetProcessList
            ; Get window under mouse position...
            GetCursorPos_ (@p.POINT)
            over = WindowFromPoint_ (p\x, p\y)
            ; Find its name and set this window's title to it...
            proc$ = FindWindowProcessName (over)
            SetWindowText_ (window, proc$)
    EndSelect
ForEver

; -----------------------------------------------------------------------------
; Paste at bottom of your code...
; -----------------------------------------------------------------------------

End ; Leave this here!

GetProcessList:

    ClearList (Process32 ())

    ; Add processes to Process32 () list...

    If OpenLibrary (#PROCESS32LIB, "kernel32.dll")

        snap = CallFunction (#PROCESS32LIB, "CreateToolhelp32Snapshot", #TH32CS_SNAPPROCESS, 0)

        If snap

            DefType.PROCESSENTRY32 Proc32
            Proc32\dwSize = SizeOf (PROCESSENTRY32)
            
            If CallFunction (#PROCESS32LIB, "Process32First", snap, @Proc32)

                AddElement (Process32 ())
                CopyMemory (@Proc32, @Process32 (), SizeOf (PROCESSENTRY32))
                
                While CallFunction (#PROCESS32LIB, "Process32Next", snap, @Proc32)
                    AddElement (Process32 ())
                    CopyMemory (@Proc32, @Process32 (), SizeOf (PROCESSENTRY32))
                Wend
                
            EndIf    
            CloseHandle_ (snap)
        
        EndIf

        CloseLibrary (#PROCESS32LIB)
        
    EndIf

Return
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
User avatar
Le Soldat Inconnu
Enthusiast
Enthusiast
Posts: 306
Joined: Wed Jul 09, 2003 11:33 am
Location: France

Post by Le Soldat Inconnu »

Very well

code write for 4.20

Code: Select all

; Auteur : Le Soldat Inconnu
; Version de PB : 4
;
; Explication du programme :
; Trouver le nom du programme à partir de l'ID d'une fenêtre

Procedure.s ProgramfileNameFromWindow(WindowID)
  Protected ProcessID, Process32, Snapshot, ProcessEntry.PROCESSENTRY32, ProgramfileName.s
  GetWindowThreadProcessId_(WindowID, @ProcessID)
  
  Process32 = OpenLibrary (#PB_Any, "kernel32.dll") 
  If Process32
    Snapshot = CallFunction (Process32, "CreateToolhelp32Snapshot", #TH32CS_SNAPPROCESS, 0) 
    If Snapshot 
      ProcessEntry\dwSize = SizeOf(PROCESSENTRY32) 
      If CallFunction (Process32, "Process32First", Snapshot, @ProcessEntry) 
        Repeat
          If ProcessID = ProcessEntry\th32ProcessID 
            ProgramfileName = PeekS(@ProcessEntry\szExeFile)
            Break
          EndIf
        Until CallFunction (Process32, "Process32Next", Snapshot, @ProcessEntry) = 0
      EndIf    
      CloseHandle_(Snapshot) 
    EndIf 
    CloseLibrary(Process32) 
  EndIf 
  ProcedureReturn ProgramfileName
EndProcedure






; Création de la fenêtre et de la GadgetList
If OpenWindow(0, 0, 0, 300, 30, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget) = 0 Or CreateGadgetList(WindowID(0)) = 0
  End
EndIf

StickyWindow(0, 1)

TextGadget(0, 5, 5, 290, 20, "")

SetTimer_(WindowID(0), 1, 500, 0)

Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #WM_TIMER
      Select EventwParam()
        Case 1
          Fenetre = WindowFromPoint_(DesktopMouseX(), DesktopMouseY())
          Texte.s =ProgramfileNameFromWindow(Fenetre)
          SetGadgetText(0, Texte)
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget() ; Gadgets
          
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow

KillTimer_(WindowID(0), 1)

End

LSI
Wolf
Enthusiast
Enthusiast
Posts: 232
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Add an space to selected line

Post by Wolf »

Thank you Le Soldat Inconnu and Hi-Toro.

It's very useful :D
Post Reply