Solved

Getting window titles

Posted on 2002-07-05
9
228 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

829 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