Solved

How to convert a thread ID to the thread handle?

Posted on 1997-04-26
4
590 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
[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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Enable Clear Text in Win 8.1 7 49
VS2015 compilation and missing DLLs 9 190
Best way to accomplish this task? 3 96
Convert GUI app into console app for Win32 Env 5 134
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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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