get process handle from its id

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?
yan051197Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
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
yan051197Author Commented:
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
chensuCommented:
ProcessViewer (PView) comes with source code in Win32 SDK samples (SDK Tool samples\SDK Windows NT Samples).
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

alexoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
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
alexoCommented:
Hey, that's MS for you!

0
Prasad033198Commented:
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
alexoCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.