Thread: ignored return values

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    ignored return values

    hello all,
    I have a question about a function return value and a call to GetLastError(). I am posting this question because I am unsure of what happens in Windows to called functions that return other than void that have their return values ignored (what actually happens to the returned values?).

    say for example SomeAPIFunc() is some Win32 API function that looks like this:

    Code:
    int SomeAPIFunc(void);
    say I call this function, but ignore its return value:

    Code:
    SomeAPIFunc();
    then I go to use GetLastError() to check if this function returned some error. will the call the GetLastError() still return the error code from this function, even though I am ignoring its return value?

    any help is greatly appreciated, thank you in advance!

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    GetLastError() will return its own error code. No, you aren't forced to use return values of all the functions but it is highly suggested that you check the return values of functions that are likely to fail (i.e. binding a socket, opening a file..).

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Most of the Win32 API returns TRUE/FALSE answers. . . it'd be just as easy to say:

    Code:
    if (!SomeAPIFunc()){
            myerrorvar = GetLastError();
    }
    Andy

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868

    Wink

    GetLastError() contains the last value from a SetLastError() call. This is usually (or should) be called inside the API functions.

    That is, if the function did not produce an error then it SHOULD call SetLastError() with ERROR_SUCCESS.

    What if this does not happen?
    Say this happens instead;

    function1() fails and calls SetLastError(SOME_ERROR);

    GetLastError() == SOME_ERROR

    function2() succeeds and DOES NOT call SetLastError(ERROR_SUCCESS);

    GetLastError() == SOME_ERROR

    This will not only give you the wrong error but an error where there was none....


    You have to ask yourself if you trust MS enough NOT to check the return.......
    "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

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    that is partly why I am asking this question novacain.

    if I ignore the return value of the function, and the error gets ignored (my original question), and then I use GetLastError() to try and see if it returned error, I could get an error code, but from a previous error that occured, not the error code I am looking for.

    with the way that you described GetLastError() it looks like I don't have to store the return value of the function, if it will call SetLastError() (this would make sense and I am sure of this since the msdn description of the function mentions GetLastError() and specific error codes).

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    GetLastError(), as its name states it, returns only the error code of the last error. Therefore, if for example you forgot to specify a class name in your WNDCLASSEX structure and called RegisterClassEx(), it would fail. If you don't check your error code and proceed by creating your window using CreateWindow(), you will get only the error code for CreateWindow(). Even though the error could be meaningful in that case, sometimes it could cause some serious and hard-to-spot errors that could bug you down quite a lot.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    it looks like I don't have to store the return value of the function,
    No need to store the return only check calls that have a high possibility of failure (or fail when debugging).

    Check some API calls with the code Kennedy supplied.

    Code:
    if (!SomeAPIFunc())
    {
            myerrorvar = GetLastError();
    }
    Check each function on MSDN to see what it returns on an error, as it varies (some return 0 for error, some return 0 for success).

    if I ignore the return value of the function, and the error gets ignored (my original question), and then I use GetLastError() to try and see if it returned error, I could get an error code, but from a previous error that occured, not the error code I am looking for.
    Is this what you mean?


    function1() fails and calls SetLastError(SOME_ERROR);

    //missed that the funtion failed

    function2() succeeds and calls SetLastError(ERROR_SUCCESS);

    //lost the actual error
    GetLastError() == ERROR_SUCCESS
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM