Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", EntryPoint:="GetWindowText")> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Int32) As Int32
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowTextLength")> _
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Int32
End Function
<DllImport("user32.dll", EntryPoint:="EnumWindows")> _
Private Shared Function EnumWindows(ByVal Adress As CallBack, ByVal lParam As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowThreadProcessId")> _
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, <Out()> ByRef lpdwProcessId As UInteger) As UInteger
End Function
Private Delegate Function CallBack(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
Private Function Enumerator(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
windowTexts.Add(hWnd, GetText(hWnd))
Return True
End Function
Private Function GetText(ByVal handle As IntPtr) As String
If handle.ToInt32() <= 0 Then Return Nothing : Exit Function
Dim length As Integer = GetWindowTextLength(handle)
If length = 0 Then Return String.Empty : Exit Function
Dim SB As New System.Text.StringBuilder(length + 1)
GetWindowText(handle, SB, SB.Capacity)
Return SB.ToString()
End Function
Private Sub RefreshWindowTexts()
windowTexts.Clear()
EnumWindows(AddressOf Enumerator, Nothing)
End Sub
Private windowTexts As New Dictionary(Of IntPtr, String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim forbiddenWords As New List(Of String)({"facebook", "yahoo"})
RefreshWindowTexts()
Dim keys As IntPtr() = New IntPtr(windowTexts.Keys.Count - 1) {}
windowTexts.Keys.CopyTo(keys, 0)
For Each ptr As IntPtr In keys
For I As Integer = 0 To forbiddenWords.Count - 1
If windowTexts(ptr).ToLower().Contains(forbiddenWords(I).ToLower()) Then
Dim pid As Integer = 0
GetWindowThreadProcessId(ptr, pid)
Process.GetProcessById(pid).Kill()
End If
Next
Next
End Sub
End Class