How to send Cntrol C to another running application

Discussion in 'C' started by programmer321, Jan 7, 2009.

  1. programmer321

    programmer321 New Member

    Joined:
    Jan 7, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jan 7, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Ctrl-C doesn't kill Windows programs. You could try using TaskKill instead http://technet.microsoft.com/en-us/library/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.
     
  3. programmer321

    programmer321 New Member

    Joined:
    Jan 7, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What if you send the Ctrl-C to the DOS box it's running in rather than sending it to the executable?
     
  5. programmer321

    programmer321 New Member

    Joined:
    Jan 7, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Please guide how to work on that.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Tried it; doesn't work. GenerateConsoleCtrlEvent looks like it might be a possibility.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice