HANDLE type

This is a discussion on HANDLE type within the Windows Programming forums, part of the Platform Specific Boards category; Does anyone know how I can get the type of an arbitrary HANDLE, i.e. if it's a file, a thread, ...

  1. #1
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893

    HANDLE type

    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    MSCRT has _isatty(), which basically uses GetFileType().

    I've also heard of folks calling GetConsoleMode() and "knowing something" if it fails - but I think GetFileType() is enough in this case.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Maybe this Sysinternals link can be of some help. About halfway down the first page of the thread is a HandlesList.zip file written by Napalm which may meet your requirements.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The _isatty implementation in my CRT doesn't use GetFileType(), but I used it. Thanks a lot. I spent more than an hour on MSDN searching for this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    Yeah, mine didn't use it directly either - I tracked it down to _open_osfhandle() in osfinfo.c (old VS 6.0 CRT source)

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21