Solved

api windows + shutdown a process with his handle

Posted on 2000-02-17
10
225 Views
Last Modified: 2011-04-14
Hello,

I have two executables and I want they comunicate themselves with messages.

When I launch the first program, I have a timer witch list all the Windows and I can get their handles. Then, I send every 5 seconds a lots of messages until the second program answer one of them.

When I launch the second program, if it recieves a message, it sends his handle to the first program as a parameter.

In my first program, when I want to terminate the process of the second program with his handle, it doesn't work.

Can you tell me what should I do to terminate the second program with his handle. Is there a API Windows' function as the same as TerminateProcess but with the handle of a Window and not with the handle of a process.

thanks!
0
Comment
Question by:ryuken
[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
  • 3
  • 2
10 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2531107
Why don't you send a registered message to the other program as a signal to it that it should terminate?  (I suppose you could just send it a WM_QUIT.)  That would be the best bet.  i.e. to signal the other program termiante volentarily.  If you force it to terminate, you can cause problems.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2531115
>>it sends his handle to the first program as a parameter.

Why don't you use the process ID instead? This way, you'll be able to
                HANDLE hProcess;
            // open process
            hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, pid);
            if ( isBadHandle( hProcess ) )
                  printf( "OpenProcess() failed, err = %lu\n", GetLastError() );
            else
            {
                  // kill process
                  if ( ! TerminateProcess( hProcess, (DWORD) -1 ) )
                        printf( "TerminateProcess() failed, err = %lu\n", GetLastError() );
                  else
                        puts( "killed." );

                  // close handle
                  CloseHandle( hProcess );
            }

0
 
LVL 22

Expert Comment

by:nietod
ID: 2531119
Anyways, another option--a less good one--is to use TerminateProcess() to terminate the other processes.  But this is not the best way as it may kill the process at any time, so the process doesn't have an opportunity to do its "clean up", if there is any to be done.
0
Independent Software Vendors: 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!

 
LVL 22

Expert Comment

by:nietod
ID: 2531122
Or as JKR said....
0
 
LVL 22

Expert Comment

by:nietod
ID: 2531138
>> Why don't you use the process ID instead
Or, given the window handle he can use GetWindowThreadProcessID() to get the process ID, but I still wouldn't recommend this route, then open the process handle.  (or the other process could pass a process handle, then you know it would have sufficient access rights).
0
 
LVL 86

Expert Comment

by:jkr
ID: 2531144
>>or the other process could pass a process handle

Difficult - the handle has to marked as 'inheritable' and duplicated (as you only receive 'pseudohandles' via 'GetCurrentProcess()'). I personally prefer to use the PID...
0
 

Author Comment

by:ryuken
ID: 2531252
thanks nietod for your help and also jkr.

I have tried the solution of nietod with the WM_QUIT message.

When the second program recieve this message, I have call the function Close(). But it doesn't word as well because I always see it with the task manager (Ctlr + Alt + Suppr).

I have a solution when I call the function Application->Terminate(). And it works as well and it doesn't remain in the task manager.

thanks!!!
0
 

Author Comment

by:ryuken
ID: 2531255
thanks nietod for your help and also jkr.

I have tried the solution of nietod with the WM_QUIT message.

When the second program recieve this message, I have call the function Close(). But it doesn't word as well because I always see it with the task manager (Ctlr + Alt + Suppr).

I have a solution when I call the function Application->Terminate(). And it works as well and it doesn't remain in the task manager.

thanks!!!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2531346
>>I personally prefer to use the PID...
I don't deal with security issues often (ever?) but I would think tye problem with the PID is you might not be able to open a process handle with the rights to terminate the process, but the process can always pass a handle that has this right.  true?

>> I have call the function Close().
What close?  what are you closing?

>> it doesn't word as well
Does message loop end?
0
 
LVL 86

Expert Comment

by:jkr
ID: 2532372
>>but I would think tye problem with the PID is you might
>>not be able to open a process handle with the rights to
>>terminate the process

Well, if you created the process, it uses the same credentials as the parent, so that hardly anything can go wrong ;-)
0

Featured Post

Industry Leaders: 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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

735 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