Retrieving Network Errors
The WNet functions return error codes for compatibility with Windows for Workgroups. Each WNet function also sets the error code value returned by GetLastError.
When one of the WNet functions returns ERROR_EXTENDED_ERROR, an application can call the WNetGetLastError function to retrieve additional information about the error. This information is usually specific to the network provider.
The following example illustrates an application-defined error-handling function (NetErrorHandler). The function 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 ERROR_EXTENDED_ERROR, NetErrorHandler calls WNetGetLastError to get extended error information and prints the information. The sample calls the MessageBox function to process messages.
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "mpr.lib")
#pragma comment(lib, "user32.lib")
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)
{
sprintf_s((LPSTR) szError, sizeof(szError), "%s failed; \nResult is %ld",
lpszFunction, dwErrorCode);
sprintf_s((LPSTR) szCaption, sizeof(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 the
// WNetGetLastError function can retrieve additional information.
else
{
dwWNetResult = WNetGetLastError(&dwLastError, // error code
(LPSTR) szDescription, // buffer for error description
sizeof(szDescription), // size of error buffer
(LPSTR) szProvider, // buffer for provider name
sizeof(szProvider)); // size of name buffer
//
// Process errors.
//
if(dwWNetResult != NO_ERROR) {
sprintf_s((LPSTR) szError, sizeof(szError),
"WNetGetLastError failed; error %ld", dwWNetResult);
MessageBox(hwnd, (LPSTR) szError, "WNetGetLastError", MB_OK);
return FALSE;
}
//
// Otherwise, print the additional error information.
//
sprintf_s((LPSTR) szError, sizeof(szError),
"%s failed with code %ld;\n%s",
(LPSTR) szProvider, dwLastError, (LPSTR) szDescription);
sprintf_s((LPSTR) szCaption, sizeof(szCaption), "%s error", lpszFunction);
MessageBox(hwnd, (LPSTR) szError, (LPSTR) szCaption, MB_OK);
return TRUE;
}
}