TerminateProcess
Remarks
The TerminateProcess function is used to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather thanExitProcess.
This function stops execution of all threads within the process and requests cancellation of all pending I/O. The terminated process cannot exit until all pending I/O has been completed or canceled. When a process terminates, its kernel object is not destroyed until all processes that have open handles to the process have released those handles.
TerminateProcess is asynchronous; it initiates termination and returns immediately. If you need to be sure the process has terminated, call the WaitForSingleObject function with a handle to the process.
A process cannot prevent itself from being terminated.
DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout ) Purpose:
Shut down a -Bit Process (or -bit process under Windows ) Parameters:
dwPID
Process ID of the process to shut down. dwTimeout
Wait time in milliseconds before shutting down the process. Return Value:
TA_FAILED - If the shutdown failed.
TA_SUCCESS_CLEAN - If the process was shutdown using WM_CLOSE.
TA_SUCCESS_KILL - if the process was shut down with
TerminateProcess().
NOTE: See header for these defines.
----------------------------------------------------------------*/
DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )
{
HANDLE hProc ;
DWORD dwRet ; // If we can't open the process with PROCESS_TERMINATE rights,
// then we give up immediately.
hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE,
dwPID); if(hProc == NULL)
{
return TA_FAILED ;
} // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
// matches your process's.
EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ; // Wait on the handle. If it signals, great. If it times out,
// then you kill it.
if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
dwRet=(TerminateProcess(hProc,)?TA_SUCCESS_KILL:TA_FAILED);
else
dwRet = TA_SUCCESS_CLEAN ; CloseHandle(hProc) ; return dwRet ;
}
BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam )
{
DWORD dwID ; GetWindowThreadProcessId(hwnd, &dwID) ; if(dwID == (DWORD)lParam)
{
PostMessage(hwnd, WM_CLOSE, 0, 0) ;
} return TRUE ;
}
Terminating a process has the following results:
- Any remaining threads in the process are marked for termination.
- Any resources allocated by the process are freed.
- All kernel objects are closed.
- The process code is removed from memory.
- The process exit code is set.
- The process object is signaled.
While open handles to kernel objects are closed automatically when a process terminates, the objects themselves exist until all open handles to them are closed. Therefore, an object will remain valid after a process that is using it terminates if another process has an open handle to it.
The GetExitCodeProcess function returns the termination status of a process. While a process is executing, its termination status is STILL_ACTIVE. When a process terminates, its termination status changes from STILL_ACTIVE to the exit code of the process.
When a process terminates, the state of the process object becomes signaled, releasing any threads that had been waiting for the process to terminate. For more about synchronization, see Synchronizing Execution of Multiple Threads.
When the system is terminating a process, it does not terminate any child processes that the process has created. Terminating a process does not generate notifications for WH_CBT hook procedures.
Use the SetProcessShutdownParameters function to specify certain aspects of the process termination at system shutdown, such as when a process should terminate relative to the other processes in the system.
How Processes are Terminated
A process executes until one of the following events occurs:
- Any thread of the process calls the ExitProcess function. Note that some implementation of the C run-time library (CRT) call ExitProcess if the primary thread of the process returns.
- The last thread of the process terminates.
- Any thread calls the TerminateProcess function with a handle to the process.
- For console processes, the default console control handler calls ExitProcess when the console receives a CTRL+C or CTRL+BREAK signal.
- The user shuts down the system or logs off.
Do not terminate a process unless its threads are in known states. If a thread is waiting on a kernel object, it will not be terminated until the wait has completed. This can cause the application to stop responding.
The primary thread can avoid terminating other threads by directing them to call ExitThread before causing the process to terminate (for more information, see Terminating a Thread). The primary thread can still call ExitProcess afterwards to ensure that all threads are terminated.
The exit code for a process is either the value specified in the call to ExitProcess or TerminateProcess, or the value returned by the main or WinMain function of the process. If a process is terminated due to a fatal exception, the exit code is the value of the exception that caused the termination. In addition, this value is used as the exit code for all the threads that were executing when the exception occurred.
If a process is terminated by ExitProcess, the system calls the entry-point function of each attached DLL with a value indicating that the process is detaching from the DLL. DLLs are not notified when a process is terminated byTerminateProcess. For more information about DLLs, see Dynamic-Link Libraries.
If a process is terminated by TerminateProcess, all threads of the process are terminated immediately with no chance to run additional code. This means that the thread does not execute code in termination handler blocks. In addition, no attached DLLs are notified that the process is detaching. If you need to have one process terminate another process, the following steps provide a better solution:
- Have both processes call the RegisterWindowMessage function to create a private message.
- One process can terminate the other process by broadcasting a private message using theBroadcastSystemMessage function as follows:
DWORD dwRecipients = BSM_APPLICATIONS;
UINT uMessage = PM_MYMSG;
WPARAM wParam = 0;
LPARAM lParam = 0; BroadcastSystemMessage(
BSF_IGNORECURRENTTASK, // do not send message to this process
&dwRecipients, // broadcast only to applications
uMessage, // registered private message
wParam, // message-specific value
lParam ); // message-specific value - The process receiving the private message calls ExitProcess to terminate its execution.
The execution of the ExitProcess, ExitThread, CreateThread, CreateRemoteThread, and CreateProcess functions is serialized within an address space. The following restrictions apply:
- During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is finished for the process.
- Only one thread at a time can be in a DLL initialization or detach routine.
- The ExitProcess function does not return until there are no threads are in their DLL initialization or detach routines.
Excerpt:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686714(v=vs.85).aspx
http://support.microsoft.com/kb/178893
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686722(v=vs.85).aspx
TerminateProcess的更多相关文章
- TerminateProcess实现关闭任意程序
#include <Windows.h> #include <tchar.h> int WINAPI _tWinMain(HINSTANCE hInstance, HINSTA ...
- 调用TerminateProcess是无法触发DLL_PROCESS_DETACH的
当应用程序中调用TerminateProcess函数,对于在DllMain函数中处理DLL_PROCESS_DETACH的额外代码操作是无法被执行的.比如:释放资源.数据持久化等.
- OpenProcess、GetExitCodeProcess、TerminateProcess
//声明: {返回进程的句柄} OpenProcess( dwDesiredAccess: DWORD; {訪问选项} bInheritHandle: BOOL; {是否能继承; Tr ...
- 使用ExitProcess()结束本进程、TerminateProcess 结束进程
进程只是提供了一段地址空间和内核对象,其运行时通过在其地址空间内的主线程来体现的.当主线程的进入点函数返回时,进程也就随之结束.这种进程的终止方式是进程的正常退出,进程中的所有线程资源都能够得到正确的 ...
- WinAPI: OpenProcess、GetExitCodeProcess、TerminateProcess (测试强制关闭 OICQ)
原文:http://www.cnblogs.com/del/archive/2008/03/10/1098502.html //声明: {返回进程的句柄} OpenProcess( dwDesir ...
- Windows API 25篇 TerminateProcess
导语:结束一个进程的方法通常有:exit(), ExitProcess, TerminateProcess. 通常一个进程在正常情况下结束的话,系统会调用 ExitProcess函数结束进程,但有时候 ...
- TerminateProcess的使用问题
最好时外部进程来结束目标进程,类似于任务管理器的结束目标进程方式.如果是自身进程想结束自身,可能不同版本的windows行为不一致,有一些能自身强制退出,有一些强制退出不了. 本来MSDN上就说了这个 ...
- C#的Process类调用第三方插件实现PDF文件转SWF文件
在项目开发过程中,有时会需要用到调用第三方程序实现本系统的某一些功能,例如本文中需要使用到的swftools插件,那么如何在程序中使用这个插件,并且该插件是如何将PDF文件转化为SWF文件的呢?接下来 ...
- Windows API 函数列表 附帮助手册
所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...
随机推荐
- Linux命令之创建文件夹3
1)mkdir fyr即可在当前目录下创建一个文件夹 2)在fyr文件夹下创建一个子目录 mkdir fyr/fyr1 注意:如果不存在父层目录直接创建对应父层目录下的子目录mkdir FYR/f ...
- Spark资源管理
Spark资源管理 1.介绍 Spark资源管控分为spark集群自身可支配资源配置和job所用资源配置. 2.spark集群支配资源控制 在spark的conf/spark-env.sh文件中可以指 ...
- C#中生成随机数的几种方法
Random 类 Random类默认的无参构造函数可以根据当前系统时钟为种子,进行一系列算法得出要求范围内的伪随机数 Random rd = new Random() rd.next(,)(生成1~1 ...
- 利用C语言编辑画图程序的实现方法
不知道大家在进行开发县级电网调度自动化系统的时候,是否都会遇到一个问题就是:要绘制一个电力系统一次接线图.大家都应该知道其实电力系统的一次接线图是较为复杂的,如果想要使用一般的编程方法来进行绘制的话, ...
- Html : 规范html代码的网站
html代码的规范也是很重要的,这里推荐一个网站,很好用,仓鼠是经常用的啦! https://htmlformatter.com/ 以上
- 字节流, FileOutputStream类,FileInputStream类,复制文件,字符流
字节输出流OutputStream OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节 基本方法: 子类可继承调用以上方法 FileOutputStream类 构造方 ...
- Linux驱动学习(编写一个最简单的模块)
在Linux中想做驱动开发,那么一定要先熟悉module的使用和编写 一.什么是module 从名字上看就是模块的意思,我个人的理解就是一个一个的小程序,可以进行动态的安装和卸载,而在这里面就实现一些 ...
- 基于Ceph分布式集群实现docker跨主机共享数据卷
上篇文章介绍了如何使用docker部署Ceph分布式存储集群,本篇在此基础之上,介绍如何基于Ceph分布式存储集群实现docker跨主机共享数据卷. 1.环境准备 在原来的环境基础之上,新增一台cen ...
- mysql5.6 配置 文件
mysql 3306 主库配置文件 [client] port = 3306 default-character-set=utf8mb4 socket = /ssd/mysql/3306/tmp/my ...
- mysql基础 反范式化