Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

get process handle from its id

Posted on 1997-11-26
8
Medium Priority
?
379 Views
Last Modified: 2013-12-03
I want to use SetPriorityClass() to increase the priority
of another process, but only pid is known (the pid listed
in TaksManager of NT 4), while SetPriorityClass() need
hHandle. How can I get the process handle from process id?
0
Comment
Question by:yan051197
8 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1408896
This is copied from the SDK documentation:

The Win32 API does not provide a way to get the thread handle from the thread identifier. If the handles were made available this way, the owning process could fail because another process unexpectedly performed an operation on one of its threads, such as suspending it, resuming it, adjusting its priority, or terminating it. Instead, you must request the handle from the thread creator or the thread itself.

If the process you are affecting is something you've written, then you can do it uses messages (or some other technique) to transfer the handle and do it.  I can explain more if you need.  But, in general (with processes that you did not write), you cannot do this because there is no way to get the handle.
0
 

Author Comment

by:yan051197
ID: 1408897
Thanks nietod.
But my situation is different.
I can not use SendMessage() or similar method to pass handle
because:
   1) the destination process is not Win32 based, it is ported from UNIX. so it can not accept any Windows message.
   2) the handle is process-related. Even for a same object,
the value of handle is different from the value of handle in
another process. (Advanced Windows, by Jeffrey Richter, ch.2)
So even I can get the handle from the thread/process creator,
the handle is useless in my process. I must get a handle from
OS and which indicate same object as in in the creator process.

I think there must have some method to get handle from id.
For example, ProcessViewer (PView) can set priority of other
process. And SetPriorityClass() needs one handle, it means
if we can get the handle, and we have premission to do with
the destination process, then we can set that process.

In my situation, the destination process is monitoring RS-232
port, so I need to increase its priority.

Thanks again, waiting for more help infomation.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1408898
ProcessViewer (PView) comes with source code in Win32 SDK samples (SDK Tool samples\SDK Windows NT Samples).
0
 [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

 
LVL 11

Accepted Solution

by:
alexo earned 200 total points
ID: 1408899
The OpenProcess() function returns a handle of an existing process object.

HANDLE OpenProcess(
    DWORD dwDesiredAccess,      // access flag
    BOOL  bInheritHandle,      // handle inheritance flag
    DWORD dwProcessId);       // process identifier


0
 
LVL 22

Expert Comment

by:nietod
ID: 1408900
I needed that months ago and found out from the SDK documentation that it could not be done, so I gave up and redesigned my code.  They have a procedure for something that can't be done?!
0
 
LVL 11

Expert Comment

by:alexo
ID: 1408901
Hey, that's MS for you!

0
 

Expert Comment

by:Prasad033198
ID: 1408902
Hey -
For getting the Process handle, you can use
the function OpenProcess() as already mentioned -
however, I am stup up with in a further step of trying to
get the ProcessID for any process in the system -
Please update me - whether any API are present for that??
I know that this information can be accessible through
the Registry - but I want through the Win32 API
0
 
LVL 11

Expert Comment

by:alexo
ID: 1408903
Well, MS hits again...
For Win95/98 and NT 5+ you have the toolhelp APIs.
For NT 3.5x/4 you should search the registry.

Search for "Taking a Snapshot and Viewing Processes" in your MSVC documentation.

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

972 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