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之 ...
随机推荐
- 二叉查找树(c++)
二叉查找数的操作: #include <iostream> using namespace std; typedef struct BitNode { int data; struct B ...
- 最近项目需要用到AdminLTE,所以整理一份中文版的小教程
先介绍一下AdminLTE的官方网站:AdminLTE官方网站 和GitHub:AdminLTE的github,可以在上面自行下载. AdminLTE 是一个完全响应管理模板,主要依赖于 Bootst ...
- winxp如何开启SNMP服务
1.先安装SNMP组件 开始——> 控制面板——>添加或删除程序——>添加/删除windows组件——>管理和监视工具(前面方框选择后)——>详细信息——>简 ...
- IOS 拼接按钮文字
NSMutableString *tempAnswerTitle=[[NSMutableString alloc]init]; for(UIButton *answerBtn in self.answ ...
- P1316 丢瓶盖
题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...
- HttpServerUtility 和 HttpUyility
参考:msdn HttpServerUtility 提供用于处理 Web 请求的 Helper 方法. 2017/08/07 加密解码 这个类没有构造函数,所以不能直接new. ...
- 创建自己的网站博客--Hexo
原文地址:https://www.xingkongbj.com/blog/hexo/creat-hexo.html 安装环境 安装 node 下载对应版本并安装 node . 安装 Git Windo ...
- BZOJ2683: 简单题(cdq分治 树状数组)
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2142 Solved: 874[Submit][Status][Discuss] Descripti ...
- 私有maven库发布及使用流程
## 私有maven库发布流程 ### 环境配置 - idea环境下,如果使用内置maven,需要手动生成settings.xml,并关联. - 操作如下 - 生成settings.xml 右键pom ...
- 转:Java后端面试自我学习
引自:https://www.cnblogs.com/JavaArchitect/p/10011253.html 最近面试java后端开发的感受:如果就以平时项目经验来面试,通过估计很难——再论面试前 ...