MSDN for GetExitCodeThread() says "The handle must have the THREAD_QUERY_INFORMATION access right." So it could be a permissions issue.
Main Topics
Browse All Topics// Thread function
extern uint ThreadTest(LPVOID pParam)
{
uint uExitCode = 2;
return uExitCode;
}
// Main function
CWinThread *pThread = AfxBeginThread(ThreadTest,
if(pThread)
{
// Wait for thread to finish
WaitForSingleObject(pThrea
DWORD threadVal=5;
CString tempStr;
// Show result
BOOL retVal=GetExitCodeThread(p
tempStr.Format("ThreadVal is %u",threadVal);
Diag(tempStr); // This prints the string to a text window
}
-----
I am trying to get a return value from a thread. Using the code above, the return value is always '5' which is the value I initialize it to. I expect the result to be '2' as per the Thread function above. What am I doing wrong?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
GetLastError returns '6'. I looked but couldn't seem to find the text message for this.
I tried using (LPSECURITY_ATTRIBUTES)THR
>>>> CWinThread *pThread = AfxBeginThread
I assume the handle is no longer valid as you created the thread by call AfxBeginThread ... That is a wrapper that most likely freed the handle in pThread->m_handle somehow (e. g. by a hook), so that it was invalid after returning. If I am right, you may have an error at WaitForSingleObject as well.
You could come out of this by calling CreateThread instead of AfxBeginThread.
Regards, Alex
I got it working... had to change the 'AutoDelete' param to false to be able to access the exit value after the thread dies. I think the only downfall is I have to manually delete the thread object when I'm done with it (fine with me).
So the thread setup changes to:
// Main function
CWinThread *pThread = AfxBeginThread(ThreadTest,
if(pThread)
{
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread();
...
There should be a way to set the AutoDelete when you create the thread... not sure about that one.
Thanx for the help
Paul
Business Accounts
Answer for Membership
by: itsmeandnobodyelsePosted on 2007-09-26 at 13:29:56ID: 19966505
You have to check the retval. I assume it is false. If so, the GetLastError() should give evidence what was wrong.
Regards, Alex