Solved

Using GetLastError()

Posted on 1997-05-06
2
1,695 Views
Last Modified: 2013-12-04
Many times I have called getLastError() after a Win32 function call failed, but the returned error code was obscure and not satisfactory documented.

For example, I am calling hWnd=CreateWindow(...) and get hWnd==NULL. I call GetLastError() and get 6. In WINERROR.H, error 6 is 'ERROR_INVALID_HANDLE' with no good explanation. The VC++ help and MSDN don't explain what does
this error mean in CreateWindow().

My question: Is there a place or book where all the Win32 errors are fully documented? E.g., where I can find what is the exact meaning of ERROR_INVALID_HANDLE for CreateWindow()? (I need a better  explanation than the laconic text "The handle is invalid").

0
Comment
Question by:Sruli
2 Comments
 
LVL 1

Accepted Solution

by:
shappir earned 60 total points
ID: 1397154
I am not familiar with any book that is specifically devoted to win32 error values. The best sources for this sort of information that I am aware of are the MSDN CD and the Microsoft Knowledge Base: http://www.microsoft.com/kb/ 
You can use the FormatMessage Win32 API function to retrieve a descriptive test string for an error. This will at least save you the hassle of search through WINERROR.H :
  TCHAR szMessage[MAX_PATH];
  FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL, GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    szMessage, MAX_PATH, NULL);
szMessage will now contain the error text.
Finally, in my opinion with a little thought and experimentation you can figure out the meaning of the error values. For example, ERROR_INVALID_HANDLE seems to indicate that one of the handle values you are passing to CreateWindow is invalid. This could be the handle of the parent window, the menu handle or the instance handle. You can use the assert macro to check these values before passing them to CreateWindow. You can check the window handle, for example, by verifying that it is NULL or that IsWindow returns TRUE.

0
 

Author Comment

by:Sruli
ID: 1397155
I should have mentioned that I checked all the function parameters and searched the MSDN and KB before asking the question.

Well, finally (after a lot of work) I found  why CreateWindow() failed:  it was because the relevant window procedure returned TRUE to WM_CREATE instead of FALSE.  However, I still don't understand why GetLastError() returned  either ERROR_INVALID_HANDLE or ERROR_FILE_NOT_FOUND, and how I can use a specific error code to solve a particular problem (I would even say that in this case, the error codes were misleading!).

Although your answer didn't help me to solve the problem, it did give me some hints I will use in the future.

Toda Raba,

Sruli Ganor
Mobix Communications Ltd.
Israel
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question