Solved

Getting window titles

Posted on 2002-07-05
9
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Suggested Courses
Course of the Month6 days, 9 hours left to enroll

634 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