Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages
Started by Richard Kavanagh
Thu, 11 Oct 2007 11:59
Handle from ThreadId, CreateToolhelp32Snapshot, THREADENTRY32
Author: Richard Kavanagh
Date: Thu, 11 Oct 2007 11:59
Date: Thu, 11 Oct 2007 11:59
37 lines
2099 bytes
2099 bytes
One of these days I might understand the Microsoft mind; but not at present.
I use the following function to obtain ThreadId's of all running processes.
I then wish to find out their cpu times using GetThreadTimes. However the
'th32ThreadID' field of the THREADENTRY32 structure is not always the same
32 bit value as the thread's handle, although it seems to be for the a
process's main thread. Is there a way to retrieve the handle from this
structure?
Rivhaed.
// global to function
std::vector<THREADENTRY32> vThread;
bool TProcInfo::GetThreadList()
{
vThread.clear();
HANDLE h = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if(h != INVALID_HANDLE_VALUE)
{
THREADENTRY32 te;
te.dwSize = sizeof(te);
if(::Thread32First(h, &te))
{
do
{
if(te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
sizeof(te.th32OwnerProcessID))
{
vThread.push_back(te);
}
te.dwSize = sizeof(te);
} while(::Thread32Next(h, &te));
}
::CloseHandle(h);
}
return h != INVALID_HANDLE_VALUE;
}
Re: Handle from ThreadId, CreateToolhelp32Snapshot, THREADENTRY32
Author: Remy Lebeau (TeamB)
Date: Thu, 11 Oct 2007 17:34
Date: Thu, 11 Oct 2007 17:34
22 lines
1822 bytes
1822 bytes
"Richard Kavanagh" <dd.foldhill[at]googlemail.com> wrote in message news:470e1044$1@newsgroups.borland.com... > However the 'th32ThreadID' field of the THREADENTRY32 > structure is not always the same 32 bit value as the thread's handle It is not supposed to be. A thread ID is not the same thing as a thread handle. The ID is just a unique number that is assigned to identify the thread. The handle points to the actual thread object itself. > Is there a way to retrieve the handle from this structure? On Windows 2000 and later, you can use OpenThread(). On older versions, there is no direct way to get a thread handle from a thread ID and vice versa. Have a look at the following posting for more information: http://groups.google.com/group/borland.public.delphi.winapi/msg/ed9350f2ffdd3432 Gambit
Re: Handle from ThreadId, CreateToolhelp32Snapshot, THREADENTRY32
Author: Richard Kavanagh
Date: Thu, 11 Oct 2007 18:31
Date: Thu, 11 Oct 2007 18:31
13 lines
1559 bytes
1559 bytes
> On Windows 2000 and later, you can use OpenThread(). On older versions, > there is no direct way to get a thread handle from a thread ID and vice > versa. Have a look at the following posting for more information: > > > http://groups.google.com/group/borland.public.delphi.winapi/msg/ed9350f2ffdd3432 > > Gambit Thank you Remy, OpenThread() is just what I need. Pity that the method shown in the link is so convoluted. Richard
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads