Solved

get process handle from its id

Posted on 1997-11-26
8
364 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
[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
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 11

Accepted Solution

by:
alexo earned 50 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

 [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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

623 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