terminateprocess (coredll)
Last changed: -201.6.156.27

.

Summary:

This function terminates the specified process and all of its threads. (From MSDN.)

C# Signature:

[DllImport("CoreDll.dll", SetLastError=true)]
static extern int TerminateProcess(IntPtr processIdOrHandle, IntPtr exitCode);

VB Signature:

Declare Function TerminateProcess Lib "coredll.dll" (ByVal processIdOrHandle As IntPtr, ByVal exitCode As IntPtr) As Integer

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Obviously, you need a Process ID to kill. If you want to kill yourself (kill the current process), you'll need GetCurrentProcessId, but as GetCurrentProcessId() is an *inline* function defined in kfuncs.h in Microsoft's "Windows CE Tools" SDK, you can't call it with PInvoke. Use OpenNETCF's implementation, or read the function's inline definition from kfuncs.h and reimplement it in C#.

Sample Code:

/* GetCurrentProcessId() and constants translated from kfuncs.h (SmartPhone2003) to C# */
const int SH_CURPROC = 2;
const int SYS_HANDLE_BASE = 64;
IntPtr GetCurrentProcessId()
{
   return ((IntPtr)(SH_CURPROC + SYS_HANDLE_BASE));
}
private void menuItem1_Click(object sender, EventArgs e)
{
    TerminateProcess(GetCurrentProcessId(), (IntPtr)0);
}

Documentation:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecoreos5/html/wce50lrfterminateprocess.asp