-1

I have the problem, I'm in a network, where everyone sends messages through msg. Through it's really annoying, I thought about killing the messageboxes process. Running under csrss.exe thats not possible. When I looked through the taskmanager I noticed, when opening the tree structure of the csrss.exe, there was the messagebox and I could kill it. Has anyone any idea, how to do that in Visual Basic.

djv
  • 15,168
  • 7
  • 48
  • 72
CodeSpoof
  • 55
  • 4
  • What is `msg` exactly? – djv Nov 12 '19 at 18:45
  • 1
    @djv https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/msg – GSerg Nov 12 '19 at 18:48
  • You can try with UI Automation, using a `WindowPattern.WindowOpenedEvent`. Get the processID of the `csrss.exe` processes and when you're notified that a new Window is opened, compare the Window processID with the ones you've stored and close the Window if it's a match. To close a Window, you need to get the WindowPatter of the Automation element and use its Close() method (e.g., `Dim pattern As object = Nothing) If [Element].TryGetCurrentPattern(WindowPattern.Pattern, pattern) Then dim wPattern = DirectCast(pattern, WindowPattern) wPattern.Close() end if`. – Jimi Nov 12 '19 at 19:25
  • You can find [here](https://stackoverflow.com/a/51505218/7444103) a VB.Net implementation of a `WindowPattern.WindowOpenedEvent` notification. Btw, I never tested in this scenario, so take it *as is*. – Jimi Nov 12 '19 at 19:25

2 Answers2

1

A simple web search led me to this question, which has some good answers already.

With a little work, I came up with this solution. It find the process by name (csrss), then filters by window title (Message from).

Sub Main()
    killProcesses("csrss", "Message from")
End Sub

Private Sub killProcesses(processName As String, mainWindowTitle As String)
    Dim processes = Process.GetProcesses().
        Where(Function(p) p.ProcessName.Contains(processName)).
        Where(Function(p) p.MainWindowTitle.Contains(mainWindowTitle))
    '  (Where clauses can be combined into one of course)
    For Each p In processes
        p.CloseMainWindow()
    Next
End Sub

It works like this. Send a message

C:\Users\djv>msg djv hey whats up

Get a message

enter image description here

Here is the task

enter image description here

And what the code sees

enter image description here

It is closed upon the p.CloseMainWindow() call. It will also close any other window which matches the description, since it's not limited to one. This should satisfy your requirement.

djv
  • 15,168
  • 7
  • 48
  • 72
0

Just so the question hast a proper answer, I found a solution using the win32 api (PInvokes) specifically the user32.dll. It provides methods like GetWindow, GetWindowThreadProcessId and CloseWindow, which solved my problem.

CodeSpoof
  • 55
  • 4