windows OpenThread() 返回 NULL Win32
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1069860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenThread() Returns NULL Win32
提问by RCC
I feel like there is an obvious answer to this, but it's been eluding me. I've got some legacy code in C++ here that breaks when it tries to call OpenThread(). I'm running it in Visual C++ 2008 Express Edition. The program first gets the ThreadID of the calling thread, and attempts to open it, like so:
我觉得对此有一个明显的答案,但它一直在躲避我。我在 C++ 中有一些遗留代码,当它尝试调用 OpenThread() 时会中断。我在 Visual C++ 2008 Express Edition 中运行它。程序首先获取调用线程的 ThreadID,并尝试打开它,如下所示:
ThreadId threadId = IsThreaded() ? thread_id : ::GetCurrentThreadId();
ThreadId threadId = IsThreaded() ? thread_id : ::GetCurrentThreadId();
HANDLE threadHandle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadId);
HANDLE threadHandle = OpenThread(THREAD_ALL_ACCESS, FALSE, threadId);
Now here's what I don't understand: if the thread ID is the current thread's ID, isn't it already open? Could that be why it's returning NULL?
现在我不明白的是:如果线程 ID 是当前线程的 ID,它不是已经打开了吗?这可能是它返回NULL的原因吗?
Any feedback would be appreciated.
对于任何反馈,我们都表示感谢。
回答by Michael Burr
Maybe you're asking for too much access (THREAD_ALL_ACCESS
), though I'd think that you'd have pretty much all permissions to your own thread. Try reducing the access to what you really need.
也许您要求太多的访问权限 ( THREAD_ALL_ACCESS
),但我认为您对自己的线程几乎拥有所有权限。尝试减少对您真正需要的东西的访问。
What does GetLastError()
return?
什么GetLastError()
回报?
Update:
更新:
Take a look at this comment from MSDN:
看看来自 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
Windows Server 2003 和 Windows XP/2000:
THREAD_ALL_ACCESS
标志的大小 在 Windows Server 2008 和 Windows Vista 上有所增加。如果为 Windows Server 2008 和 Windows Vista 编译的应用程序在 Windows Server 2003 或 Windows XP/2000 上运行,则THREAD_ALL_ACCESS
标志太大并且指定此标志的函数失败并显示ERROR_ACCESS_DENIED
. 为避免此问题,请指定操作所需的最小访问权限集。如果THREAD_ALL_ACCESS
必须使用,请设置_WIN32_WINNT
为您的应用程序所针对的最低操作系统(例如,#define _WIN32_WINNT _WIN32_WINNT_WINXP
)。有关详细信息,请参阅使用 Windows 标头
回答by Viraj
Try using _beginthreadex instead of OpenThread. Example:
尝试使用 _beginthreadex 而不是 OpenThread。例子:
HANDLE hThread;
UINT uiThreadId = 0;
hThread = (HANDLE)_beginthreadex(NULL, // Security attributes
0, // stack
&this->ThreadProc, // Thread proc
this, // Thread param
CREATE_SUSPENDED, // creation mode
&uiThreadId); // Thread ID
if (hThread != NULL){
//SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
ResumeThread(hThread);
m_hThread = hThread;
}
else{
eRetVal = err_ThreadStartErr;
}