|
How often do you get really irritated searching around for the meaning behind API error codes? Well, there's a quick and simple way to obtain the error text for thousands of these codes, with one simple API call.
This is a little tool I put together to quickly identify the meaning of API error numbers. It provides a search-o-matic listbox with all defined API error messages. Also will paste onto the clipboard the required routine to obtain error messages on demand using FormatMessage. This download now includes the full source for the utility, as well as a compiled (with VB5/SP3) EXE that I'll bet will end up on your desktop, as it has mine. Also included is a drop-in BAS module you can add instantly to any project.
It can be frustrating looking up values returned by Err.LastDllError, to be sure. What's that? You use GetLastError? As much as I hate to tell you, that's really not very smart. You see, the way Microsoft designed VB, there's a whole lotta API calls going on behind your back There's no guarantee that calling GetLastError will result in that set by the API call you made, as it could just as easily be referring to the return from an API call the VB runtime made for you..
All 32-bit versions of Classic VB automatically call GetLastError on your behalf, and store the returned value in Err.LastDllError, after every single API call that you make. This is of course a horribly inefficient scheme, perhaps adding an additional 20% or more to the time required to make each and every API call. But, because of UniMess�, there's really very few options. The ideal answer would've been adding a parameter to API declarations which would turn this generally unnecessary overhead off.
Here's the quick rundown on decoding API error messages.
Option Explicit Private Declare Function FormatMessage Lib "kernel32" _ Alias "FormatMessageA" (ByVal dwFlags As Long, _ lpSource As Any, ByVal dwMessageId As Long, _ ByVal dwLanguageId As Long, ByVal lpBuffer As String, _ ByVal nSize As Long, Arguments As Long) As Long Private Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000 Private Function ApiErrorText(ByVal ErrNum As Long) As String Dim msg As String Dim nRet As Long msg = Space$(1024) nRet = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, _ ErrNum, 0&, msg, Len(msg), ByVal 0&) If nRet Then ApiErrorText = Left$(msg, nRet) Else ApiErrorText = "Error (" & ErrNum & ") not defined." End If End FunctionThe sample gets a little more involved by including messages contained in netmsg.dll and wininet.dll, so for a full-fledged, drop-in ready module, grab the whole thing below.
This sample, or the one from which it originally derived, was published (or at least peripherally mentioned) in the following article(s):
- Move Data With Memory-Mapped Files, Ask the VB Pro, VBPJ, October 1999
This sample uses the following API calls:
Module Library Function ApiErrMsg.frm kernel32
user32FormatMessage
FreeLibrary
LoadLibraryEx
RtlMoveMemory
Sleep
InvalidateRect
LockWindowUpdate
SendMessageCLstSrch5.cls user32 SendMessage COpSysInfo.cls kernel32 GetVersionEx CRegSettings.cls advapi32 RegCloseKey
RegCreateKeyEx
RegDeleteKey
RegDeleteValue
RegOpenKeyEx
RegQueryValueEx
RegSetValueExMApiErrMsg.bas kernel32 FormatMessage
FreeLibrary
GetVersionEx
LoadLibraryExDon't see what you're looking for? Here's a complete API cross-reference.
Please, enjoy and learn from this sample. Include its code within your own projects, if you wish. But, in order to insure only the most recent code is available to all, I ask that you don't share the sample by any form of mass distribution. Download ApiErrMsg.zip, 36Kb, Last Updated: Monday, July 5, 1999