Does anyone know how I can get the type of an arbitrary HANDLE, i.e. if it's a file, a thread, whatever?

If that's not possible, is there at least some reliable way to distinguish between console screen buffer handles and other handles that allow Read/WriteFile calls?

Basically, I'm trying to find out whether stdout is attached to a console or redirected to a file, so that I set the colour of the text if it's a console.

Under POSIX I'd call ttyname() to get the same effect.