Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to find the current process handle?

Posted on 1998-04-15
17
Medium Priority
?
431 Views
Last Modified: 2013-12-03
THe Win32API function GetCurrentProcess does not return a real handle to the current process (and the pseudo handle returned always îs -1 !!).

How is it possible to find out this current process handle? I want to give it to anothe process who should take control of the first one.
0
Comment
Question by:poupou
[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
17 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1412776
Try the OpenProcess(0 windows API procedure.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412777
That is supposed to be OpenProcess().

The OpenProcess() procedure allows you to specify which operations can be performed with the handle.  This allows you to limit the other process from taking actions with your proccess that you don't want.
0
 

Author Comment

by:poupou
ID: 1412778
the openprocess returns a handle that seems to be not valid for other processes.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 22

Expert Comment

by:nietod
ID: 1412779
No it is valid, it is not a pseudo handle.  What are your trying to do with it?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412780
You might need to copy the handle for the target process using DuplicateHandle().  but I don't think that is necessary.
0
 

Author Comment

by:poupou
ID: 1412781
OK, ProcessA existe already, and I want to take controle on it from ProcessB (which would Terminate ProcessA).

I need to have a valid handle to ProcessA in ProcessB to perform that. But OpenProcess(PROCESS_ALL_ACCESS,false,id of ProcessA) returns a handle that cannot be used in TerminateProcess (Access is denied).
0
 

Author Comment

by:poupou
ID: 1412782
DuplicateHandle(). That is perhaps the solution. I need to try it. But ProcessA does not know about ProcessB, so it cannot duplicate a handle to it.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412783
That is correct.  That is a security mechanism.  One proccess cannot get a handle to another process that could be used to terminate the process or do other dangerious things to the process.  

However, a process can create for itself handle that can be used to do "dangerious" things, like terminate.  It can then give this handle to other processes.  This way a process can control what other processes can do to it.  

Process A must create the handle with OpenProcess() and give it to process B.  Then process B can use it.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412784
The "that is correct" refers to the comment where you described the behavior.  Not the DuplicateHandle() comment.  (It wasn't there when I read in the question.)

If process A does not know about process B, you are in trouble.  Process B cannot go around terminating processes without their permission.  That is a security violation.  That is dangerious.  

What exacly are you trying to do?
0
 

Author Comment

by:poupou
ID: 1412785
OK, what I try to do:

ProcessB is a GUI that controls the behaviour of ProcessA. If ProcessB (the GUI) wants to close ProcessA, B send to A a close request. Then B WaitForSingleObject(hProcessA). But this wait is never successfull, because hProcessA is not valid in ProcessB.

And I still do not succeed to pass from ProcessA a valid handle to ProcessB.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412786
Did you write both process A and B?
Are they specific processes or could one of them be any running process?
How do the processes "know" about each other?
Does one process start the other?  (Which starts which?)
How are your trying to pass a handle from A to B?  How is the handle created?
0
 
LVL 1

Expert Comment

by:iron_fist
ID: 1412787
Process B can do a lot more to process A if it owns process A. If B call CreateProcess to start A. B can call TerminateProcess to kill A with the handle returned by CreateProcess.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1412788
That's why I asked if one of the processes starts the other.

The other thing is that terminating a process is not a great thing to do.  It is better to get the process to terminate itself.  (That is why I asked some of those questions.)
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 900 total points
ID: 1412789
One application by default cannot do anything to another under Windows NT. However, it is possible under Windows 95.

Under Windows NT, if the user does not have the necessary access privilege, then this will not work. But the good thing is that a process under Windows NT can give itself some addition privileges. One such privilege is SE_DEBUG_NAME. Your process will need to give itself that privilege by using AdjustTokenPrivileges(). Then your TerminateProcess(handle, 1) will work.

This is all documented in a Win32 SDK sample called tlist under \mssdk\samples\sdktools\tlist. It describes how to retrieve the process handle of another application along with how you can terminate it.

You can harshly kill an application by using TerminateProcess. Another way would be to walk the desktop windows' children windows, which would be the top level windows. Then find the first top level window whose process id matches the id you're interested in and then posting it the WM_CLOSE message. If there isn't such a window, then it may be a service or a console application in which case TerminateProcess will work.
0
 
LVL 3

Expert Comment

by:byang
ID: 1412790
If B creates A using CreateProcess(), the handle return in PROCESS_INFORMATION structure call be used to wait for A to finish.
If not, you should call DuplicateHandle() from process A and return the duplicated handle to B.

If you're writing both A and B, and B does not start A, the easiest method is to use a mutex or a semaphore. A should grab it upon startup and and then release it upon exit. B can try to gain access or wait for it, using its globally unique name.

0
 

Author Comment

by:poupou
ID: 1412791
thui,


great!!


I tried the ms sample from tlist, and now I have the privilege to terminate processA.
I just need now to disable the SE_DEBUG_NAME privilege to be sure my problem really was due to privileges. (The examples given in the msdev help and example files do not work to disable the privileges.)


thanks a lot for your help, and also for the other experts.
0
 

Expert Comment

by:darnell_caballes
ID: 4315813
How did you get around the original problem of GetCurrentProcess always returning -1?

-d
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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