Solved

Getting window titles

Posted on 2002-07-05
9
225 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now