Activate Any Window With API

Level:
Level2

One of the most common questions I've seen posted on newsgroups and online services involves activating another Windows application. The VB AppActivate statement falls hopelessly short of being truly useful since the window title of an application can change without notice. A much more reliable means of activating another application is by using the window class name. It's much more reliable because the class name of a window will not change once the application has been installed.

So, you ask, how do I get the class name of another application? While many will tell you that you need some sort of "spy" program, this is completely unnecessary. You can find the window class name for any application by running a few lines of code in the debug window. This is a one-time-only procedure.

Disclaimer: This will not help you activate a program written in VB. All VB form windows have the class name "ThunderForm". You'll have to revert to the old method of using the window text, or if the program is your own, make it an OLE server and provide a public Activate method. You can also check out this article in the Microsoft Knowledge Base:

How to Get a Window Handle Without Specifying an Exact Title
Article ID: Q113475

That being said, here's what you do:

Create a blank new module or use an existing module.

Place the following in the declarations section:

Private Declare Function FindWindow Lib "user32" _
   Alias "FindWindowA" _
   (ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Long

Private Declare Function GetClassName Lib "user32" _
   Alias "GetClassNameA" _
   (ByVal hWnd As Long, _
   ByVal lpClassName As String, _
   ByVal nMaxCount As Long) As Long

Enter the following procedure:

Public Sub GetClassNameFromTitle()
   Dim sInput As String
   Dim hWnd As Long
   Dim lpClassName As String
   Dim nMaxCount As Long
   Dim lresult As Long
   ' pad the return buffer for GetClassName
   nMaxCount = 256
   lpClassName = Space(nMaxCount)
   ' Note: must be an exact match
   sInput = InputBox("Enter the exact window title:")
   ' No validation is done as this is a debug window utility
   hWnd = FindWindow(vbNullString, sInput)
   ' Get the class name of the window, again, no validation
   lresult = GetClassName(hWnd, lpClassName, nMaxCount)
   Debug.Print "Window: " & sInput
   Debug.Print "Class name: " & Left$(lpClassName, lresult)
End Sub

Run GetClassNameFromTitle in the debug window. If you enter the window title correctly, the class name will be printed in the debug window.

Once you have the window class name, you can always get a window handle by using FindWindow with the class name and vbNullString. For example:

hWnd = FindWindow("OpusApp", vbNullString)

...will find the hWnd for the Microsoft Word window. Actually, I typically will code constants for the applications I will need to activate on a regular basis.

Public Constant gcClassnameMSWord = "OpusApp"

I also use a wrapper function for the whole process:

Public Function fActivateWindowClass(psClassname As String) As Boolean
   Dim hWnd As Long
   hWnd = FindWindow(psClassname, vbNullString)
   If hWnd > 0 Then
      ' ShowWindow returns True if the window was previously hidden. 
      ' I don't care so I use the sub style
      ' ShowWindow and SW_SHOW declared elsewhere
      ' SW_SHOW will display the window in its current size and position
      Call ShowWindow hWnd, SW_SHOW
      fActivateWindowClass = True
   Else
      ' FindWindow failed, return False
      fActivateWindowClass = False
   End If
End Function

You can now activate the Word window with this simple code:

If fActivateWindowClass(gcClassnameMSWord) Then
   ' succeeded, do what you will
Else
   ' failed, handle with grace
End if

This procedure could easily be extended to shell the application if FindWindow fails. That is left as an exercise for you.

Originally written by Joe Garrick

If you enjoyed this post, subscribe for updates (it's free)

A much more reliable means of

A much more reliable means of activating another application is by using the window class name. It's much more reliable because the class name of a window will not change once the application has been installed. cliquez ici

blog comments and forums

reliable because the class name of a window will not change once the application has been insta blog comments and forums

BlackMen

It's much more reliable because the class name of a window will not change once the application has been installed. JAMAICAN COFFEE BEST COFFEE IN THE WORLD

window text, or if the

window text, or if the program is your own, make it an OLE server and provide a public Activate method. You can also check out this article in the Microsoft Knowledge Base: student loan lawyer

Each day is

Each day is long, but I am proud to serve my community and the children of Somalia. I don’t want to see another child paralyzed by polio
mapquest driving directions

best fiverr seo gigs

I have the handle for the active window. But i need the classname. My code looks somewhat like this

So i have the handle. But how

So i have the handle. But how can i get the classname from that hadle?
Please help me...... Its important and i dont hav much time.
Thanks in advance.

crowdfunding definition

will find the hWnd for the Microsoft Word window. Actually, I typically will code constants for the applications crowdfunding definition

Executive Employers

application is by using the window class name. It's much more reliable because the class name of a window will not change once the application has been installed. Executive Employers

So i have the handle. But how

So i have the handle. But how can i get the classname from that hadle?
Please help me...... Its important and i dont hav much time. Free Family Lawyers

ARPIONTH's music

short of being truly useful since the window title of an application can change without notice. A much more reliable means of activating another application is by using the window class name. It's much more reliable because the class name of a window will not change once the application has been installed. ARPIONTH\'s music

I am grateful for your hard

I am grateful for your hard work. But if you did it in a simple procedure that would be in reality polite. But over all I exceedingly not compulsory you and certain will stay for more posts like this. noah health

This is such

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future...
english

BlackMen

The VB AppActivate statement falls hopelessly short of being truly useful since the window title of an application can change without notice. A much more reliable means of activating another application is by using the window class name. It's much more reliable because the class name of a window will not change once the application has been installed. Click this link

Florida

application is by using the window class name. It's much more reliable because the class name of a window will not change once the application has been installed. Florida

BlackMen

how do I get the class name of another application? While many will tell you that you need some sort of "spy" program, this is completely unnecessary. You can find the window class name for any application by running a few lines of code in the debug window. Mary

So i have the handle. But how

So i have the handle. But how can i get the classname from that hadle?
Please help me...... Its important and i dont hav much time.
Thanks in advance. dofollow blog comments

fast-report.com Give your app

fast-report.com
Give your app fast OLAP functions! Delphi and C++Builder components

strawberry smoothie recipe

how do I get the class name of another application? While many will tell you that you need some sort of "spy" program, this is completely unnecessary. You can find the window class name for any application by running a few lines of code in the debug window. This is a one-time-only procedurestrawberry smoothie recipe

This is a very

This is a very good post. Just wonderful. Truly, I am amazed at what informative things you’ve told us today
Leather Jackets For Women

BlackMen

It's much more reliable because the class name of a window will not change once the application has been installed. how to lose weight fast

Admire it regarding

Admire it regarding positioning the comely amuse…I was summary set regarding a manifold regular this…I identified that can peaceful good, employing likelihood, you would perhaps store positioning a immense sum of blogs.
free stuff

debt relief

window text, or if the program is your own, make it an OLE server and provide a public Activate method. You can also check out this article in the Microsoft Knowledge Base: debt relief

This choice is a happen of a

This choice is a happen of a lack of a convenient route to discern the point in rhythm during the literal creation of the baby naturally happens. http://aokmedia.co

his will not help you

his will not help you activate a program written in VB. All VB form windows have the class name "ThunderForm". You'll have https://www.rebelmouse.com/

will not help you activate a

will not help you activate a program written in VB. All VB form windows have the class name "ThunderForm furnished rentals vancouver

I am grateful for your hard

I am grateful for your hard work. But if you did it in a simple procedure that would be in reality polite. But over all I exceedingly not compulsory you and certain will stay for more posts like this.
Sonicare Flexcare

BlackMen

So, you ask, how do I get the class name of another application? While many will tell you that you need some sort of "spy" program, this is completely unnecessary. You can find the window class name for any application by running a few lines of code in the debug window. This is a one-time-only procedure. 3d ultrasound nj

debt settlement

debt settlement offering a single software management platform is an advantage as organizations can effectively manage their IT infrastructures with limited staff resources. debt settlement

BlackMen

A much more reliable means of activating another application is by using the window class name. It's much more reliable because the class name of a window will not change once the application has been installed. atkins diet food chart

Celebrity Plastic Surgery

how do I get the class name of another application? While many will tell you that you need some sort of "spy" program, this is completely unnecessary. You can find the window class name for any application by running a few lines of code in the debug window. This is a one-time-only procedure. Celebrity Plastic Surgery

Private Declare Function

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Enter the following procedure:

Public Sub GetClassNameFromTitle()
Dim sInput As String
Dim hWnd As Long
Dim lpClassName As String
Dim nMaxCount As Long
Dim lresult As Long
' pad the return buffer for GetClassName
nMaxCount = 256
lpClassName = Space(nMaxCount)
' Note: must be an exact match
sInput = InputBox("Enter the exact window title:")
' No validation is done as this is a debug window utility
hWnd = FindWindow(vbNullString, sInput)
' Get the class name of the window, again, no validation
lresult = GetClassName(hWnd, lpClassName, nMaxCount)
Debug.Print "Window: " & sInput
Debug.Print "Class name: " & Left$(lpClassName, lresult)
End Sub

The ShowWindow API is missing from the Example

The 'ShowWindow' API is missing from the Example. Here's the full code I worked out with the ShowWindow API That Works VB6. Also in the original example the variable SW_SHOW is not set. This will cause an error. I set SW_SHOW as a constant at the top of the module. The value 9 activates the window and shows it if minimized.

See: http://support.microsoft.com/kb/104710
for more info and a list of application class names.

'''''''''''''''''''''''''''''''' START CODE ''''''''''''''''''''''''''''''''''''''

' Windows API Declarations
Private Declare Function ShowWindow Lib "User32" _
(ByVal hWnd As Long, ByVal nCmdShow As Integer) As Integer

Private Declare Function FindWindow Lib "User32" _
Alias "FindWindowA" _
(ByVal lpclassname As String, _
ByVal lpWindowName As String) As Long

Private Declare Function GetClassName Lib "User32" _
Alias "GetClassNameA" _
(ByVal hWnd As Long, _
ByVal lpclassname As String, _
ByVal nMaxCount As Long) As Long

Const SW_SHOW = 9

' Enter the following procedure:

Public Sub GetClassNameFromTitle()
Dim sInput As String
Dim hWnd As Long
Dim lpclassname As String
Dim nMaxCount As Long
Dim lresult As Long
' pad the return buffer for GetClassName
nMaxCount = 256
lpclassname = Space(nMaxCount)
' Note: must be an exact match
sInput = "Microsoft Word" ' InputBox("Enter the exact window title:")
' No validation is done as this is a debug window utility
hWnd = FindWindow(vbNullString, sInput)
' Get the class name of the window, again, no validation
lresult = GetClassName(hWnd, lpclassname, nMaxCount)
' Debug.Print "Window: " & sInput
' Debug.Print "Class name: " & Left$(lpClassName, lresult)

Call fActivateWindowClass(lpclassname)
End Sub

Public Function fActivateWindowClass(psClassname As String) As Boolean
Dim hWnd As Long
hWnd = FindWindow(psClassname, vbNullString)
If hWnd > 0 Then
' ShowWindow returns True if the window was previously hidden.
' I don't care so I use the sub style
' ShowWindow and SW_SHOW declared elsewhere
' SW_SHOW will display the window in its current size and position
' SW_SHOW = 1
Call ShowWindow(hWnd, SW_SHOW)
fActivateWindowClass = True
Else
' FindWindow failed, return False
fActivateWindowClass = False
End If
End Function

Sub CheckWindowOpen()
' You can now activate the Word window with this simple code:
If fActivateWindowClass(gcClassnameMSWord) Then
' succeeded, do what you will
Else
' failed, handle with grace
End If

End Sub

' You can also call the fActivateWindowClass Function directly if you know the Applicaiton Class Name
Sub WordAppActivate()

x = fActivateWindowClass("opusapp")

End Sub

this is vb6.0 not vb.net

this is vb6.0 not vb.net

Maybe...

Because it's in a VB6 tutorial site?

How can i get classname from the handle of active window?

Am new to VB.Net coding.
I have the handle for the active window. But i need the classname. My code looks somewhat like this.

Public Class Form1
Declare Function GetForegroundWindow Lib "user32.dll" () As Int32
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Int32, ByVal lpwindowtext As String, ByVal nmaxcount As Int32) As Int32

Public Structure Window
Public Handle As IntPtr
Public Text As String
End Structure

Public Function GetWindowText() As Window
Dim title As String
title = New String(Chr(0), 100)
Dim titleLength As Integer
titleLength = GetWindowText(GetForegroundWindow(), title, 255)
titleLength = title.Length
Dim w As New Window
w.Text = title.ToString()
w.Handle = GetForegroundWindow()
Return w
End Function

So i have the handle. But how can i get the classname from that hadle?
Please help me...... Its important and i dont hav much time.
Thanks in advance.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.