Solved

Using GetLastError()

Posted on 1997-05-06
2
1,711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

687 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