home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / acdual / server / mfcdual.cpp < prev    next >
C/C++ Source or Header  |  1998-04-02  |  3KB  |  114 lines

  1. // mfcdual.cpp: Helpful functions for adding dual interface support to
  2. //              MFC applications
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "AutoClik.h"
  16.  
  17. #include <afxpriv.h>
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // DualHandleException
  27.  
  28. HRESULT DualHandleException(REFIID riidSource, const CException* pAnyException)
  29. {
  30.     USES_CONVERSION;
  31.  
  32.     ASSERT_VALID(pAnyException);
  33.  
  34.     TRACE0("DualHandleException called\n");
  35.  
  36.     // Set ErrInfo object so that VTLB binding container
  37.     // applications can get rich error information.
  38.     ICreateErrorInfo* pcerrinfo;
  39.     HRESULT hr = CreateErrorInfo(&pcerrinfo);
  40.     if (SUCCEEDED(hr))
  41.     {
  42.         TCHAR   szDescription[256];
  43.         LPCTSTR pszDescription = szDescription;
  44.         GUID    guid = GUID_NULL;
  45.         DWORD   dwHelpContext = 0;
  46.         BSTR    bstrHelpFile = NULL;
  47.         BSTR    bstrSource = NULL;
  48.         if (pAnyException->IsKindOf(RUNTIME_CLASS(COleDispatchException)))
  49.         {
  50.             // specific IDispatch style exception
  51.             COleDispatchException* e = (COleDispatchException*)pAnyException;
  52.  
  53.             guid = riidSource;
  54.             hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF,
  55.                               (e->m_wCode + 0x200));
  56.  
  57.             pszDescription = e->m_strDescription;
  58.             dwHelpContext = e->m_dwHelpContext;
  59.  
  60.             // propagate source and help file if present
  61.             // call ::SysAllocString directly so no further exceptions are thrown
  62.             if (!e->m_strHelpFile.IsEmpty())
  63.                 bstrHelpFile = ::SysAllocString(T2COLE(e->m_strHelpFile));
  64.             if (!e->m_strSource.IsEmpty())
  65.                 bstrSource = ::SysAllocString(T2COLE(e->m_strSource));
  66.  
  67.         }
  68.         else if (pAnyException->IsKindOf(RUNTIME_CLASS(CMemoryException)))
  69.         {
  70.             // failed memory allocation
  71.             AfxLoadString(AFX_IDP_FAILED_MEMORY_ALLOC, szDescription);
  72.             hr = E_OUTOFMEMORY;
  73.         }
  74.         else
  75.         {
  76.             // other unknown/uncommon error
  77.             AfxLoadString(AFX_IDP_INTERNAL_FAILURE, szDescription);
  78.             hr = E_UNEXPECTED;
  79.         }
  80.  
  81.         if (bstrHelpFile == NULL && dwHelpContext != 0)
  82.             bstrHelpFile = ::SysAllocString(T2COLE(AfxGetApp()->m_pszHelpFilePath));
  83.  
  84.         if (bstrSource == NULL)
  85.             bstrSource = ::SysAllocString(T2COLE(AfxGetAppName()));
  86.  
  87.         // Set up ErrInfo object
  88.         pcerrinfo->SetGUID(guid);
  89.         pcerrinfo->SetDescription(::SysAllocString(T2COLE(pszDescription)));
  90.         pcerrinfo->SetHelpContext(dwHelpContext);
  91.         pcerrinfo->SetHelpFile(bstrHelpFile);
  92.         pcerrinfo->SetSource(bstrSource);
  93.  
  94.         TRACE("\tSource = %ws\n", bstrSource);
  95.         TRACE("\tDescription = %s\n", pszDescription);
  96.         TRACE("\tHelpContext = %lx\n", dwHelpContext);
  97.         TRACE("\tHelpFile = %ws\n", bstrHelpFile);
  98.  
  99.         // Set the ErrInfo object for the current thread
  100.         IErrorInfo* perrinfo;
  101.         if (SUCCEEDED(pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID*)&perrinfo)))
  102.         {
  103.             SetErrorInfo(0, perrinfo);
  104.             perrinfo->Release();
  105.         }
  106.  
  107.         pcerrinfo->Release();
  108.     }
  109.  
  110.     TRACE("DualHandleException returning HRESULT %lx\n", hr);
  111.  
  112.     return hr;
  113. }
  114.