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

x
?
Solved

How to convert a thread ID to the thread handle?

Posted on 1997-04-26
4
Medium Priority
?
617 Views
Last Modified: 2013-12-04
How can I convert the thread ID to the thread handle which is returned from the API GetWindowThreadProcessId() ??

Thanks for you answer.
0
Comment
Question by:crystal030497
  • 2
4 Comments
 

Author Comment

by:crystal030497
ID: 1396935
Edited text of question
0
 
LVL 1

Accepted Solution

by:
dominic01 earned 100 total points
ID: 1396936
Here is your answer from the online help of MSCV. Sorry for the layout of the thing. Just cut and paste it in Notepad.exe and it'll appear OK.
**********************************
PSS ID Number: Q127992
Article last modified on 05-25-1995
 
4.00    | 3.50 3.51
 
WINDOWS | WINDOWS NT
 

----------------------------------------------------------------------
The information in this article applies to:
 
 - Microsoft Win32 Software Development Kit (SDK), versions 3.5, 3.51,
   and 4.0
----------------------------------------------------------------------
 
The CreateThread() API is used to create threads. The API returns both a
thread handle and a thread identifier (ID). The thread handle has full
access rights to the thread object created. The thread ID uniquely
identifies the thread on the system level while the thread is running. The
ID can be recycled after the thread has been terminated. This relationship
is similar to that of the process handle and the process ID (PID).
 
There is no way to get the thread handle from the thread ID. While there is
an OpenProcess() API that takes a PID and returns the handle to the
process, there is no corresponding OpenThread() that takes a thread ID and
returns a thread handle.
 
The reason that the Win32 API does not make thread handles available this
way is that it can cause damage to an application. The APIs that take a
thread handle allow suspending/resuming threads, adjusting priority of a
thread relative to its process, reading/writing registers, limiting a
thread to a set of processors, terminating a thread, and so forth.
Performing any one of these operations on a thread without the knowledge of
the owning process is dangerous, and may cause the process to fail.
 
If you will need a thread handle, then you need to request it from the
thread creator or the thread itself. Both the creator or the thread will
have a handle to the thread and can give it to you using DuplicateHandle().
This requirement allows both applications to coordinate their actions.
 
NOTE: You can also take full control of the application by calling
DebugActiveProcess(). Debuggers receive the thread handles for a process
when the threads are created. These handles have THREAD_GET_CONTEXT,
THREAD_SET_CONTEXT, and THREAD_SUSPEND_RESUME access to the thread.
 
Additional reference words: 3.50 4.00 95
KBCategory: kbprg
KBSubcategory: BseProcThrd
=============================================================================
 
Copyright Microsoft Corporation 1995.

*********************

Hope this helped.

DOM



0
 

Author Comment

by:crystal030497
ID: 1396937
Thank you for your answer.
It would be helpful to me.

0
 
LVL 1

Expert Comment

by:ete
ID: 1396938
Even though Microsoft claims, that getting valid thread handle is not possible, even if you know the id, is not actually true.

It can be accomplished with the standard Win32 API mechanisms. The next beta release of DirectIPC, due the beginning of July, implements it in a very robust way.

DirectIPC for Windows NT and Windows 95 (downloadable in http://www.winsite.com on Windows NTprogramming utilities) is a high level API for Process Control and Inter Process Communication, which already implements many other advanced features, which should have been part of Microsoft Win32 API.

Best regards,
ETE
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…

885 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