How to send Cntrol C to another running application

Newbie Member
7Jan2009,10:48   #1
programmer321's Avatar
Hi,

I am trying to write a C code on windows to send cntrol C signal to a running, so that the running application closes gracefully.

Here is the code I have tried to write:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include <windows.h>

void main()
{
 int i=0;
 HWND h = NULL;
 h = FindWindow(NULL,"RUNNING EXE");
 printf("[%d]\n",h);
 i = SendMessage(h,WM_CHAR,0x03,0);
 printf("[%d]\n",i);
}
But the running application is not closing gracefully, it is simply receiving ^C. This is not solving my purpose.

Waiting for a quick response.

Regards.

Last edited by shabbir; 7Jan2009 at 12:08.. Reason: Code block
Mentor
7Jan2009,16:08   #2
xpi0t0s's Avatar
Ctrl-C doesn't kill Windows programs. You could try using TaskKill instead http://technet.microsoft.com/en-us/l.../bb491009.aspx
Or if you want to do it programmatically you could try sending WM_CLOSE to the main window, but the behaviour will depend on what the application does with that message.
Newbie Member
7Jan2009,16:18   #3
programmer321's Avatar
Hi thanks for da reply.

But the second application closes gracefully only if it receives SIGINT signal(Generated by cntrol C). Taskkill will forceully kill the application without gracefully closing it.

Please guide something related to this.
Mentor
7Jan2009,17:21   #4
xpi0t0s's Avatar
What if you send the Ctrl-C to the DOS box it's running in rather than sending it to the executable?
Newbie Member
7Jan2009,17:29   #5
programmer321's Avatar
Please guide how to work on that.
Mentor
8Jan2009,04:09   #6
xpi0t0s's Avatar
Tried it; doesn't work. GenerateConsoleCtrlEvent looks like it might be a possibility.