读书人

已知线程句柄怎么得到Id

发布时间: 2012-01-28 22:06:13 作者: rapoo

已知线程句柄,如何得到Id
已知线程句柄,如何得到Id?微软也真是麻烦,搞两个都快晕菜了。
虽然ID全局唯一,句柄也可以全局唯一,搞一个不就行了,这么麻烦。。

[解决办法]
在你创建线程的时候,就已经得到线程ID了。
如果线程是用 AfxBeginThread 创建的, 返回的是 CWinThread 指针,其成员变量m_nThreadID就是线程ID
如果用API, CreateThread, 其最后一个参数就是返回 ThreadID 的
[解决办法]
或者:

The GetThreadId function retrieves the thread identifier of the specified thread.

DWORD GetThreadId(
HANDLE Thread
);


[解决办法]
又坑人了, 抱歉呀LZ, 那个是Vista下的.

AfxBeginThread创建后返回的CWinThread对象中, m_nThreadID成员就是Thread ID
[解决办法]
句柄和ID是功能不同的2个东东,另外,ID是全局唯一的,而句柄不是。微软这样设计是正确的。

以下是msdn:注意,Server: Requires Windows Server 2003.一句

GetThreadId
The GetThreadId function retrieves the thread identifier of the specified thread.

DWORD GetThreadId(
HANDLE Thread
);

Parameters
Thread
[in] Handle to the thread. The handle must have the THREAD_QUERY_INFORMATION access right.
Return Values
If the function succeeds, the return value is the thread identifier of the specified thread.


If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
Until a thread terminates, its thread identifier uniquely identifies it on the system. For more information about access rights, see Thread Security and Access Rights.

Requirements
Server: Requires Windows Server 2003.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.



[解决办法]
有一个函数只能得到本线程的ID
GetCurrentThreadID
[解决办法]
在baidu上搜索 句柄表 应该有lz要的东西

读书人网 >VC/MFC

热点推荐