Home Contents Search

Error Message
Error Message Issue #23 skins QTime Perpetual Newbie Perpetual Newbie Perpetual Newbie Perpetual Newbie How Convert User Registration rabbit hole CASE & Classes skins Premium 5 Premium 6 Premium Domains Premium 2 Premium 3 Premium 4 Rare domains cities_realestate Similar   Websites education_sites entertainment_sites games misc_sites LLLL.com Site Acronym 2 Acronym 4 LLLLL.com LLLLL.com 2 LLLLL.com 3 Acronym 5 Acronym 6 Acronym 7 Acronym 8 Acronym 9 Acronym 10 Acronym 3 Brandable sites Pin Yin sites service_sites technology Acronym sites Payment Options About Our Office

System Error Text Messages

This article originally appeared in the February 1999 issue of Delphi Informant Magazine and is being reprinted here with permission from Informant Communications.

How many times have you seen this reference in the Windows API Help?

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

You see� while programmers around the world were sleeping, Microsoft decided that it was getting too difficult (and inefficient) to return all possible error values as the result of most function calls. Therefore, they reworked the idea of obtaining error values through a separate API function, namely GetLastError. Many API functions however retain some backward compatibility and still return the error results themselves (in addition to setting the value GetLastError uses).

However, we need to keep up with the times folks. As time goes on, the function return values for errors we have come to rely on will quite likely just go away.

A good example of this was the LoadLibrary command. When it succeeds, it returns the handle of the library (DLL or EXE) specified. However, when it failed, it used to return a small integer value (between 0 and 32). Now however, if it fails, it simply returns a zero (NULL) and requires that you go to GetLastError to get the real error result.

Obviously this is a much more efficient solution to obtaining error values. After all, mixing error values in with success values as the return result of a function requires that you know as a programmer which ones are errors and which ones are good function results.

Everything is wonderful now right? Well� No. If you look at GetLastError, it is still just returning a numeric result which still needs to be interpreted by your program. What if you want to get a text message of what the error is? Microsoft does include another function called FormatMessage which can take the result of GetLastError and make an intelligible error message out of it. Problem is that FormatMessage is not the most easy to use function.

Fortunately Delphi comes to the rescue again. In Delphi�s SysUtils unit, there is a very useful wrapper function around FormatMessage called SysErrorMessage. Whenever you get an error result from an API function, you simply pass GetLastError into a call to SysErrorMessage. The result is a string representation of the error.

For example,

ShowMessage(SysErrorMessage(GetLastError));

will use the result from SysErrorMessage and show it in a dialog box for you. Pretty handy!

Do keep in mind however that the value stored in GetLastError is very temporary. If you call a function and get an error result, make sure you call GetLastError immediately. Its value may change if you calling other API functions before you go and retrieve its value.

Note also that you can use SetLastError to set your own error values. This is normally done for functions stored in DLL�s. You can learn more about this in the Win32 API help under the section on SetLastError.
 

Copyright © 2007 undu.com                    Powered by Engineer Partner The One Stop Outsource