Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using GetLastError()

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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