Code archives/User Libs/Use FindWindow Without Class Name

This code has been declared by its author to be Public Domain code.

Download source code

Use FindWindow Without Class Name by semar2004
Hi,
as you know, from within VB, the user32 api call FindWindow could also be called without
specify the class name of the application, by passing a vbNullString value.

To simulate the vbNullString from within Blitz, we could declare another FindWindow in the
.decls file, which accepts a number instead of a string for the class parameter:
api_FindWindow_0 (note the % instead of $)

.lib "user32.dll"
api_FindWindow% (lpClassName$, lpWindowName$) : "FindWindowA"
api_FindWindow_0% (lpClassName%, lpWindowName$) : "FindWindowA"


In this way, we can call FindWindow using 0 as class name parameter.
So we don't have to know the class name of the window we are searching for. That means, we can actually get any window handler by only specifing it's window title.

Here is an example; try the function FindWindow by using method1 or method2.
You can search outlook window, notepad, without to know any class name !

Note: if you use the class name method (method2), this assumes you are trying this code with B3D; if you try this code using BlitzPlus, you should change the class$ accordingly:

"GX_WIN32_CLASS" ; <- BlitzPlus 1.11
"BLITZMAX_WINDOW_CLASS" ; <- BlitzPlus 1.34
"Blitz Runtime Class" ; <- Blitz3D

Hope you find it useful,
Sergio.
;FindWindow without class name demo
;by Sergio - semar

title$ = "myblitzapp"
AppTitle title
Print "handle of " + title + " = " + findwindow(title)
WaitKey
End

;===============================
Function findwindow(name$)
;===============================
;api_FindWindow% (lpClassName$, lpWindowName$)
;api_FindWindow_0% (lpClassName%, lpWindowName$)
;
;method 1:
;passing a null value

method = 1 ;change to method 2 but check the class name first !

If method = 1 Then
hwnd% =  api_FindWindow_0% (0,name)
Else

;method 2:
;passing a string value = class name
;the class name of a b3d application is: "Blitz Runtime Class"

class$ = "Blitz Runtime Class"
hwnd% =  api_FindWindow% (class,name)

EndIf

Return hwnd

End Function

Comments

jfk EO-111102005
btw. since version 1.89(?) of Blitz3D you can use

hwnd=systemproperty$("AppHWND")


Code Archives Forum