Appendix C: Handling ErrorsAppendix C: Handling Errors*
*



Contents  *



Index  *Topic Contents
*Previous Topic: Appendix B: Using Buffers
*Next Topic: Microsoft Win32 Internet Functions Reference

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;
}

Up Top of Page
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.