Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.4 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1999 **
  4. //*********************************************************************
  5. //
  6. // CFACTORY.H - Header file for the Implementation of IClassFactory
  7. //
  8. // HISTORY:
  9. //
  10. // 1/27/99 a-jaswed Created.
  11. //
  12. // Base class for reusing a single class factory for all
  13. // components in a DLL.
  14. #ifndef __CFactory_h__
  15. #define __CFactory_h__
  16. #include "cunknown.h"
  17. ///////////////////////////////////////////////////////////
  18. // Forward reference.
  19. //
  20. class CFactoryData;
  21. // Global data used by CFactory.
  22. extern CFactoryData g_FactoryDataArray[];
  23. extern int g_cFactoryDataEntries;
  24. //////////////////////////////////////////////////////////////////////////////////////
  25. // Component creation function pointer.
  26. //
  27. typedef HRESULT (*FPCREATEINSTANCE)(IUnknown*, CUnknown**);
  28. //////////////////////////////////////////////////////////////////////////////////////
  29. // typedefs
  30. //
  31. // The special registration function is used to add custom registration and
  32. // unregistration code to the component. It takes a single parameter.
  33. // TRUE = Reg and FALSE = UnReg.
  34. typedef void (*FPSPECIALREGISTRATION)(BOOL);
  35. //////////////////////////////////////////////////////////////////////////////////////
  36. // CFactoryData -
  37. //
  38. // Information CFactory needs to create a component supported
  39. // by the DLL.
  40. class CFactoryData
  41. {
  42. public:
  43. // The class ID for the component
  44. const CLSID* m_pCLSID;
  45. // Pointer to the function that creates it
  46. FPCREATEINSTANCE CreateInstance;
  47. // Name of the component to register in the registry
  48. const WCHAR* m_RegistryName;
  49. // ProgID
  50. const WCHAR* m_szProgID;
  51. // Version independent ProgID
  52. const WCHAR* m_szVerIndProgID;
  53. // Helper function for finding the class id
  54. BOOL IsClassID(const CLSID& clsid) const
  55. {return (*m_pCLSID == clsid) ;}
  56. // Function for performing special registration
  57. FPSPECIALREGISTRATION SpecialRegistration ;
  58. //----- Out of process server support -----
  59. // Pointer running class factory associated with this component.
  60. IClassFactory* m_pIClassFactory ;
  61. // Magic cookie to identify running object
  62. DWORD m_dwRegister ;
  63. } ;
  64. ///////////////////////////////////////////////////////////
  65. //
  66. // Class Factory
  67. //
  68. class CFactory : public IClassFactory
  69. {
  70. public:
  71. // IUnknown
  72. virtual HRESULT __stdcall QueryInterface( const IID& iid,
  73. void** ppv) ;
  74. virtual ULONG __stdcall AddRef() ;
  75. virtual ULONG __stdcall Release() ;
  76. // IClassFactory
  77. virtual HRESULT __stdcall CreateInstance( IUnknown* pUnkOuter,
  78. const IID& iid,
  79. void** ppv) ;
  80. virtual HRESULT __stdcall LockServer(BOOL bLock);
  81. // Constructor - Pass pointer to data of component to create.
  82. CFactory(const CFactoryData* pFactoryData) ;
  83. // Destructor
  84. ~CFactory() {/*empty*/ }
  85. // ----- static FactoryData support functions. -----
  86. // DllGetClassObject Support
  87. static HRESULT GetClassObject( const CLSID& clsid,
  88. const IID& iid,
  89. void** ppv);
  90. // Helper function for DllCanUnloadNow
  91. static BOOL IsLocked()
  92. { return (s_cServerLocks > 0) ; }
  93. // Functions to [Un]Register all components.
  94. static HRESULT RegisterAll();
  95. static HRESULT UnregisterAll();
  96. // Functions to determine if component can be unloaded.
  97. static HRESULT CanUnloadNow();
  98. #ifdef _OUTPROC_SERVER_
  99. // ----- Out of process server support -----
  100. static BOOL StartFactories() ;
  101. static void StopFactories() ;
  102. static DWORD s_dwThreadID;
  103. // Shut down the application.
  104. static void CloseExe()
  105. {
  106. if (CanUnloadNow() == S_OK)
  107. {
  108. ::PostThreadMessage(s_dwThreadID, WM_QUIT, 0, 0);
  109. }
  110. }
  111. #else
  112. // CloseExe doesn't do anything if we are in process.
  113. static void CloseExe() {/*Empty*/}
  114. #endif
  115. public:
  116. // Reference Count
  117. LONG m_cRef;
  118. // Pointer to information about class this factory creates.
  119. const CFactoryData* m_pFactoryData;
  120. // Count of locks
  121. static LONG s_cServerLocks ;
  122. // Module handle
  123. static HMODULE s_hModule ;
  124. };
  125. #endif