Click to See Complete Forum and Search --> : GetLastError() returns ERROR_INVALID_HANDLE


masg
July 20th, 2005, 02:40 AM
Hi,
GetLastError() returns ERROR_INVALID_HANDLE.

I create a thread, join the thread using waitforsingleobject.
Then, i have used GetExitCodeThread call to get the value.
GetExitCodeThread returns 0, which indicates this call is failed.
So, i used GetLastError call and this returns ERROR_INVALID_HANDLE.
I am not able to find what's causing this. Can anybody help in finding solution to this.
code:

h1 = CreateThread( NULL, l_dwStackSize, l_lpStAddr, l_lpParam, l_dwCreFlags, NULL ) ;
l_ret = WaitForSingleObject( h1, INFINITE);
nreturn += GetExitCodeThread(h1,lpExit);
nreturn = GetLastError();

Thanks in advance.
masg

MrViggy
July 20th, 2005, 11:04 AM
Well, what flags are you passing into CreateThread? According to the MSDN:
BOOL GetExitCodeThread( HANDLE hThread, LPDWORD lpExitCode);

hThread
[in] Handle to the thread.
The handle must have the THREAD_QUERY_INFORMATION access right. For more information, see Thread Security and Access Rights.
See GetExitCodeThread (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getexitcodethread.asp).

Viggy

MikeAThon
July 20th, 2005, 11:13 AM
h1 = CreateThread( NULL, l_dwStackSize, l_lpStAddr, l_lpParam, l_dwCreFlags, NULL ) ;
l_ret = WaitForSingleObject( h1, INFINITE);
nreturn += GetExitCodeThread(h1,lpExit);
nreturn = GetLastError();

It seems pointless to create a new thread and then wait forever for it to complete its work. If that's your achitecture, why not just perform the work in the current thread?

In other words, maybe you do not need the thread at all.

Mike