Getting the handle of a window?

Just starting out? Need help? Post your questions and find answers here.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

Hi Go for the EXE name
Like this for "PUREBASIC.EXE"

Code: Select all

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


#TH32CS_SNAPPROCESS = $2
#PROCESS_ALL_ACCESS=$FFF
;-Procedure List



    If OpenLibrary(0, "Kernel32.dll")
        
        CreateToolhelpSnapshot = IsFunction(0, "CreateToolhelp32Snapshot")
        ProcessFirst           = IsFunction(0, "Process32First")
        ProcessNext            = IsFunction(0, "Process32Next")
        
        If CreateToolhelpSnapshot And ProcessFirst And ProcessNext           
            Process.PROCESSENTRY32\dwSize = SizeOf(PROCESSENTRY32)
            
            Snapshot = CallFunctionFast(CreateToolhelpSnapshot, #TH32CS_SNAPPROCESS, 0)
            If Snapshot
                
                ProcessFound = CallFunctionFast(ProcessFirst, Snapshot, Process)
                While ProcessFound
                    If GetFilePart(PeekS(@Process\szExeFile)) ="PUREBASIC.EXE"
                     Check =PeekL(@Process\th32ProcessID)
                     Break
                     EndIf 
                    ProcessFound = CallFunctionFast(ProcessNext, Snapshot, Process)
                    
                Wend
            EndIf
            
            CloseHandle_(Snapshot)
        EndIf
        
        CloseLibrary(0)
    EndIf
    



    Repeat
        hwin=FindWindow_(0,0)
        
        While hwin<>0 ;And oldwin <>win
        MemID= AllocateMemory (255)
            GetWindowText_ (hwin, MemID, 255)
            title$ = PeekS (MemID)
            
           GetWindowThreadProcessId_(hwin,@ProcessId.l)

            If Check= ProcessId
            
            Debug "ProcessID =: "+StrU(ProcessId,#Long)
            ;  Debug title$
            Break
            
            EndIf
            hwin=GetWindow_(hwin,#GW_HWNDNEXT)
        Wend
        Until hwin
    
    
    
Debug "WindHandler =: "+Str(hwin) 
Edit .. ups abit late :?

Best regards
Henrik
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Henrick, your code seems a little better for what I need, but im getting an error when compiling at line 57


MemID= AllocateMemory (255)


needs another parameter tho.. isnt 255 the size? then whats the #mem?!
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

iy's compiled with 3.90

Code: Select all

MemID= AllocateMemory (255)
GetWindowText_ (hwin, MemID, 255)
title$ = PeekS (MemID)
with 3.81 use

Code: Select all

GetWindowText_ (hwin, AllocateMemory (0, 255), 255)
title$ = PeekS (MemoryID ())

Best regards
Henrik
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Compiles now.. heh I still dont installed 3.90 as I think that many libs wont work fine (im wrong maybe?).. anyway I changed the exe name at your code and compiled in debug mode..

I got this:

ProcessID = 2092
WindHandler = 166724300

(from winamp.exe)

but those doesnt seem to be real.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

8O very odd ProcessID & Handler you got there :?:
ProcessId seems way to smal and handler too big, dont get it ?
I don't have winamp so i cant test it for you

Pleas try it with the fweil's example, what handler dit it return ?

Edit: Oh BTW i'am running a win98, duno if it workes on NT, 2000 and Xp ?

Best Regards
Henrik
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

dagcrack could you hand me a link to your ver. of winamp ?
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Processes numbers generated by the OS are low numbers from 0 to xxxx usually, but handles and DCs are higher values (32 bits range). This is the way Windows does for allocating unique numbers to such object's levels.

AFAIK handles start at $10000, below are processes.

Let me know if one has more information about.
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

@fweil
not on win98 :!:

I just tested it aganst another program hwo shows the handler:
with both i thied to find "Windows commander 50"
Shatter shows that the handlre is:
Hex $874 , dec 2164

my code showes only the one i posted shows

ProcessID =: 4294328589
Windows Commander 5.0
WindHandler =: 2164
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

hm I dunno if I'll find 2.91 download or installer on my machine.. i could host you my winamp dir but maybe winamp installs some dll to the windows dir ( ? ).. anyway I hosted it on a friend's ISP account :P

http://users.skynet.be/fa297478/Winamp.zip
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

i just found something "VERY" -very- odd
If i run my code first i have no prob. by finding wincmds handler
But if i have startet "Shatter" AND then run my code i get a comlpetly wrong handler
When i close the Shatter and run my code again it okay again.

Edit/ but the processID is alwayes correct.
Maybe you should try GetDesktopWindow_() insted of FindWindow_()

Dont get it ?

Best regrads
henrik
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

...

Post by NoahPhense »

fweil wrote:...,

want to learn more about windows and how to get handles and other information ?

Play first with the following code and launch Winamp. Then come back with more questions if required ....

Code: Select all

#GWW_HINSTANCE = -6

[edited]
WOW.. VERY nice. ...

- np
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

@dagcrack
i installed winamp and checked it with Shatter
The handler was:
Hex $ cd0 : Dec 3280

i tried the "CloseWindow_(3280) and it worked as you said.
Then i tried my code and the debugger was:

ProcessID =: 4294189209
Winamp Playlist Editor
WindHandler =: 3360

The window you see is not the main window but a child window ..
you have to go trough it's childs to get it

That one thing we learned another is that win98 shows difrent processID and handler from ex. XP

But i have to go to bed now i try tomorrow after work to find the window
But look at some snippers "CodeArchiv" about tasks, as far as i remember there is some about getting childswindows/process.
Zzzz..

Best Regards
Henrik
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Thx Henrik, i'll be looking at the codearchiv. I thougt about looking at it anyway. (got it at my hdd, is always useful when learning something new, dunno if theres a newer version of the codearchiv?!).

I'll be modelling some tanks for my game too.. as I can't stop everything for this damn winamp handle :D .. But i'll finding out how to get it on every OS later.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

@dagcrack
Lol it was kind off easy in the end.
I don't have much time so i make it short.

I found it, its' a skinwinchild of winamp.exe
the name off the window is "Winamp 2.91", you can't see the Window's title bar, -skinwin you know- . :wink:
Try this code and see if it finds the window and close it.
It workes here on win98 se

Code: Select all

Repeat
    hwin=FindWindow_(0,0)
    
    While hwin<>0 ;And oldwin <>win
        
        MemID= AllocateMemory (255)
        GetWindowText_ (hwin, MemID, 255)
        Wintitle$ = PeekS (MemID)
        
        Debug Wintitle$
        GetWindowThreadProcessId_(hwin,@ProcessId.l)
        If Wintitle$ = "Winamp 2.91"
        Debug "Winamp 2.91' processID : " +StrU(ProcessId,#Long)
        break
        EndIf
        hwin=GetWindow_(hwin,#GW_HWNDNEXT)
    Wend
Until hwin



Debug "WindHandler =: "+Str(hwin)
Debug " "
Debug " Wait 4 sec."
Debug " And I do a CloseWindow_(hwin) on Winamp"

Delay (4000)
CloseWindow_(hwin)

Best regrads
Henrik
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Weird.. debugs a whole list non-stop... gotta check the source.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Post Reply