?
Solved

Converting a thread handle to a thread ID and vice versa !

Posted on 2003-03-28
6
Medium Priority
?
683 Views
Last Modified: 2008-02-01
How can I get the thread ID corresponding to a thread handle ?

How can I get the thread handle corresponding to a thread ID ?

A function exist 'void GetThreadID(HANDLE)', but works only under .NET

What's about VC6.0 !
0
Comment
Question by:mike_marquet
[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
  • 3
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
JMoon5FTM earned 750 total points
ID: 8225036
To convert a thread id into a thread handle, you can use the OpenThread function, which has this prototype:

HANDLE OpenThread(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId);

However, this function requires Win ME/2000/XP.

If the thread you're interested in is the current one, you can use GetCurrentThread to retrieve a handle, or GetCurrentThreadId to retrive the thread id.  Both work in all Win32 OSes.  The handle retrieved by GetCurrentThread should be duplicated with DuplicateHandle before it is communicated to another process.

If you need the thread id for an open handle, the only thing I can think to do would be to call EnumThreadWindows to find a window associated with the thread, and then call GetWindowThreadProcessId to retrieve the thread id.

My documentation for GetThreadId indicates that it is a system function available to all languages - under Windows Server 2003.  It's not available in any other OS.

This is definitely one area where the Win32 API is lacking.
0
 

Author Comment

by:mike_marquet
ID: 8243871
I will try to use EnumThreadWindows because I need it under Windows NT4
0
 

Author Comment

by:mike_marquet
ID: 8244019
What's about a thread that is not associated with a window (create using CreateThread function) !
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 5

Expert Comment

by:JMoon5FTM
ID: 8270816
If you're desperate, I suppose you can inject code into the thread to force it to create a window (provided you have the permissions to do this).  There ought to be a better way, and I'll look around, but I really don't think there is.
0
 

Expert Comment

by:CleanupPing
ID: 9446698
mike_marquet:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:JMoon5FTM
ID: 9463581
If mike_marquet had to retrieve the id of a thread that didn't create any windows, he would have had to inject code and do other such nasty things that I didn't describe, and thus I don't suppose I'd quite deserve an A.  However, I do believe that I've answered all of his questions about converting between thread ids and handles.  If this is not the case, he should post what issues he still has.  Otherwise, I think my comment should be accepted as the answer.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

800 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