Solved

Kill all Messagebox in Visual studio 2008

Posted on 2009-07-11
7
581 Views
Last Modified: 2013-11-27
I want to kill all messagebox from external program , or kill all open messagebox  in windows. thanks a lot
0
Comment
Question by:Gknrsy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
7 Comments
 

Author Comment

by:Gknrsy
ID: 24831502
i found some c# codes but i don't know how in vb2008

// showbox.cs

using System;
using System.Windows.Forms;

class Program
{
    static void Main()
    {
       Application.Run(new ShowBox());      
    }
}

class ShowBox : Form
{
    private Button btn;

    public ShowBox()
    {
       btn = new Button();
       btn.Text = "Press me";
       btn.Click += new EventHandler(button_Click);
       this.Controls.Add(btn);
    }

    private void button_Click(object sender, EventArgs e)
    {
       MessageBox.Show(this, "Hello from Show Box", "Greeting");
    }
}  

// capturebox.cs

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

class CaptureBox
{
    [DllImport("user32.dll")]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);


    static IntPtr hwndParent;

    static void Main()
    {
        Process[] procs = Process.GetProcessesByName("showbox");

        if (procs.Length == 0)
        {
            Console.WriteLine("Showbox isn't running");
            return;
        }
     
        hwndParent = procs[0].MainWindowHandle;

        bool hasAppeared = false;

        do        
        {
            if (!procs[0].HasExited)
            {
               hasAppeared = CheckMessageBox();
               Thread.Sleep(1000); // check every second
            }
            else
            {
               Console.WriteLine("Showbox has exited");
               break;
            }
        }          
        while(!hasAppeared);
    }

    static bool CheckMessageBox()
    {
        // IntPtr hwndMB = FindWindowEx(hwndParent, IntPtr.Zero, "#32770", "Greeting");
        // Apparently MessageBox is always top level in .NET
        IntPtr hwndMB = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Greeting");
        if (hwndMB == IntPtr.Zero)
        {
           Console.WriteLine("MessageBox not showing");
           return false;
        }
        else
        {          
           Console.WriteLine("MessageBox has appeared");
           return true;
        }
    }
}
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 300 total points
ID: 24832307
Below is a VB translation of the code you posted. For the class CaptureBox, I started a VB Console Application project and put the code inside a module file.
''''''''''''''''''''''''''''''''''''''''''''
'' Showbox code
''''''''''''''''''''''''''''''''''''''''''''
Public Class ShowBox
    Private btn As Button
 
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        btn = New Button()
        btn.Text = "Press me"
        AddHandler btn.Click, AddressOf button_Click
        Controls.Add(btn)
    End Sub
 
    Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
        MessageBox.Show(Me, "Hello from Show Box", "Greeting")
    End Sub
 
End Class
 
''''''''''''''''''''''''''''''''''''''''''''
'' CaptureBox code
''''''''''''''''''''''''''''''''''''''''''''
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.Threading
 
Module Module1
    Dim hwndParent As IntPtr
 
    Sub Main()
        Dim procs() As Process = Process.GetProcessesByName("showbox")
 
        If procs.Length = 0 Then
            Console.WriteLine("Showbox isn't running.");
            Return
        End If
 
        hwndParent = procs(0).MainWindowHandle
 
        Dim hasAppeared As Boolean = False
 
        Do
            If Not procs(0).HasExited Then
                hasAppeared = CheckMessageBox()
                Thread.Sleep(1000)
            Else
                Console.WriteLine("Showbox has exited")
                Exit Do
            End If
        Loop While Not hasAppeared
    End Sub
 
    Function CheckMessageBox() As Boolean
        Dim hwndMB As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Greeting")
 
        If hwndMB = IntPtr.Zero Then
            Console.WriteLine("MessageBox not showing.")
            Return False
        Else
            Console.WriteLine("MessageBox has appeared")
            Return True
        End If
    End Function
 
    <DllImport("user32.dll")> _
    Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    End Function
 
 
End Module

Open in new window

0
 

Author Comment

by:Gknrsy
ID: 24833242
i just found this code in internet. is it working??
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:Gknrsy
ID: 24833244
i asked because just tomarrow i will try :)
0
 

Author Comment

by:Gknrsy
ID: 24837022
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Threading
Module Module1



    Dim hwndParent As IntPtr



    Function CheckMessageBox() As Boolean
        Dim hwndMB As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Greeting!")

        If hwndMB = IntPtr.Zero Then
            MsgBox("MessageBox not showing.")
            Return False
        Else
            MsgBox("MessageBox has appeared")
            Return True
        End If
    End Function

    <DllImport("user32.dll")> _
    Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    End Function


End Module

with this code i am checking messagebox working or not .

now if messagebox working how i will close this message box?


0
 

Author Comment

by:Gknrsy
ID: 24837068

    Function CheckMessageBox() As Boolean
        Dim hwndMB As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Hello")

        If hwndMB = IntPtr.Zero Then
            Exit Function
            Return False
        Else
            Dim procs() As Process = Process.GetProcessesByName("ABC")
            AppActivate(procs(0).Id)
            SendKeys.Send("{enter}") ' this sends F5 to the window
            Return True
        End If
    End Function

i wite this code , but this code working with focus i mean when sending enter key to messagebox it becomes active. i want to send enter key or just kill message box background , i don't want to it brings front :) sorry for bad explanation.
0
 

Accepted Solution

by:
Gknrsy earned 0 total points
ID: 24837225
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Threading
Module Module1

    Dim hwndParent As IntPtr

    Function CheckMessageBox() As Boolean
        Dim hwndMB As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "")

        If hwndMB = IntPtr.Zero Then
            Exit Function
            Return False
        Else
           SendMessage(hwndMB, WM_CLOSE, 0, 0)
            Return True
        End If
    End Function

    <DllImport("user32.dll")> _
   Function SendMessage(ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, ByVal wParam As Long, _
    ByVal lParam As Long) As Long

    End Function

    Private Const WM_CLOSE = &H10


    <DllImport("user32.dll")> _
    Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
    End Function


End Module


This is working very well :)
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question