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. helm深入学习

    Helm把Kubernetes资源(比如deployments.services或 ingress等) 打包到一个chart中,而chart被保存到chart仓库.通过chart仓库可用来存储和分享c ...

  2. 【js基础修炼之路】- 手把手教你实现bind

    手写bind前我们先回顾一下bind有哪些特性,以便更好的理解bind和实现bind. bind的特性 var obj = { a: 100, say(one, two) { console.log( ...

  3. leetcode: 树

    1. sum-root-to-leaf-numbers Given a binary tree containing digits from0-9only, each root-to-leaf pat ...

  4. ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较

    ABAP 使用关键字重复引入一个include program,会报syntax error: 原因是因为ABAP对include这个关键字的实现和其他编程语言有点不一样,在激活时简单地把被inclu ...

  5. 257. Binary Tree Paths (dfs recurive & stack)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  6. IOS GCD03-其他用法

    #define global_queue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define main_queu ...

  7. 轻量级HTTP服务器Nginx(常用配置实例)

    轻量级HTTP服务器Nginx(常用配置实例)   文章来源于南非蚂蚁   Nginx作为一个HTTP服务器,在功能实现方面和性能方面都表现得非常卓越,完全可以与Apache相媲美,几乎可以实现Apa ...

  8. Linux 启动、停止、重启tomcat工具(Shell脚本)

    1.   启动 #!/bin/bash pids=`ps -ef | grep java | grep -w tomcat | awk '{print $2}'` #pids=`ps -ef | gr ...

  9. hihoCoder 网络流四·最小路径覆盖

    题面带解释 hihoCoder感觉很好. 网络流的精华就是建图 #include<cstdio> #include<iostream> #include<algorith ...

  10. image retrieval数据集

    1. Oxford,vgg组,主要是building方面的数据.http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/index.html 2. Calte ...