How to send CTRL+BREAK signal to detached command-line process
1.GenerateConsoleCtrlEvent function
Sends a specified signal to a console process group that shares the console associated with the calling process.
for detail: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683155(v=vs.85).aspx
2.Send signal
Browsing though MSDN showed that GenerateConsoleCtrlEvent should be perfect for the job. Just write a little wrapper and we're done, right? If only. When I tried to use it, it always failed with ERROR_INVALID_PARAMETER unless I was trying to signal myself.
The next thing I tried was injecting a thread into the target process to call GenerateConsoleCtrlEvent. I had all the code to do this from SaveConsole. Well, that worked so long as the target process was a console process. Our IDE started Java as a windowed process (javaw.exe instead of java.exe), so the call would fail with ERROR_INVALID_HANDLE.
After much digging around in the debugger I discovered that the entry point that actually does the behavior associated with a signal like ctrl-break is kernel32!CtrlRoutine. The function had the same prototype as ThreadProc, so it can be used with CreateRemoteThread directly, without having to inject code. However, that's not an exported symbol! It's at different addresses (and even has different names) on different versions of Windows. What to do?
Here is the solution I finally came up with. I install a console ctrl handler for my app, then generate a ctrl-break signal for my app. When my handler gets called, I look back at the top of the stack to find out the parameters passed to kernel32!BaseThreadStart. I grab the first param, which is the desired start address of the thread, which is the address of kernel32!CtrlRoutine. Then I return from my handler, indicating that I have handled the signal and my app should not be terminated. Back in the main thread, I wait until the address of kernel32!CtrlRoutine has been retrieved. Once I've got it, I create a remote thread in the target process with the discovered start address. This causes the ctrl handlers in the target process to be evaluated as if ctrl-break had been pressed!
The nice thing is that only the target process is affected, and any process (even a windowed process) can be targeted. One downside is that my little app can't be used in a batch file, since it will kill it when it sends the ctrl-break event in order to discover the address of kernel32!CtrlRoutine.
Small update: I ended up needing to use it in a script, so now I know what happens. If called directly, it will work but it will hang the script because cmd.exe will pause with a Terminate batch job (Y/N)? prompt. Not good for something that's supposed to run unattended. :) However, you can use start to run it in a separate console. You won't be able to redirect the output, but at least you won't get a mysterious prompt. :)
reprinted:http://www.latenighthacking.com/projects/2003/sendSignal/
sourcecode: http://www.latenighthacking.com/projects/2003/sendSignal/SendSignalSrc.zip
3.Sample for call in console app
/*full_path = "sendsignal 9527" 9527 is a pid which process need to be signal up*/
int CTDMaintainManager::CreateProcess(const char * full_path)
{
//Create process
STARTUPINFO info;
PROCESS_INFORMATION pinfo;
memset(&info,,sizeof(info));
info.cb = sizeof(info);
//info.wShowWindow = SW_SHOWNOACTIVATE;
info.dwFlags = STARTF_USESHOWWINDOW;
info.wShowWindow = ; //TRUE表示显示创建的进程的窗口 if(!::CreateProcess(NULL,(LPSTR) full_path, NULL,NULL,FALSE,,NULL,NULL,&info,&pinfo))
{
CT_LOG_ERR("CTDMaintainManager::CreateProcess program %s failed %d.\n", full_path, GetLastError());
return -;
} CT_LOG_CRITICAL(NULL, "CTDMaintainManager::CreateProcess \"%s\" [%d] ok .\n", full_path, pinfo.dwProcessId); return ;
}
How to send CTRL+BREAK signal to detached command-line process的更多相关文章
- mailsend - Send mail via SMTP protocol from command line
Introduction mailsend is a simple command line program to send mail via SMTP protocol. I used to sen ...
- -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
-XX:+PrintClassHistogram –按下Ctrl+Break后,打印类的信息: num #instances #bytes class name ------ ...
- 5 Ways to Send Email From Linux Command Line
https://tecadmin.net/ways-to-send-email-from-linux-command-line/ We all know the importance of email ...
- alias, bg, bind, break, builtin, caller, cd, command,
bash, :, ., [, alias, bg, bind, break, builtin, caller, cd, command, compgen, complete, com ...
- linux command line send email
https://www.tecmint.com/send-email-attachment-from-linux-commandline/ https://stackoverflow.com/ques ...
- TerminateProcess
Remarks The TerminateProcess function is used to unconditionally cause a process to exit. The state ...
- man screen
http://www.gnu.org/software/screen/manual/screen.html Screen User's Manual Next: Overview, Previous: ...
- 哈工大 计算机系统 实验七 TinyShell
所有实验文件可见github 计算机系统实验整理 实验报告 实 验(七) 题 目 TinyShell 微壳 计算机科学与技术学院 目 录 第1章 实验基本信息 - 4 - 1.1 实验目的 - 4 - ...
- Total Commander 8.52 Beta 1
Total Commander 8.52 Beta 1http://www.ghisler.com/852_b1.php 10.08.15 Release Total Commander 8.52 b ...
随机推荐
- 笨办法学Python(三十)
习题 30: Else 和 If 前一习题中你写了一些 “if 语句(if-statements)”,并且试图猜出它们是什么,以及实现的是什么功能.在你继续学习之前,我给你解释一下上一节的加分习题的答 ...
- cs231n 17-18 assignment2 出现 No module named 'past' 解决方法
解决方法: pip install future
- IOS xcode 离线帮助文档安装和安装路径
将想要安装的xcode 帮助文档 版本,将 ‘帮助文档包’ 放入‘xcode 帮助文档安装路径’.再将Xcode软件重启. xcode 帮助文档安装路径: 在‘应用程序’->Xcode软件 右击 ...
- 2018.8.4session的removeAttribute()和invalidate()的区别
session的removeAttribute()和invalidate()的区别 session.invalidate()是销毁跟用户关联session,例如有的用户强制关闭浏览器,而跟踪用户的信息 ...
- VedioCapture
国内的技术的浮躁可见一般,在一个用了七八年的项目里面使用的类,居然拼写都是错的,在网上一搜,转载的也大有人在,最低级的错误,你可以不懂编程,但是只要上过高中,Video这个单词总该学过吧,居然转载的时 ...
- c++ 读入和写入文件
读入 #include<ifstream> ifstream infile; infile.open(img_dir); while(getline(infile,tmp)){ } 写入 ...
- 初尝微信小程序2-基本框架
基本框架: .wxml :页面骨架 .wxss :页面样式 .js :页面逻辑 描述一些行为 .json :页面配置 创建一个小程序之后,app.js,app.json,app.wxss是必须的 ...
- Maven tomcat插件 远程发布【Learn】
Tomcat配置修改: ①.conf/tomcat-users.xml <role rolename="manager-gui"/> <role rolename ...
- mysql 数据库设计规范
MySQL数据库设计规范 目录 1. 规范背景与目的 2. 设计规范 2.1 数据库设计 2.1.1 库名 2.1.2 表结构 2.1.3 列数据类型优化 2.1.4 索引设计 2.1.5 分库分表. ...
- MyEclipse的快捷键大全(超级实用,方便)
常用快捷键 1. [ALT+/] 能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ALT+/]快捷键带来的好处吧. 2. [Ctrl+O] 显示类 ...