Jump to content
  • Advertisement
Sign in to follow this  
SuperT

Quick GetLastError ( ) question

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Using C++ Win32API How come GetLastError() returns 6 ("The handle is invalid") straight after WinMain() is called? And should I care about win32 error codes as long as a function succeeds?
 
#include <windows.h>


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, TCHAR* szCmdLine, int iCmdShow)
{
UINT iErrorCode = GetLastError ();
TCHAR szErrorText[24];
wsprintf (szErrorText, TEXT("Error code = %u"), iErrorCode);
MessageBox (NULL, szErrorText, NULL, MB_ICONERROR);

return 0;	
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by SuperT
Using C++ Win32API

How come GetLastError() returns 6 ("The handle is invalid") straight after WinMain() is called?

Don't know, but probably has to do with the fact that you haven't called any API functions, so it hasn't been set (or reset).
Quote:
And should I care about win32 error codes as long as a function succeeds?

No. If a function succeeds, then GetLastError will return 0 (no error). The only wierd cases are for things like file i/o. Sometimes, the function may indicate an error, but no real error has occured. This might be the case for reaching end-of-file. The only way to know if you have an error or just a normal end-of-file may be to check GetLastError.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
Quote:
Original post by SuperT
Using C++ Win32API

How come GetLastError() returns 6 ("The handle is invalid") straight after WinMain() is called?

Don't know, but probably has to do with the fact that you haven't called any API functions, so it hasn't been set (or reset).


Thanks for the reply. Just wanted to make sure I could safely ignore it.



Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
No. If a function succeeds, then GetLastError will return 0 (no error).

Nope. If a function returns success then GetLastError is undefined unless the docs specifically say otherwise. In the vast majority of cases it will simply not change whatever the last error was previously. In other cases it may set it to 0 or some other value but this is not guaranteed. Also, not all Win32 API's return errors via GetLastError.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
If a function returns success then GetLastError is undefined unless the docs specifically say otherwise.


True. I was speaking from memory and should have said so. The GetLastError page is quite clear about that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!