Microsoft HomeproductssearchsupportshopWrite Us   Microsoft Home
Magazine
 |  Community
 |  Workshop
 |  Tools & Samples
 |  Training
 |  Site Info

Workshop  |  Networking, Protocols & Data Formats

Appendix C: Handling Errors


The Win32® function GetLastError is used to get the error code for all of the Win32 Internet functions. If ERROR_INTERNET_EXTENDED_ERROR is returned, there is a string or buffer containing a verbose error message. Call the InternetGetLastResponseInfo function to retrieve the extended error text.

To get the error text for a normal Win32 Internet error, use the Win32 function FormatMessage, and pass it an HMODULE handle to the Wininet.dll using the Win32 function GetModuleHandle.

The following is an example of a function to handle Win32 Internet function errors.

BOOL WINAPI ErrorOut ( HWND hErr, DWORD dError, TCHAR * szCallFunc)
{
TCHAR szTemp[356] = "", *szBuffer=NULL, *szBufferFinal = NULL;
        DWORD  dwIntError , dwLength = 0;
        char strName[256]="";

        FormatMessage(FORMAT_MESSAGE_FROM_HMODULE,
        GetModuleHandle("wininet.dll"),dError,0,
(LPSTR)strName,256,NULL);
wsprintf (szTemp,  "%s error code: %d\nMessage: %s\n", 
szCallFunc, dError, strName);
        int response;
        
        if (dError == ERROR_INTERNET_EXTENDED_ERROR)
        {
                InternetGetLastResponseInfo (&dwIntError, NULL, &dwLength);
                if (dwLength)
                {
                        if ( !(szBuffer = (TCHAR *) LocalAlloc ( LPTR,  dwLength) ) )
                        {
                                lstrcat (szTemp, TEXT ( "Unable to allocate memory to display 
                                         Internet error code. Error code: ") );
                                lstrcat (szTemp, TEXT (_itoa (GetLastError(), szBuffer, 10)  ) );
                                lstrcat (szTemp, TEXT ("\n") );
                                response = MessageBox(hErr, (LPSTR)szTemp,"Error",MB_OK);
                                return FALSE;
                        }
                        if (!InternetGetLastResponseInfo (&dwIntError, (LPTSTR) szBuffer, 
                            &dwLength))
                        {
                                lstrcat (szTemp, TEXT ( "Unable to get Internet error. Error code: ") );
                                lstrcat (szTemp, TEXT (_itoa (GetLastError(), szBuffer, 10)  ) );
                                lstrcat (szTemp, TEXT ("\n") );
                                response = MessageBox(hErr, (LPSTR)szTemp,"Error",MB_OK);
                                return FALSE;
                        }
                        if ( !(szBufferFinal = (TCHAR *) LocalAlloc ( LPTR,  
                               (strlen (szBuffer) +strlen (szTemp) + 1)  ) )  )
                        {
                                lstrcat (szTemp, TEXT ( "Unable to allocate memory. Error code: ") );
                                lstrcat (szTemp, TEXT (_itoa (GetLastError(), szBuffer, 10)  ) );
                                lstrcat (szTemp, TEXT ("\n") );
                                response = MessageBox(hErr, (LPSTR)szTemp,"Error",MB_OK);
                                return FALSE;
                        }
                        lstrcpy (szBufferFinal, szTemp);
                        lstrcat (szBufferFinal, szBuffer);
                        LocalFree (szBuffer);
                        response = MessageBox(hErr, (LPSTR)szBufferFinal,"Error",MB_OK);
                        LocalFree (szBufferFinal);
                }
        }
        else
    {
                response = MessageBox(hErr, (LPSTR)szTemp,"Error",MB_OK);
        }

    return response;
}

Does this content meet your programming needs? Write us!

Back to topBack to top

© 1998 Microsoft Corporation. All rights reserved. Terms of use.

 

Magazine Home
Ask Jane
DHTML Dude
Extreme XML
For Starters
More or Hess
Servin' It Up
Site Lights
Web Men Talking
Member Community Home
Benefits: Freebies & Discounts
Benefits: Promote Your Site
Benefits: Connect with Your Peers
Benefits at a Glance
Online Special-Interest Groups
Your Membership
SBN Stores
Join Now
Workshop Home
Essentials
Content & Component Delivery
Component Development
Data Access & Databases
Design
DHTML, HTML & CSS
Extensible Markup Language (XML)
Languages & Development Tools
Messaging & Collaboration
Networking, Protocols & Data Formats
Reusing Browser Technology
Security & Cryptography
Server Technologies
Streaming & Interactive Media
Web Content Management
Workshop Index
Tools & Samples Home
Tools
Samples, Headers, Libs
Images
Sounds
Style Sheets
Web Fonts
Training Home
SBN Live Seminars
SBN Live Chats
Courses
Peer Support
CD-ROM Training
Books & Training Kits
Certification
SBN Home
New to SBN?
What's New on SBN
Site Map
Site Search
Glossary
Write Us
About This Site