Solved

Getting window titles

Posted on 2002-07-05
9
227 Views
Last Modified: 2010-05-02
Hello,

  I have a MDI program and I need to get a list of all window titles that are currently running on the entire system.  I have the following code:

Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetWindowTextLength Lib _
"user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal _
lpString As String, ByVal cch As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2

Dim CurrWnd As Long
Dim Length As Long
Dim TaskName As String
Dim Parent As Long

'Check if Vantive is running
CurrWnd = GetWindow(frmMain.hwnd, GW_HWNDFIRST)

While CurrWnd <> 0
Parent = GetParent(CurrWnd)
Length = GetWindowTextLength(CurrWnd)
TaskName = Space$(Length + 1)

Length = GetWindowText(CurrWnd, TaskName, Length + 1)
TaskName = Left$(TaskName, Len(TaskName) - 1)

If Length > 0 Then
If TaskName <> Me.Caption Then
    If InStr(1, TaskName, "Vantive System") Then
        MsgBox TaskName
        VantiveUp = True
    End If
End If
End If
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
DoEvents
Wend



However, when I run this I get this error:

Run-time error '426'
Only one MDI-form allowed.

This code works fine if I run it in a non-MDI program.
0
Comment
Question by:ingenito
9 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7133270
Strange, that code has nothing to do with your error...
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7133388

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vb98/html/vbmsgobjOneMDIForm.asp

The above link has this code:
-----------------------------------------------
Only one MDI Form allowed (Error 426):

A project can only have one MDIForm. This error has the following causes and solutions:

You tried to load a file containing an MDIForm into a project that already has an MDIForm in it.
Remove the file containing the loaded MDIForm before loading another.

You tried to load a second instance of an MDIForm created with a Dim or Set statement.
You can only create one instance of an MDIForm in a project.
------------------------------------------------

Are you trying to do one of these..

-priya

0
 

Author Comment

by:ingenito
ID: 7133900
Hey guys,

Richie,

The code gets the error on this line:

'Check if Vantive is running
CurrWnd = GetWindow(frmMain.hwnd, GW_HWNDFIRST)

It looks like it's the call to frmmain.hwnd that gives the error.

priya,

I saw that also when I went to help on the error.  I am not doing any of that, just the code above.

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Expert Comment

by:samsonite1023
ID: 7134433
Do you need the titles of ALL windows running? (including children windows)  Or just parent windows?

Let me know, I have the code for both.
0
 

Author Comment

by:ingenito
ID: 7135820
samsonite,

Just a code for all parent windows running, i just needs the title bars of them.  Thanks.
0
 
LVL 1

Expert Comment

by:samsonite1023
ID: 7135950
Ok, here you go:

'''''''''''''''''''''''''
'This goes in a module:
'''''''''''''''''''''''''

'api's to enumerate windows and get text
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long


Public Function GetWindowTitles(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim wText As String, temp As Long

wText = Space(255)
temp = GetWindowText(hWnd, wText, 255)

'Add any code you want here
'wText is the window title
'hWnd is the window handle

'to keep going, set this to 1, to stop, set to 0
GetWindowTitles = 1

End Function




''''''''''''''''''''''''
'this goes into your form
''''''''''''''''''''''''

Private Sub Command1_Click()
Dim retval As Long
'after addressof, put whatever function you'd like.  must be public, and in a module
retval = EnumWindows(AddressOf GetWindowTitles, 0)
End Sub




That's it!  In the "GetWindowTitles" function, you can add whatever code you'd like, such as adding it to a listbox.

Hope this is what you need!

-Sam
0
 
LVL 1

Accepted Solution

by:
samsonite1023 earned 100 total points
ID: 7136060
Ok, here you go:

'''''''''''''''''''''''''
'This goes in a module:
'''''''''''''''''''''''''

'api's to enumerate windows and get text
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long


Public Function GetWindowTitles(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim wText As String, temp As Long

wText = Space(255)
temp = GetWindowText(hWnd, wText, 255)

'Add any code you want here
'wText is the window title
'hWnd is the window handle

'to keep going, set this to 1, to stop, set to 0
GetWindowTitles = 1

End Function




''''''''''''''''''''''''
'this goes into your form
''''''''''''''''''''''''

Private Sub Command1_Click()
Dim retval As Long
'after addressof, put whatever function you'd like.  must be public, and in a module
retval = EnumWindows(AddressOf GetWindowTitles, 0)
End Sub




That's it!  In the "GetWindowTitles" function, you can add whatever code you'd like, such as adding it to a listbox.

Hope this is what you need!

-Sam
0
 

Author Comment

by:ingenito
ID: 7137737
samsonite,

Perfect!  Thank you.
0
 
LVL 1

Expert Comment

by:samsonite1023
ID: 7138087
No problem
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question