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 ...
随机推荐
- php的yii框架开发总结9
这一篇讲解怎么实现的自动发邮件的功能,我在网上查了很多资料,很多都是用定时检测来实现的,我试过,效率太低,网站也卡了. 后来就写了一个.bat文件来实现刷新页面,用了windows的定时任务定时来运行 ...
- ES6相关特性(解构赋值)
解构赋值:本质上是一种匹配模式,等号两边的模式相同,则左边的变量可以被赋予对应的值. 注意:null & undefined 不能解构赋值!!! 数组的解构赋值: let [a,[[b],c] ...
- 笨办法学Python(三十四)
习题 34: 访问列表的元素 列表的用处很大,但只有你能访问里边的内容时它才能发挥出作用来.你已经学会了按顺序读出列表的内容,但如果你要得到第 5 个元素该怎么办呢?你需要知道如何访问列表中的元素.访 ...
- SQL SERVER 下:1、递归查询父分类下的各个子分类。 2、查询每个商品分类中最贵的前两个商品SQL
1.递归查询父分类下的各个子分类.表设计: SQL: --CTE 语句(适用于MSSQL2005以后版本) with cte_testNavi(Id,Name,Pid ) as ( --这是查询语句 ...
- VERITA Netbackup日常巡检详细说明
VERITA备份日常监控 一. 相关检查方法.命令 1.1 启动NBU的图形管理界面: /usr/openv/netbackup/bin/jnbSA & 1.2字符界面命令: 1.2.1cat ...
- matlab远程调试
转自:http://blog.163.com/hair_communication/blog/static/20198911920124145414945/ 只是作者好像也是转来的,原来出处好像是百度 ...
- Vue 前端md5加密
用户注册时将加密后的密码发送给后端存储 当登陆的时候,再将加密后的密码和数据库中加密的密码相匹配. npm: https://www.npmjs.com/package/crypto-browseri ...
- 在ubuntu中docker的简单使用(一)
>>docker version 当运行docker version 命令出现Cannot connect to Docker daemon. Is the docker daemon r ...
- P2661 信息传递 DFS
题目链接:洛谷 P2661 信息传递 一个人要想知道自己的生日,就意味着信息的传递是成环的,因为每轮信息只能传递一个人,传递的轮数就等于环的大小 环的大小就等于环中的两个点到第三个点的距离之和加一,我 ...
- 【赛时总结】◇赛时·VII◇ Atcoder ABC-106
[赛时·VII] ABC-106 一条比赛时莫名其妙发了半个小时呆的菜鸡&咸鱼得到了自己应有的下场……279th. Rating:1103(+) 终于AK,一次通过…… ◇ 简单总结 ABC还 ...