14 Replies - 33429 Views - Last Post: 24 September 2009 - 11:28 AM Rate Topic: -----

#1 tension  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-January 09

Getting A List Of All Open Windows

Post icon  Posted 19 June 2009 - 01:08 AM

Hi everyone, I'm creating a program that will hide/show windows. I am stuck on getting a list of all the open windows. I can get the window's handles, but I would like the actual window title... I am using this code: http://www.dreaminco...snippet2157.htm

Here is my source so far:

- My variables/imports/required functions
Imports System.Runtime.InteropServices
Imports System.Diagnostics

Public Class Form1

	Public Delegate Function CallBack(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
	Public Declare Function EnumWindows Lib "user32" (ByVal Adress As Callback, ByVal y As Integer) As Integer
	Public Declare Function IsWindowVisible Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
	Private Const SW_HIDE As Integer = 0
	Private Const SW_RESTORE As Integer = 9
	Private hWnd As Integer
	Public selectedIndex As Integer
	Private ActiveWindows As New System.Collections.ObjectModel.Collection(Of IntPtr)
	<DllImport("User32")> Private Shared Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
	End Function




-Here is the main function for getting the window's handle
	Public Function GetActiveWindows() As ObjectModel.Collection(Of IntPtr)
		ListView1.Items.Clear()
		EnumWindows(AddressOf Enumerator, 0)
		Return ActiveWindows
	End Function

	Private Function Enumerator(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
		If IsWindowVisible(hwnd) Then
			ListView1.Items.Add(hwnd)
		End If
		Return True
	End Function



As you can see it just lists the window's handle in my listview control just by calling GetActiveWindows()

So how do I get the window's text?

Thanks!

This post has been edited by tension: 19 June 2009 - 01:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting A List Of All Open Windows

#2 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Getting A List Of All Open Windows

Posted 19 June 2009 - 05:00 AM

 Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpWindowText As String, ByVal cch As Integer) As Integer 



			Dim text As String = Space(Int16.MaxValue)
			GetWindowText(hndl, text, Int16.MaxValue)

Was This Post Helpful? 1
  • +
  • -

#3 tension  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-January 09

Re: Getting A List Of All Open Windows

Posted 19 June 2009 - 12:21 PM

Thank you so much! +rep!

//Actually this did not work lol, I must be doing something wrong. I added the function and here's how i get the text:

In my previous Enumerator function i change:
Private Function Enumerator(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
		If IsWindowVisible(hwnd) Then
			ListView1.Items.Add(hwnd)
		End If
		Return True
	End Function



To This:
Private Function Enumerator(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
		Dim text As String = Space(Int16.MaxValue)
		If IsWindowVisible(hwnd) Then
			ListView1.Items.Add(GetWindowText(hwnd, text, Int16.MaxValue))
		End If
		Return True
	End Function



And I get numbers. However they aren't the same numbers as the handle's numbers, but: (i.e)

Quote

0
79
67
126
15


Strage. Any one know what's wrong?

This post has been edited by tension: 19 June 2009 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Getting A List Of All Open Windows

Posted 19 June 2009 - 07:23 PM

I see the problem, GetWindowText returns an integer value which is what your adding to the text box. The text paramater is passed by referance and will be filled with the window text value. You need to do this:

	Dim text As String = Space(Int16.MaxValue)
		If IsWindowVisible(hwnd) Then
GetWindowText(hwnd, text, Int16.MaxValue)
			ListView1.Items.Add(text)
		End If
		Return True


This post has been edited by T3hC13h: 19 June 2009 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 tension  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-January 09

Re: Getting A List Of All Open Windows

Posted 19 June 2009 - 08:14 PM

Thank you so much for your help, it works perfectly!
Was This Post Helpful? 0
  • +
  • -

#6 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Getting A List Of All Open Windows

Posted 19 June 2009 - 08:23 PM

Your most welcome!
Was This Post Helpful? 0
  • +
  • -

#7 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: Getting A List Of All Open Windows

Posted 12 August 2009 - 01:59 PM

View PostT3hC13h, on 19 Jun, 2009 - 07:23 PM, said:


Hi I need help with this code. It works when option strict is off but when I turn option strict on it does not. I have noted two areas that I recieve the errors. I'm self taught and new at developing and this is exactly what I need but I can't make enough sense of it to understand how to fix these two errors. Please help! :)

 Public Function GetActiveWindows() As ObjectModel.Collection(Of IntPtr)
		Listview1.Items.Clear()
		EnumWindows(AddressOf Enumerator, 0) ' Get error here stating Option Strict on does not allow 
'narrowing in implicit type conversion between method private function enumerator(hwnd as system.intptr,
'lparam as integer) as boolean and Delegate Delegate function call back(hwnd as integer, lparam as inter) as
'boolean.
		Return ActiveWindows
	End Function

	Private Function Enumerator(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
		Dim text As String = Space(Int16.MaxValue)
		If IsWindowVisible(hwnd) Then
			GetWindowText(hwnd, text, Int16.MaxValue) 'Get error here stating "Option Strict On disallows
'implicit conversions from 'system.intptr to integer
			ListView1.Items.Add(text)
		End If
		Return True

	End Function

Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Getting A List Of All Open Windows

Posted 12 August 2009 - 02:04 PM

What are the errors/lines?

This post has been edited by AdamSpeight2008: 12 August 2009 - 02:05 PM

Was This Post Helpful? 0
  • +
  • -

#9 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: Getting A List Of All Open Windows

Posted 12 August 2009 - 02:12 PM

View PostAdamSpeight2008, on 12 Aug, 2009 - 01:04 PM, said:

What are the errors/lines?

Hi Thanks for responding so quickly...

I get the error on "Enumerator" and the error is"Option Strict on does not allow narrowing in implicit type conversion between method private function enumerator(hwnd as system.intptr, lparam as integer) as boolean and Delegate Delegate function call back(hwnd as integer, lparam as inter) as boolean."

EnumWindows(AddressOf Enumerator, 0) 


and here I get the error on "hwnd" and the error is "Option Strict On disallows implicit conversions from 'system.intptr to integer."


GetWindowText(hwnd, text, Int16.MaxValue)

Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Getting A List Of All Open Windows

Posted 12 August 2009 - 02:21 PM

Try changing the signature of CallBack to this

Public Delegate Function CallBack(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean


Was This Post Helpful? 0
  • +
  • -

#11 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: Getting A List Of All Open Windows

Posted 12 August 2009 - 03:08 PM

View PostPsychoCoder, on 12 Aug, 2009 - 01:21 PM, said:

Try changing the signature of CallBack to this


That worked! Thank you!
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Getting A List Of All Open Windows

Posted 12 August 2009 - 03:14 PM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#13 lacrue79  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 12-August 09

Re: Getting A List Of All Open Windows

Posted 13 August 2009 - 12:54 PM

I have another question about this. What I'm trying to do is get the Window Titles from the running sessions populated in a list box to allow a user to select the application that they want myapplication to work with. So I used the code from this chat to help me do that and it is working great. Now I just need to be able to use the User selected application (from the listbox). However, I do not know how to do that.

I believe that I may need to use similar API calls to get the path of the application like I got the title but can't find anything on the Web that shows me how to do that.

Just to add more details it is Extra sessions that I'm trying to work with. So my application will populate the list of already running extrasessions (since the users usually already have 3 or 4 of these extrasessions already running for other stuff they do). The user will select which one of those sessions they want my application to work with. Then my application should be able to intrface with that session. Once I get the path I know what to do next so that is all I need to know.

Here is the code that I have so far...Can I just modify this in some way to also get the application path at the time that I get the window title? Thanks to anyone that can help out!

 Imports System.Runtime.InteropServices
Imports System.Diagnostics
Module MyExtraOpenWindows

	Public Delegate Function CallBack(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean
	Public Declare Function EnumWindows Lib "user32" (ByVal Adress As CallBack, ByVal y As Integer) As Integer
	Public Declare Function IsWindowVisible Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
	Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpWindowText As String, ByVal cch As Integer) As Integer
	Private Const SW_HIDE As Integer = 0
	Private Const SW_RESTORE As Integer = 9
	Private hWnd As Integer
	Public selectedIndex As Integer
	Private ActiveWindows As New System.Collections.ObjectModel.Collection(Of IntPtr)

	<DllImport("User32")> Private Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
	End Function

	Public Function GetActiveWindows() As ObjectModel.Collection(Of IntPtr)
		UserInfoForm.cboSessions.Items.Clear()
		EnumWindows(AddressOf Enumerator, 0)
		Return ActiveWindows
	End Function
	Private Function Enumerator(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean

		Dim Windwtext As String = Space(Int16.MaxValue)
		Dim WinName As System.Text.StringBuilder

		

		If IsWindowVisible(hwnd) Then
			GetWindowText(hwnd, Windwtext, Int16.MaxValue)
			
			WinName = New System.Text.StringBuilder(UCase(Windwtext))

			If InStr(WinName.ToString, "MYEXTRA") > 0 Then
				WinName.Replace("~1 - MYEXTRA! ENTERPRISE", " ")

				UserInfoForm.cboSessions.Items.Add(WinName.ToString)
			End If
		End If
		Return True

	End Function

   


End Module

Was This Post Helpful? 0
  • +
  • -

#14 asura5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-August 09

Re: Getting A List Of All Open Windows

Posted 23 September 2009 - 10:44 AM

You could have side by side list boxes and just add the list of hwnds to the second (invisible?) list box - then use the same index of the window you want to use
Was This Post Helpful? 0
  • +
  • -

#15 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Getting A List Of All Open Windows

Posted 24 September 2009 - 11:28 AM

Hi,



There's a much easier way :D

For Each p as Process in Process.GetProcesses
If p.MainWindowTitle = Nothing Then
Else
Listbox1.Items.Add(p.MainWindowTitle)
End If

This post has been edited by Jack Eagles1: 24 September 2009 - 11:46 AM

Was This Post Helpful? 1

Page 1 of 1