Solved

How to convert a thread ID to the thread handle?

Posted on 1997-04-26
4
569 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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