Solved

How to convert a thread ID to the thread handle?

Posted on 1997-04-26
4
555 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 50 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

12 Experts available now in Live!

Get 1:1 Help Now