Solved

How to send Ctrl-C event to childprocess

Posted on 2001-09-07
5
1,681 Views
Last Modified: 2011-10-03
I use CreateProcess() to create a new console process.I want to send Ctrl-C event to the child process to stop the child.

I have tried GenerateConsoleCtrlEvent(), but it always returned ERROR_INVALID_HANDLE error.

can anybody give me the right answer.

Thanks.
0
Comment
Question by:walkdan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:bkdc
ID: 6464902
That's not the way to do it. First of all, you should search the database for an answer (I gave one a week ago, I think, in this topic). Sadly, I don't remember which.

Quick and simple method

#define WM_KICK_THE_BUCKET WM_USER+1; //whatever

worker function(your thread)

int workerThread(...)
{
  bool killed=FALSE;
  while(!killed)
  {
    //do your job
    ......
    if(PeekMessage(...))
    {
       GetMessage(...);
       if(msg==WM_KICK_THE_BUCKET)
       {
          killed=true;
       }
     }
   }
}

from the main thread:

PostThreadMessage(yourThreadHandle,WM_KICK_THE_BUCKET);


}
0
 
LVL 2

Expert Comment

by:bkdc
ID: 6464953
Oooops, I think I missed the target...sorry.
Let's fix that.
1. I'd like to see some code....
2. Processes created with DETACHED_PROCESS or CREATE_NEW_CONSOLE are not affected by SIGINT or SIGBREAK.
3. GenerateConsoleCtrlEvent requires a groupId; what did you send ?
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6464976
>>I use CreateProcess() to create a new console process.I
>>want to send Ctrl-C event to the child process
>>to stop the child.

'CreateProcess()' fills in a PROCESS_INFORMATION struct that has a 'hProcess' member. Just use this in a call to 'TerminateProcess()'.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6865863
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jkr" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6886779
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Lambda for random numbers problem 7 120
FMX enumerated colours 2 133
C++ question 3 74
Finding Divisors 5 37
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question