c++ - OpenThread() 返回 NULL Win32

标签 c++ windows multithreading winapi visual-c++

我觉得这个问题有一个明显的答案,但它一直困扰着我。我这里有一些 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, the THREAD_ALL_ACCESS flag is too large and the function specifying this flag fails with ERROR_ACCESS_DENIED. To avoid this problem, specify the minimum set of access rights required for the operation. If THREAD_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/

相关文章:

windows - Gitlab私有(private)项目接受任何人的推送

multithreading - 从 Perl 线程运行 bash 脚本

c++ - std 容器是否有插入任何容器的通用方法?

c++ - 仅从 std::cin 获取第一个值

c++ - R^3(x,y,z)中快速随机访问和顺序访问的数据结构

c++ - 循环具有相似名称的对象

mysql - UTF8 在翻译中丢失,HTML 格式从/到 mysql

c++ - Dll 函数调用比普通函数调用更快?

java - 定时器错误 java.lang.IllegalStateException

java - 在 JPanel 中显示正确的信息