home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / oledll.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  4KB  |  121 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_OLE3_SEG
  14. #pragma code_seg(AFX_OLE3_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // Support for MFC/COM in DLLs
  24.  
  25. SCODE AFXAPI AfxDllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  26. {
  27.     *ppv = NULL;
  28.     DWORD lData1 = rclsid.Data1;
  29.  
  30.     // search factories defined in the application
  31.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  32.     AfxLockGlobals(CRIT_OBJECTFACTORYLIST);
  33.     for (COleObjectFactory* pFactory = pModuleState->m_factoryList;
  34.         pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  35.     {
  36.         if (pFactory->m_bRegistered != 0 &&
  37.             lData1 == pFactory->m_clsid.Data1 &&
  38.             ((DWORD*)&rclsid)[1] == ((DWORD*)&pFactory->m_clsid)[1] &&
  39.             ((DWORD*)&rclsid)[2] == ((DWORD*)&pFactory->m_clsid)[2] &&
  40.             ((DWORD*)&rclsid)[3] == ((DWORD*)&pFactory->m_clsid)[3])
  41.         {
  42.             // found suitable class factory -- query for correct interface
  43.             SCODE sc = pFactory->InternalQueryInterface(&riid, ppv);
  44.             AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
  45.             return sc;
  46.         }
  47.     }
  48.     AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
  49. #ifdef _AFXDLL
  50.     AfxLockGlobals(CRIT_DYNLINKLIST);
  51.     // search factories defined in extension DLLs
  52.     for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
  53.         pDLL = pDLL->m_pNextDLL)
  54.     {
  55.         for (pFactory = pDLL->m_factoryList;
  56.             pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  57.         {
  58.             if (pFactory->m_bRegistered != 0 &&
  59.                 lData1 == pFactory->m_clsid.Data1 &&
  60.                 ((DWORD*)&rclsid)[1] == ((DWORD*)&pFactory->m_clsid)[1] &&
  61.                 ((DWORD*)&rclsid)[2] == ((DWORD*)&pFactory->m_clsid)[2] &&
  62.                 ((DWORD*)&rclsid)[3] == ((DWORD*)&pFactory->m_clsid)[3])
  63.             {
  64.                 // found suitable class factory -- query for correct interface
  65.                 SCODE sc = pFactory->InternalQueryInterface(&riid, ppv);
  66.                 AfxUnlockGlobals(CRIT_DYNLINKLIST);
  67.                 return sc;
  68.             }
  69.         }
  70.     }
  71.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  72. #endif
  73.  
  74.     // factory not registered -- return error
  75.     return CLASS_E_CLASSNOTAVAILABLE;
  76. }
  77.  
  78. SCODE AFXAPI AfxDllCanUnloadNow(void)
  79. {
  80.     // return S_OK only if no outstanding objects active
  81.     if (!AfxOleCanExitApp())
  82.         return S_FALSE;
  83.  
  84.     // check if any class factories with >1 reference count
  85.     AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  86.     AfxLockGlobals(CRIT_OBJECTFACTORYLIST);
  87.     for (COleObjectFactory* pFactory = pModuleState->m_factoryList;
  88.         pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  89.     {
  90.         if (pFactory->m_dwRef > 1)
  91.         {
  92.             AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
  93.             return S_FALSE;
  94.         }
  95.     }
  96.     AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
  97. #ifdef _AFXDLL
  98.     AfxLockGlobals(CRIT_DYNLINKLIST);
  99.     // search factories defined in extension DLLs
  100.     for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
  101.         pDLL = pDLL->m_pNextDLL)
  102.     {
  103.         for (pFactory = pDLL->m_factoryList;
  104.             pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  105.         {
  106.             if (pFactory->m_dwRef > 1)
  107.             {
  108.                 AfxUnlockGlobals(CRIT_DYNLINKLIST);
  109.                 return S_FALSE;
  110.             }
  111.         }
  112.     }
  113.     AfxUnlockGlobals(CRIT_DYNLINKLIST);
  114. #endif
  115.  
  116.     TRACE0("Info: AfxDllCanUnloadNow returning S_OK\n");
  117.     return S_OK;
  118. }
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121.