|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis 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
With C# we can use the Microsoft Windows API
Figure 1. Displaying System Menu. Details
In our sample Kill application we will use In order to use
Figure 2. Displaying usage of Microsoft Spy++ Tool. So lets examine the 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 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||