[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 684
  • Last Modified:

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

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
mike_marquet
Asked:
mike_marquet
  • 3
  • 2
1 Solution
 
JMoon5FTMCommented:
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
 
mike_marquetAuthor Commented:
I will try to use EnumThreadWindows because I need it under Windows NT4
0
 
mike_marquetAuthor Commented:
What's about a thread that is not associated with a window (create using CreateThread function) !
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JMoon5FTMCommented:
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
 
CleanupPingCommented:
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
 
JMoon5FTMCommented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now