Solved

How to send Ctrl-C event to childprocess

Posted on 2001-09-07
5
1,657 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
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now