Retrieving Network Errors

When one of the WNet functions returns WN_EXTENDED_ERROR, an application can call the WNetGetLastError function to get more information about the error that occurred. This information is usually specific to the network provider.

The following example illustrates an application-defined error-handling function (NetErrorHandler) that takes three arguments: a window handle, the error code returned by one of the WNet functions, and the name of the function that produced the error. If the error code is WN_EXTENDED_ERROR, NetErrorHandler calls WNetGetLastError to get extended error information.

BOOL WINAPI NetErrorHandler(HWND hwnd,

DWORD dwErrorCode,

LPSTR lpszFunction)

{

DWORD dwWNetResult, dwLastError;

CHAR szError[256];

CHAR szCaption[256];

CHAR szDescription[256];

CHAR szProvider[256];

// The following code performs standard error-handling.

if (dwErrorCode != ERROR_EXTENDED_ERROR)

{

wsprintf((LPSTR) szError, "%s failed; \nResult is %ld",

lpszFunction, dwErrorCode);

wsprintf((LPSTR) szCaption, "%s error", lpszFunction);

MessageBox(hwnd, (LPSTR) szError, (LPSTR) szCaption, MB_OK);

return TRUE;

}

// The following code performs error-handling when the

// ERROR_EXTENDED_ERROR return value indicates that WNetGetLastError

// can retrieve additional information.

else

{

dwWNetResult = WNetGetLastError(&dwLastError,

(LPSTR) szDescription, // buffer for error description

sizeof(szDescription),

(LPSTR) szProvider, // buffer for provider name

sizeof(szProvider));

if(dwWNetResult != NO_ERROR) {

wsprintf((LPSTR) szError,

"WNetGetLastError failed; error %ld", dwWNetResult);

MessageBox(hwnd, (LPSTR) szError,

"WNetGetLastError", MB_OK);

return FALSE;

}

wsprintf((LPSTR) szError,

"%s failed with code %ld;\n%s",

(LPSTR) szProvider, dwLastError, (LPSTR) szDescription);

wsprintf((LPSTR) szCaption, "%s error", lpszFunction);

MessageBox(hwnd, (LPSTR) szError, (LPSTR) szCaption, MB_OK);

return TRUE;

}

}