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的更多相关文章

  1. 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 ...

  2. -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息

    -XX:+PrintClassHistogram –按下Ctrl+Break后,打印类的信息: num     #instances         #bytes  class name ------ ...

  3. 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 ...

  4. alias, bg, bind, break, builtin, caller, cd, command,

    bash,  :,  .,  [, alias, bg, bind, break, builtin, caller, cd, command,       compgen, complete, com ...

  5. linux command line send email

    https://www.tecmint.com/send-email-attachment-from-linux-commandline/ https://stackoverflow.com/ques ...

  6. TerminateProcess

    Remarks The TerminateProcess function is used to unconditionally cause a process to exit. The state ...

  7. man screen

    http://www.gnu.org/software/screen/manual/screen.html Screen User's Manual Next: Overview, Previous: ...

  8. 哈工大 计算机系统 实验七 TinyShell

    所有实验文件可见github 计算机系统实验整理 实验报告 实 验(七) 题 目 TinyShell 微壳 计算机科学与技术学院 目 录 第1章 实验基本信息 - 4 - 1.1 实验目的 - 4 - ...

  9. 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 ...

随机推荐

  1. npm升级自身

    参考:https://github.com/felixrieseberg/npm-windows-upgrade Usage First, ensure that you can execute sc ...

  2. Python基础学习之字符串(2)

    字符串常用方法 1.s.capitalize() 描述:返回字符串s的副本,并将首字符变为大写. 示例: >>> s='yesterday when I was Young!' &g ...

  3. Saw a tweet from Andrew Liam Trask, sounds like Oxford DeepNLP 2017 class have all videos slides practicals all up. Thanks Andrew for the tip!

    Saw a tweet from Andrew Liam Trask, sounds like Oxford DeepNLP 2017 class have all videos/slides/pra ...

  4. Html : 规范html代码的网站

    html代码的规范也是很重要的,这里推荐一个网站,很好用,仓鼠是经常用的啦! https://htmlformatter.com/ 以上

  5. Bonita portal 源码编译(未完成)

    首先下载源代码 https://github.com/bonitasoft/bonita-portal-js 以下内容为Github 的安装教程包含我安装过程中遇到的问题.并加以修正 Bonita p ...

  6. 2017.9.17 HTML学习总结---table标签

    接上: 2.1.3  HTML表单标签与表单设计 表单是用户与服务器交互的主要方法,用户在表单中输入数据,提交给服务器程序来处理. (1)表单的组成: 文本框(text),密码框(password), ...

  7. ZooKeeper 完全分布式集群环境搭建

    1. 搭建前准备 示例共三台主机,主机IP映射信息如下: 192.168.32.101 s1 192.168.32.102 s2 192.168.32.103 s3 2.下载ZooKeeper, 以  ...

  8. C#定义常量的两种方法

    在C#中定义常量的方式有两种,一种叫做静态常量(Compile-time constant),另一种叫做动态常量(Runtime constant).前者用“const”来定义,后者用“readonl ...

  9. C# for语句

    一.C# for语句 for语句是C#语言中使用频率最高的循环语句. 1. for语句 语法格式如下: for(initializer; condition; iterator){    embedd ...

  10. (排班表二)后台动态绘制Grid表格

    后台动态绘制值班表(Grid表格 列名不固定) 要求:表头除了值班人姓名,还要显示日期,及每天的星期值,用斜杠‘/’分隔.即:几号/星期几 最终实现的效果:根据查询的年月显示每个值班人查询月份每天的值 ...