Eric Schmidt
Eric Schmidt

Reputation: 1317

CreateThread() // GetLastError() returns 87

Below is a simple program that creates a thread. I hope I am missing something obvious. FYI - I am a newb to Win32.

Why does GetLastError() in the thread execution return 87 (ERROR_INVALID_PARAMETER) ?

#include "stdafx.h"
#include <Windows.h>
#include <iostream>

long WINAPI Run();

int _tmain(int argc, _TCHAR* argv[])
{
  DWORD id;

  HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Run, NULL, 0, &id);    

  // hThread is not NULL  

  DWORD err = GetLastError(); // returns 0

  int input;
  std::cin  >> input;

  return 0;
}

long WINAPI Run() 
{
    DWORD err = GetLastError(); // returns 87
    return 0;
}

Upvotes: 1

Views: 5319

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 992965

The GetLastError() function only returns valid results when it is called immediately after a Win32 API function that returns a "failed" result.

In your case, you are calling GetLastError() as the first thing in a new thread, which is definitely not immediately after a Win32 API function call.

Also, each thread has its own copy of a "last error" so they don't step on each other.

Upvotes: 4

Related Questions