Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Getting window titles

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
ingenito
Asked:
ingenito
1 Solution
 
Richie_SimonettiCommented:
Strange, that code has nothing to do with your error...
0
 
priya_pbkCommented:

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
 
ingenitoAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
samsonite1023Commented:
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
 
ingenitoAuthor Commented:
samsonite,

Just a code for all parent windows running, i just needs the title bars of them.  Thanks.
0
 
samsonite1023Commented:
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
 
samsonite1023Commented:
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
 
ingenitoAuthor Commented:
samsonite,

Perfect!  Thank you.
0
 
samsonite1023Commented:
No problem
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now