Thread: GetStdHandle() returns INVALID_HANDLE_VALUE

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001

    GetStdHandle() returns INVALID_HANDLE_VALUE

    I am really wigging out bro. I have been having weird problem after odd problem with this app I'm working on.
    Okay, I was working on this code on my pc at home (running xp pro) with no worries. Now here at work (running 98se), I run it and am having my mind boggled. Check it:
    hSaveStdin = GetStdHandle(STD_INPUT_HANDLE);
    if (hSaveStdin == INVALID_HANDLE_VALUE) {
      AfxMessageBox("hSaveStdin == INVALID_HANDLE_VALUE");
      CString s; 
      s.Format("Called GetStdHandle(STD_INPUT_HANDLE) - FAILURE# %d", GetLastError());
    The problem: GetStdHandle() with STD_INPUT_HANDLE or STD_OUTPUT_HANDLE returns INVALID_HANDLE_VALUE, but when I check the error code (with GetLastError()), no error code is denoted (zero is returned). Again, I didn't have this problem on my other box... Do you know what might be causing GetStdHandle() to fail (or actually, not fail, but return the aforementioned invalid value)?

    In case you're wondering, I am using this to redirect the output of a console application (invoked with CreateProcess()) to file and to a control, but none of that happens until much later than these calls.

  2. #2
    train spotter
    Join Date
    Aug 2001
    near a computer
    Careful that some other function is not clearing the last error output ie the messagebox.

    Try calling GetLastError as soon as the error occurs, not after the messagebox.

    "You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data. That is because some functions call SetLastError with a zero when they succeed, wiping out the error code set by the most recently failed function." MSDN
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Something else to be aware of is GetLastError() is not fully implemented in 9x cored systems. A large number of functions in 9x, are still basically 16 bit, bodges. Many of these return zero, a few return rubbish.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    if (hSaveStdout == INVALID_HANDLE_VALUE) {
      DWORD err = GetLastError();
      CString s; s.Format("Called GetStdHandle(STD_OUTPUT_HANDLE) - FAILURE# %d", err);
    Thanks for the tip. Here's what I changed it to, but still the last error is zero. Looks like adrianxw's comment may be the reason.

    Dammit... Owell, I just decided to stop exitting the function upon the GetStdHandle() failure as it doesn't impede my function.

    Thanks to both of you for the assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM