tool displaying win ids on mouseover

For everything that's not in any way related to PureBasic. General chat etc...
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re:

Post by Dude »

MemoryID() doesn't exist anymore, so how do we use this code?

Code: Select all

GetClassName_ (window, ReAllocateMemory (0, 255), 255)
class$ = PeekS (MemoryID ())
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8434
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: tool displaying win ids on mouseover

Post by netmaestro »

The code is invalid. ReAllocateMemory(0, 255) returns a memory address. This address must be used in future references to this allocation. In the early days of PB, WindowID(), ImageID(), MemoryID(), etc. needed no parameter because of previous Use..[] commands. The Use commands are obsolete for some years now and so you must retain the return value of a call to AllocateMemory or ReAllocateMemory (which changes the pointer) in order to reference the memory. MemoryID without a parameter is obsolete (as it should be since PureBasic grew up)
BERESHEIT
User avatar
tj1010
Enthusiast
Enthusiast
Posts: 624
Joined: Mon Feb 25, 2013 5:51 pm
Location: US or Estonia
Contact:

Re: tool displaying win ids on mouseover

Post by tj1010 »

In my reverse engineering days there was a program that when given a PID could dump all data on GUI elements. I just used it to track elements in event handlers for easy patching of routines. Windows 10 is basically XP and 7 in that respect for the most part, so such tools still work the same.
The truth hurts.
Hi-Toro
Enthusiast
Enthusiast
Posts: 265
Joined: Sat Apr 26, 2003 3:23 pm

Re: tool displaying win ids on mouseover

Post by Hi-Toro »

You could adapt this to do something similar -- just run the list of windows through pid = FindWindowProcessID (window) to match window to process:

Code: Select all


#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

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

Global 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

#TREEGADGET = 0

Global level

Procedure ListChildWindows (window, parameter)

	*mem = ReAllocateMemory (0, 255)
    GetClassName_ (window, *mem, 255) 
    class$ = PeekS (*mem)

	*mem = ReAllocateMemory (0, 255)
    GetWindowText_ (window, *mem, 255) 
    title$ = PeekS (*mem)

    If title$ = ""
        title$ = "Unnamed window"
    EndIf

	If FindString (LCase (class$), LCase ("MSTaskSwWClass"), 1)
		title$ = "****** " + title$
	EndIf
	
    FreeMemory (*mem) 

    level = level + 1
        AddGadgetItem (#TREEGADGET, -1, title$ + " [Window ID: " + Str (window) + "]    [Class: " + class$ + "]", #Null, level)
        EnumChildWindows_ (window, @ListChildWindows (), parameter + 1)
    level = level - 1
    
    ProcedureReturn #True
    
EndProcedure

Procedure ListWindows (window, parameter) 

	*mem = ReAllocateMemory (0, 255)
    GetClassName_ (window, *mem, 255) 
    class$ = PeekS (*mem) 

	*mem = ReAllocateMemory (0, 255)
    GetWindowText_ (window, *mem, 255) 
    title$ = PeekS (*mem) 

    If title$ = ""
        title$ = "Unnamed window"
    EndIf

    FreeMemory (*mem) 

    level = level + 1
        AddGadgetItem (#TREEGADGET, -1, title$ + " [Window ID: " + Str (window) + "]    [Class: " + class$ + "]    [Process " + Str (FindWindowProcessID (window)) + ": " + FindWindowProcessName (window) + "]", #Null, level)
        EnumChildWindows_ (window, @ListChildWindows (), 1)
    level = level - 1

    ProcedureReturn #True 

EndProcedure 

Procedure WinHook (WindowID, Message, wParam, lParam)
    If Message = #WM_SIZE
        ResizeGadget (#TREEGADGET, 0, 0, WindowWidth (0), WindowHeight (0) - 25)
        RedrawWindow_ (GadgetID (#TREEGADGET), #Null, #Null, #RDW_INVALIDATE)
        ResizeGadget (1, 0, WindowHeight (0) - 25, WindowWidth (0), 25)
    EndIf
    ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure GetWindowTree ()
    ClearGadgetItems (#TREEGADGET)
    level = level + 1
    AddGadgetItem (#TREEGADGET, -1, "All windows...", #Null, level)
    EnumWindows_ (@ListWindows (), 0)
    level = level - 1
EndProcedure

OpenWindow (0, 0, 0, 800, 600, "All windows...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)

TreeGadget (#TREEGADGET, 0, 0, WindowWidth (0), WindowHeight (0) - 25)
Gosub GetProcessList
GetWindowTree ()
SetGadgetItemState (0, 0, #PB_Tree_Expanded)

ButtonGadget (1, 0, WindowHeight (0) - 25, WindowWidth (0), 25, "Update list...")

CreatePopupMenu (0)
MenuItem (1, "Copy")

SetWindowCallback (@WinHook ())

Repeat

    Select WaitWindowEvent ()

    	Case #PB_Event_Menu

    		Select EventMenu ()
    			Case 1
    				SetClipboardText (GetGadgetItemText (0, GetGadgetState (0)) + Chr (13) + Chr (10))
    		EndSelect

        Case #PB_Event_CloseWindow
            End

        Case #PB_Event_Gadget

            Select EventGadget ()

				Case 0

			    	Select EventType ()

			    		Case #PB_EventType_RightClick
				    	
				    		DisplayPopupMenu (0, WindowID (0))
			
					EndSelect		    		

            	Case 1

	                Gosub GetProcessList
	                GetWindowTree ()
	                SetGadgetItemState (0, 0, #PB_Tree_Expanded)
	
            EndSelect

	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

            Proc32.PROCESSENTRY32
            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
EDIT: lol, just noticed it uses a Gosub! I believe this was because Process32First and co didn't actually work on Windows 9x when called as a function -- never found any references to this on the internet at the time, but the same problem happened in a C test too.

EDIT2: Some sort of Unicode problem with the displayed program names -- this is REALLY old code though! Assume not too hard to fix...
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Post Reply