?
Solved

Using GetLastError()

Posted on 1997-05-06
2
Medium Priority
?
1,770 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 120 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

862 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