Click to return to the Networking, Protocols     
Appendix B: Using Buffers     Microsoft Win32 Internet ...     Microsoft Win32 Internet ...    
Web 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;
}


Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

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