How to send Cntrol C to another running application

programmer321's Avatar, Join Date: Jan 2009
Newbie Member
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
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
programmer321's Avatar, Join Date: Jan 2009
Newbie Member
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What if you send the Ctrl-C to the DOS box it's running in rather than sending it to the executable?
0
programmer321's Avatar, Join Date: Jan 2009
Newbie Member
Please guide how to work on that.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Tried it; doesn't work. GenerateConsoleCtrlEvent looks like it might be a possibility.