oledll.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "stdafx.h"
  11. #ifdef AFX_OLE3_SEG
  12. #pragma code_seg(AFX_OLE3_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Support for MFC/COM in DLLs
  20. SCODE AFXAPI AfxDllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  21. {
  22. *ppv = NULL;
  23. DWORD lData1 = rclsid.Data1;
  24. // search factories defined in the application
  25. AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  26. AfxLockGlobals(CRIT_OBJECTFACTORYLIST);
  27. for (COleObjectFactory* pFactory = pModuleState->m_factoryList;
  28. pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  29. {
  30. if (pFactory->m_bRegistered != 0 &&
  31. lData1 == pFactory->m_clsid.Data1 &&
  32. ((DWORD*)&rclsid)[1] == ((DWORD*)&pFactory->m_clsid)[1] &&
  33. ((DWORD*)&rclsid)[2] == ((DWORD*)&pFactory->m_clsid)[2] &&
  34. ((DWORD*)&rclsid)[3] == ((DWORD*)&pFactory->m_clsid)[3])
  35. {
  36. // found suitable class factory -- query for correct interface
  37. SCODE sc = pFactory->InternalQueryInterface(&riid, ppv);
  38. AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
  39. return sc;
  40. }
  41. }
  42. AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
  43. #ifdef _AFXDLL
  44. AfxLockGlobals(CRIT_DYNLINKLIST);
  45. // search factories defined in extension DLLs
  46. for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
  47. pDLL = pDLL->m_pNextDLL)
  48. {
  49. for (pFactory = pDLL->m_factoryList;
  50. pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  51. {
  52. if (pFactory->m_bRegistered != 0 &&
  53. lData1 == pFactory->m_clsid.Data1 &&
  54. ((DWORD*)&rclsid)[1] == ((DWORD*)&pFactory->m_clsid)[1] &&
  55. ((DWORD*)&rclsid)[2] == ((DWORD*)&pFactory->m_clsid)[2] &&
  56. ((DWORD*)&rclsid)[3] == ((DWORD*)&pFactory->m_clsid)[3])
  57. {
  58. // found suitable class factory -- query for correct interface
  59. SCODE sc = pFactory->InternalQueryInterface(&riid, ppv);
  60. AfxUnlockGlobals(CRIT_DYNLINKLIST);
  61. return sc;
  62. }
  63. }
  64. }
  65. AfxUnlockGlobals(CRIT_DYNLINKLIST);
  66. #endif
  67. // factory not registered -- return error
  68. return CLASS_E_CLASSNOTAVAILABLE;
  69. }
  70. SCODE AFXAPI AfxDllCanUnloadNow(void)
  71. {
  72. // return S_OK only if no outstanding objects active
  73. if (!AfxOleCanExitApp())
  74. return S_FALSE;
  75. // check if any class factories with >1 reference count
  76. AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  77. AfxLockGlobals(CRIT_OBJECTFACTORYLIST);
  78. for (COleObjectFactory* pFactory = pModuleState->m_factoryList;
  79. pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  80. {
  81. if (pFactory->m_dwRef > 1)
  82. {
  83. AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
  84. return S_FALSE;
  85. }
  86. }
  87. AfxUnlockGlobals(CRIT_OBJECTFACTORYLIST);
  88. #ifdef _AFXDLL
  89. AfxLockGlobals(CRIT_DYNLINKLIST);
  90. // search factories defined in extension DLLs
  91. for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
  92. pDLL = pDLL->m_pNextDLL)
  93. {
  94. for (pFactory = pDLL->m_factoryList;
  95. pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  96. {
  97. if (pFactory->m_dwRef > 1)
  98. {
  99. AfxUnlockGlobals(CRIT_DYNLINKLIST);
  100. return S_FALSE;
  101. }
  102. }
  103. }
  104. AfxUnlockGlobals(CRIT_DYNLINKLIST);
  105. #endif
  106. TRACE0("Info: AfxDllCanUnloadNow returning S_OK\n");
  107. return S_OK;
  108. }
  109. /////////////////////////////////////////////////////////////////////////////