Jump to content
Sign in to follow this  
Leudassdf

Obter todos os Processos e subprocessos

Recommended Posts

Leudassdf

Boas pessoal,

Eu tenho o seguinte código para obter todos os processos:

	For Each p As Process In Process.GetProcesses
		If p.MainWindowTitle = String.Empty = False And p.MainWindowTitle = "Optimizer" Then
			handle_janela = p.MainWindowHandle.ToInt32
			MsgBox(handle_janela)
		End If

	Next

O problema é o seguinte. Eu estou no windows 8. 1 Processo neste caso tem 2 janelas abertas. E eu queria obter o handler de uma dessas janelas.

Por exemplo se abrirem um browser duas vezes no windows 8 aparece o nome do browser e por baixo os nomes das janelas desse processo. E era isso que eu queria. o nome de todas as janelas associado a um determinado processo. Isto é possível?

Share this post


Link to post
Share on other sites
nelsonr

Boas,

vê aqui se o exemplo a meio da página ajuda:

http://www.dreamincode.net/forums/topic/207233-get-a-list-of-active-windows-their-process-names/

Não uso o Windows 8, mas testei com o 7, para listar as várias janelas do firefox:

Definições:

<DllImport("USER32.DLL")> _
Private Shared Function GetShellWindow() As IntPtr
End Function

<DllImport("USER32.DLL")> _
Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function

<DllImport("USER32.DLL")> _
Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer
End Function

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, <Out()> ByRef lpdwProcessId As UInt32) As UInt32
End Function

<DllImport("USER32.DLL")> _
Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
End Function

Private Delegate Function EnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean

<DllImport("USER32.DLL")> _
Private Shared Function EnumWindows(ByVal enumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean
End Function

Private hShellWindow As IntPtr = GetShellWindow()
Private dictWindows As New Dictionary(Of IntPtr, String)
Private currentProcessID As Integer

Public Function GetOpenWindowsFromPID(ByVal processID As Integer) As IDictionary(Of IntPtr, String)
	dictWindows.Clear()
	currentProcessID = processID
	EnumWindows(AddressOf enumWindowsInternal, 0)
	Return dictWindows
End Function

Private Function enumWindowsInternal(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
	If (hWnd <> hShellWindow) Then
		Dim windowPid As UInt32
		If Not IsWindowVisible(hWnd) Then
			Return True
		End If
		Dim length As Integer = GetWindowTextLength(hWnd)
		If (length = 0) Then
			Return True
		End If
		GetWindowThreadProcessId(hWnd, windowPid)
		If (windowPid <> currentProcessID) Then
			Return True
		End If
		Dim stringBuilder As New System.Text.StringBuilder(length)
		GetWindowText(hWnd, stringBuilder, (length + 1))
		dictWindows.Add(hWnd, stringBuilder.ToString)
	End If
	Return True
End Function

E usado assim:

   For Each P As Process In Process.GetProcesses
		If P.ProcessName = "firefox" Then
			'
			Dim windows As IDictionary(Of IntPtr, String) = GetOpenWindowsFromPID(P.Id)

			For Each kvp As KeyValuePair(Of IntPtr, String) In windows
				MessageBox.Show(kvp.Value)
			Next
		End If
	Next

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.