5,557,174 members and growing! (16,994 online)
Email Password   helpLost your password?
Languages » C# » How To     Intermediate

Kill any application with system menu using C#

By Chandra Hundigam

This article uses Windows APIs to kill any application with a system menu
C#, Windows, .NET 1.0, .NETVisual Studio, VS.NET2002, Dev

Posted: 17 Nov 2002
Updated: 17 Nov 2002
Views: 69,544
Bookmarked: 22 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 2.75 Rating: 2.46 out of 5
4 votes, 44.4%
1
1 vote, 11.1%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
4 votes, 44.4%
5

Introduction

This article uses the Windows API’s to kill 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.

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.

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.

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. 

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); 
}

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

For further reading on Platform Invoke

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chandra Hundigam


Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Software Consultant for a US-based company.His areas of interests are in emerging Technologies.
Occupation: Architect
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionC#membernandale2:06 22 May '07  
GeneralAlternative to FindWindow in C#membermycsharpcorner4:31 6 Apr '07  
GeneralWhat is the reason behind the int j?memberwin32newb9:52 19 Jun '04  
GeneralVISITsussAnonymous20:00 4 May '04  
GeneralWhere can I found more information about message constants values?memberJavier Lanatta4:54 31 Jul '03  
GeneralRe: Where can I found more information about message constants values?memberRomkaFromUa8:28 5 Sep '06  
Generalwhy don't use Kill()?sussAnonymous4:25 26 Nov '02  
GeneralRe: why don't use Kill()?sussAnonymous7:49 26 Nov '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Nov 2002
Editor: Nishant Sivakumar
Copyright 2002 by Chandra Hundigam
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project