Click here to Skip to main content
15,883,941 members
Articles / Programming Languages / C#

End any application with system menu using C#

Rate me:
Please Sign up or sign in to vote.
2.62/5 (10 votes)
17 Nov 2002CPOL2 min read 116.2K   2.4K   32   8
This article uses Windows APIs to kill any application with a system menu

Introduction

This article uses the Windows API’s to end the application running under windows environment. So here we will see how to import a function from unmanaged library to managed application. To simplify the process of using platform invoke in our program, we will create a class called Win32, Which will have all native declarations in it.

With C# we can use the Microsoft Windows API SendMessage function to close any active window that has a system menu with the Close option. Below Figure 1 displays the System Menu of Calculator application.

Image 1

Figure 1. Displaying System Menu.

Details

In our sample Kill application we will use SendMessage function to send a message to any window in the environment provided the handle to the window is known. To know the handle we can use FindWindow function which will determine the handle associated with the window the user, wants to close.

In order to use FindWindow, you must know either the Class Name or the Caption (if any) of that window. One can get the information about the Class Name or the Caption using Microsoft Spy++ tool available in Visual Studio .Net Tools. Below Figure 2 shows the working of Spy++ tool.

Image 2

Figure 2. Displaying usage of Microsoft Spy++ Tool.

So lets examine the Win32 class, which has all the Platform Invoke declarations. This class will simply group all the functions we need for our application.

C#
public class Win32 
{ 
    public const int WM_SYSCOMMAND = 0x0112; 
    public const int SC_CLOSE = 0xF060; 

    [DllImport("user32.dll")] 
    public static extern int FindWindow( 
        string lpClassName, // class name 
        string lpWindowName // window name 
    ); 

    [DllImport("user32.dll")] 
    public static extern int SendMessage( 
        int hWnd, // handle to destination window 
        uint Msg, // message 
        int wParam, // first message parameter 
        int lParam // second message parameter 
    ); 
} 

In C# the DllImport attribute is used to identify the name of the actual DLL that contains the exported functions. In order to apply the attribute the method must be defined as static and external.

We will be calling above class methods in button click event of Kill application. Following is the code snippet for it. 

C#
private void button1_Click(object sender, System.EventArgs e) 
{ 
    // Determine the handle to the Application window. 
    int iHandle=Win32.FindWindow(txtClsNm.Text ,txtWndNm.Text); 
    // Post a message to Application to end its existence. 
    int j=Win32.SendMessage(iHandle, Win32.WM_SYSCOMMAND, 
        Win32.SC_CLOSE, 0); 
}

Image 3

Figure 3. Kill application sending “Close Message” to Calculator application.

For further reading on Platform Invoke

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Chandra Hundigam-Venkat has Master degree in Computer Application, Microsoft Certified Professional and Cloud & Software Architect. Significantly involved in Cloud solutions, Cloud native application, Cloud migration, Enterprise application development distributed object oriented system development. Started computer career at the early age of eighteen as an intern in computer labs and had the opportunity to work with various USA fortune 500 companies in various technology and leadership roles.


Comments and Discussions

 
QuestionC# Pin
nandale22-May-07 1:06
nandale22-May-07 1:06 
GeneralAlternative to FindWindow in C# Pin
mycsharpcorner6-Apr-07 3:31
mycsharpcorner6-Apr-07 3:31 
QuestionWhat is the reason behind the int j? Pin
MeterMan19-Jun-04 8:52
MeterMan19-Jun-04 8:52 
GeneralVISIT Pin
Anonymous4-May-04 19:00
Anonymous4-May-04 19:00 
QuestionWhere can I found more information about message constants values? Pin
Javier Lanatta31-Jul-03 3:54
Javier Lanatta31-Jul-03 3:54 
AnswerRe: Where can I found more information about message constants values? Pin
RomkaFromUa5-Sep-06 7:28
RomkaFromUa5-Sep-06 7:28 
Questionwhy don't use Kill()? Pin
Anonymous26-Nov-02 3:25
Anonymous26-Nov-02 3:25 
AnswerRe: why don't use Kill()? Pin
Anonymous26-Nov-02 6:49
Anonymous26-Nov-02 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.