Getting the Win32 FindWindow(ClassName) for a .Net Form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi. I want to use the Win32 FindWindow() function and I want to use its FIRST
parameter, "ClassName" instead of its "WindowName" parameter to find a window
for a .NET Application.

I feel stupid but I can't seem to figure out 2 things:

1. What I create a Form in a VS.NET Windows Forms Application project,
exactly what text will be the string that Win32 FindWindow()'s ClassName
parameter will take from another application to find my form? It doesn't SEEM
to just be the .Net Class name of the Form. SO what might it be?

2. Is there a dynamic way of finding that class name in my own app? Meaning,
if I have the Form object, is there a property or something which will allow
me to get or build that ClassName string that other apps can use to do
FindWindow() on my form?

Thanks for any help you can give!

Alex
 
Alex,

here's how you can get the class name that you can pass to FindWindow:

'API declares
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hwnd As IntPtr, ByVal lpClassName As
System.Text.StringBuilder, _
ByVal nMaxCount As Integer) As Integer
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As
IntPtr

'retrieve the class name of the current form
'Me refers to the form and Me.Handle is the forms handle
'basically, just pass in the handle of any window for which
'you want to find the class name
Dim sClassName As New StringBuilder("", 256)
Call GetClassName(Me.Handle, sClassName, 256)

'get the handle of the window with the given class name
Dim myHwnd As IntPtr = FindWindow(sClassName.ToString, vbNullString)

You can see that myHwnd and Me.Handle have the same value since GetClassName
and FindWindow are sort of doing the inverse of each other. You can specify
the window title as the second parameter instead of vbNullString to get a
better match if you have many windows belonging to the same class open.

I hope this helps..
Imran.
 
Back
Top