Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to send Cntrol C to another running application (http://www.go4expert.com/forums/send-cntrol-c-running-application-t15748/)

programmer321 7Jan2009 10:48

How to send Cntrol C to another running application
 
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.

xpi0t0s 7Jan2009 16:08

Re: How to send Cntrol C to another running application
 
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.

programmer321 7Jan2009 16:18

Re: How to send Cntrol C to another running application
 
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.

xpi0t0s 7Jan2009 17:21

Re: How to send Cntrol C to another running application
 
What if you send the Ctrl-C to the DOS box it's running in rather than sending it to the executable?

programmer321 7Jan2009 17:29

Re: How to send Cntrol C to another running application
 
Please guide how to work on that.

xpi0t0s 8Jan2009 04:09

Re: How to send Cntrol C to another running application
 
Tried it; doesn't work. GenerateConsoleCtrlEvent looks like it might be a possibility.


All times are GMT +5.5. The time now is 01:45.