Solved

Using GetLastError()

Posted on 1997-05-06
2
1,687 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now