Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the winapi id of a thread that has been created using the standard library?

C++11’s standard library contains <thread> which allows threads to be created. However, the Windows api requires an ID for some functions (PostThreadMessage, namely). How can I get it?

remark: std::thread::get_id() doesn’t seem to work:

PostThreadMessage(m_thread->get_id(), WM_QUIT, 0, 0);

e:\documents\khook\khooker\hook_runner.cpp(129): error C2664: 'PostThreadMessageW' : cannot convert parameter 1 from 'std::thread::id' to 'DWORD'
like image 756
qdii Avatar asked Jun 04 '12 17:06

qdii


People also ask

How do I get my Pthread thread ID?

The pthread_self() function is used to get the ID of the current thread. This function can uniquely identify the existing threads.

What is a thread ID?

Thread Id is a long positive integer that is created when the thread was created. During the entire lifecycle of a thread, the thread ID is unique and remains unchanged. It can be reused when the thread is terminated.

What is the Windows API for creating a thread?

To create a thread, the Windows API supplies the CreateThread( ) function. Each thread has its own stack (see thread vs processes). You can specify the size of the new thread's stack in bytes using the stackSize parameter which is the 2nd argument of CreateThread( ) function in the example below.

What is create thread?

Creates a thread to execute within the virtual address space of the calling process. To create a thread that runs in the virtual address space of another process, use the CreateRemoteThread function.


1 Answers

Use the member function native_handle(). It provides the native thread handle. Then you can call GetThreadId() on it.

like image 164
R. Martinho Fernandes Avatar answered Oct 08 '22 21:10

R. Martinho Fernandes