home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / General / ADsError / IADsError.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-14  |  5.1 KB  |  246 lines

  1. // IADsError.cpp : Implementation of CADsErrorApp and DLL registration.
  2.  
  3. #include "stdafx.h"
  4. #include "ADsError.h"
  5. #include "IADsError.h"
  6.  
  7.  
  8. /////////////////////////////////////////////////////
  9. //
  10. // Private Functions
  11. HRESULT GetADSIError( HRESULT hr, CComBSTR &bstr );
  12. HRESULT GetErrorMessage( HRESULT hr, CComBSTR &bstr );
  13. BOOL GetMessageHelper(DWORD flags, HMODULE hModule, HRESULT hr, CComBSTR &bstr );
  14.  
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. //
  18.  
  19. STDMETHODIMP ADsError::InterfaceSupportsErrorInfo(REFIID riid)
  20. {
  21.     static const IID* arr[] = 
  22.     {
  23.         &IID_IADsError,
  24.     };
  25.  
  26.     for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  27.     {
  28.         if (InlineIsEqualGUID(*arr[i],riid))
  29.             return S_OK;
  30.     }
  31.     return S_FALSE;
  32. }
  33.  
  34. STDMETHODIMP ADsError::GetErrorMessage(long hResult, BSTR *pbMsg)
  35. {
  36.     CComBSTR bstr;
  37.     HRESULT hr;
  38.     hr = ::GetErrorMessage( hResult, bstr );
  39.  
  40.     if ( SUCCEEDED(hr) )
  41.     {
  42.         *pbMsg = SysAllocString(bstr);
  43.     }
  44.     
  45.  
  46.     return hr;
  47. }
  48.  
  49.  
  50.  
  51. ////////////////////////////////////////////////////////
  52. //
  53. // ADSI Error Message
  54. //
  55. ////////////////////////////////////////////////////////
  56.  
  57. #define FACILITY_ADSI     0x00005000
  58.  
  59.  
  60.  
  61. HRESULT GetErrorMessage( HRESULT hr, CComBSTR &bstr )
  62. {
  63.  
  64.     bstr.Empty();
  65.     if ( SUCCEEDED(hr) )
  66.     {
  67.         bstr = _T("SUCCESS");
  68.         return S_OK;
  69.     }
  70.  
  71.     int code = HRESULT_CODE(hr);
  72.  
  73.     if (code == -1)
  74.     {
  75.        bstr = _T("Unknown Error Code");
  76.        return S_OK;
  77.     }
  78.  
  79.     // default is the system error message table
  80.    HMODULE hModule = 0;
  81.  
  82.    DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS |FORMAT_MESSAGE_FROM_SYSTEM;
  83.  
  84.    int facility = HRESULT_FACILITY(hr);
  85.    if ( hr & FACILITY_ADSI ) // standard ADSI Errors 
  86.    {
  87.         
  88.         static HMODULE adsi_err_res_dll = 0;
  89.         // use the net error message resource dll
  90.         if (adsi_err_res_dll == 0)
  91.         {
  92.                adsi_err_res_dll =LoadLibraryEx(_T("activeds.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE);
  93.         }
  94.  
  95.         hModule = adsi_err_res_dll;
  96.         dwFlags |= FORMAT_MESSAGE_FROM_HMODULE;
  97.  
  98.         // the message dll expects the entire error code
  99.         code = hr;
  100.  
  101.    }
  102.    else
  103.    {
  104.  
  105.         switch (facility)
  106.         {
  107.  
  108.             case FACILITY_WIN32:    // 0x7
  109.             {
  110.                 // included here:
  111.                 // lanman error codes (in it's own dll)
  112.                 // dns
  113.                 // winsock
  114.  
  115.                  static HMODULE lm_err_res_dll = 0;
  116.                  if (code >= NERR_BASE && code <= MAX_NERR)
  117.                  {
  118.                     // use the net error message resource dll
  119.                     if (lm_err_res_dll == 0)
  120.                     {
  121.                         lm_err_res_dll = LoadLibraryEx(_T("netmsg.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE);
  122.                     }
  123.  
  124.                     hModule = lm_err_res_dll;
  125.                     dwFlags |= FORMAT_MESSAGE_FROM_HMODULE;
  126.                  }
  127.             }
  128.             break;
  129.  
  130.             case FACILITY_SSPI: // 0x9
  131.             {
  132.                 // some of these are in their own dll (those declared in issperr.h),
  133.                 // some are in the system message tables.
  134.                 // first try the system message tables.
  135.                 CComBSTR bTest;
  136.                 if( !GetMessageHelper( dwFlags, 0, code, bTest ) )
  137.                 {
  138.                     // not found.  try the secur32.dll resources
  139.                     // @@ use SafeDLL here.
  140.                     static HMODULE sec_err_res_dll = 0;
  141.                     if (sec_err_res_dll == 0)
  142.                     {
  143.                         sec_err_res_dll = LoadLibraryEx(_T("secur32.dll"), NULL, LOAD_LIBRARY_AS_DATAFILE);
  144.                     }
  145.  
  146.                     hModule = sec_err_res_dll;
  147.                     dwFlags |= FORMAT_MESSAGE_FROM_HMODULE;
  148.                 }
  149.                 break;
  150.             }
  151.  
  152.             default:
  153.             {
  154.                 //do nothing;
  155.                 break;
  156.             }
  157.  
  158.  
  159.         } // switch
  160.  
  161.  
  162.  
  163.  
  164.    } // end else
  165.  
  166.  
  167.     // Retrieve the message
  168.     GetMessageHelper( dwFlags, hModule, code, bstr );
  169.  
  170.  
  171.  
  172.  
  173.     
  174.     //////////////////////////////////////////////////////////////////
  175.     //
  176.     // Extended error message may be occured. 
  177.     //
  178.     // IADs, IADsContainer, IDirectoryObject or IDirectorySearch may return
  179.     // this extended error message
  180.     //
  181.     ////////////////////////////////////////////////////////////////////
  182.  
  183.     if (  (hr & FACILITY_ADSI)  ||         //adsi
  184.           facility == FACILITY_WIN32  )   // and win32
  185.     {
  186.         WCHAR szBuffer[MAX_PATH];
  187.         WCHAR szName[MAX_PATH];
  188.         DWORD dwError;
  189.     
  190.  
  191.         hr = ADsGetLastError( &dwError, szBuffer, (sizeof(szBuffer)/sizeof(WCHAR))-1,
  192.                               szName, (sizeof(szName)/sizeof(WCHAR))-1 );
  193.     
  194.     
  195.         if ( SUCCEEDED(hr) && dwError != ERROR_INVALID_DATA  && wcslen(szBuffer))
  196.         {
  197.             USES_CONVERSION;
  198.             bstr.Append(_T("  -- Extended Error:"));
  199.             bstr.Append(szName);
  200.             bstr.Append(_T(" : "));
  201.             bstr.Append( szBuffer );
  202.         }
  203.     }
  204.  
  205.  
  206.     return S_OK;
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. BOOL GetMessageHelper(DWORD dwFlags, HMODULE hModule, HRESULT hr, CComBSTR &bstr )
  215. {
  216.         BOOL bRet;
  217.         LPTSTR lpBuffer=NULL;
  218.  
  219.         /////////////////////////////////////////////
  220.         //
  221.         // Retrieve the Error message
  222.         //
  223.         /////////////////////////////////////////////////
  224.  
  225.         bRet = FormatMessage(dwFlags,
  226.                              hModule,  hr,
  227.                              MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
  228.                              (LPTSTR) &lpBuffer, 0, NULL);
  229.  
  230.         if ( !bRet )
  231.         {
  232.             TCHAR szErr[256];
  233.             wsprintf(szErr, _T("Error %X"), hr );
  234.             bstr =  szErr;
  235.         }
  236.  
  237.         if ( lpBuffer )
  238.         {
  239.             bstr.Append( lpBuffer );
  240.             LocalFree( lpBuffer );
  241.         }
  242.  
  243.         return bRet;
  244.     
  245. }
  246.