我觉得这个问题有一个明显的答案,但它一直困扰着我。我这里有一些 C++ 遗留代码,当它尝试调用 OpenThread() 时,这些代码会中断。我在 Visual C++ 2008 Express Edition 中运行它。程序首先获取调用线程的ThreadID,并尝试打开它,如下所示:
ThreadId threadId = IsThreaded() ? thread_id::GetCurrentThreadId();
HANDLE threadHandle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadId);
现在我不明白的是:如果线程ID是当前线程的ID,它不是已经打开了吗?这可能就是它返回 NULL 的原因吗?
如有任何反馈,我们将不胜感激。
最佳答案
也许您要求的访问权限过多(THREAD_ALL_ACCESS
),但我认为您对自己的线程拥有几乎所有权限。尝试减少对您真正需要的内容的访问。
GetLastError()
返回什么?
更新:
看看 MSDN 上的这条评论:
Windows Server 2003 and Windows XP/2000: The size of the
THREAD_ALL_ACCESS
flag increased on Windows Server 2008 and Windows Vista. If an application compiled for Windows Server 2008 and Windows Vista is run on Windows Server 2003 or Windows XP/2000, theTHREAD_ALL_ACCESS
flag is too large and the function specifying this flag fails withERROR_ACCESS_DENIED
. To avoid this problem, specify the minimum set of access rights required for the operation. IfTHREAD_ALL_ACCESS
must be used, set_WIN32_WINNT
to the minimum operating system targeted by your application (for example,#define _WIN32_WINNT _WIN32_WINNT_WINXP
). For more information, see Using the Windows Headers
关于c++ - OpenThread() 返回 NULL Win32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1069860/