?
Solved

How to convert a thread ID to the thread handle?

Posted on 1997-04-26
4
Medium Priority
?
602 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 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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