Solved

get process handle from its id

Posted on 1997-11-26
8
332 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio/Web Essentials Won't install 1 50
Windows 10 Scheduled Tasks 11 63
Windows Installer 4 60
Need to create an object factory 2 41
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now