Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Close some other Application without directly interacting with it in C#.net 2.0.

Author: DevAsp Team
Download Source Code : 688_CloseotherWindow.zip

: In this simple article you will learn how you can close some other Application without directly interacting with it.

Simply just create New Project of C#.

 

Firstly Add Reference of System.Runtime.InteropServices.

 

using System.Runtime.InteropServices;

 

Now drag one Button, two TextBox and two Labels on the form

 

First there is concept to interact with any application directly, if we have Handle of any window we can do any thing with that window. Windows (Operating System) do all working through Messages. So in this article we use same technique to interact with other window.

 

We get handle of any window by giving it Class Name and its Text, we take here example of Notepad. Class name of Notepad is Notepad and you can see its text on top of window.

 

Firstly import these DLLs

[DllImport("user32.dll")]

        public static extern int FindWindow(string lpClassName, string lpWindowName);

 

[DllImport("user32.dll")]

        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

 

FindWindow(… method return you handle of the window………

 

SendMessage(… is use to send message to Specific window……

 

Define some global Variables these are Predefined Message of Windows (OS)

 

public const int WM_COMMAND = 0x0112;

      public const int WM_CLOSE = 0xF060;

 

Now just write code on Form Load Event

 

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp Application Controller";

           

            this.button1.Text = "Close";

           

            this.textBox1.Text = "Notepad";

           

            this.textBox2.Text = "Untitled - Notepad";

 

            label1.Text = "Class Name";

           

            label2.Text = "Window Text";

        }

 

Now write this very simple code on Button Click Event

 

private void button1_Click(object sender, EventArgs e)

        {

            int handle = FindWindow(textBox1.Text, textBox2.Text);

 

            SendMessage(handle, WM_COMMAND, WM_CLOSE, 0);

        }

 

When you run application before click on this button first run a new instance of Notepad this application will close that window.

Article Comments
I want to paste data from windows based application to any currently running application whether its Notepad or visual studio - code will paste the text automatically. how can i do this??

Posted on 9/12/2007 11:53:00 AM by Harry

Hi,
i want all application running while running my application

Posted on 2/4/2008 6:59:36 AM by venkate

how can i close a window immediately once it is started?

Posted on 6/16/2010 4:44:15 AM by anonymous

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How can we raise ComboBox Drop down event on a button’s click event in .net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net