Solved

get process handle from its id

Posted on 1997-11-26
8
354 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use Dotfuscator CE with Visual Studio 2010 Installer? 4 358
Slow process to read Excel 15 148
Windows ICD FFU Issue 4 90
Shared files and folders migration 2 27
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

749 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