// (C) PCSOFT - 1998
// Le seul support technique disponible pour ce programme est
// accessible à travers le service "Assistance Directe".


#ifndef __CFactory_h__
#define __CFactory_h__


#include <unknwn.h>

///////////////////////////////////////////////////////////
 
class CFactory : public IClassFactory
{
public:
	LONG					m_cRef;

	static DWORD			s_dwThreadID;
	static LONG				s_cServerLocks;   
	static HMODULE			s_hModule;			// handle du module
	static long				s_cActiveComponents;// compteur d'objets du module
	static IClassFactory*	s_pIClassFactory;	// Pointer to running class factory for this component
	static DWORD			s_dwRegister;		// Magic cookie to identify running object

public:
	// IUnknown
	virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ;
	virtual ULONG   __stdcall AddRef() ;
	virtual ULONG   __stdcall Release() ;
	
	// IClassFactory
	virtual HRESULT __stdcall CreateInstance(IUnknown* pUnknownOuter,
	                                         const IID& iid,
	                                         void** ppv) ;
	virtual HRESULT __stdcall LockServer(BOOL bLock) ; 


	CFactory();

	static BOOL IsLocked()		{ return (s_cServerLocks > 0) ;}

	// Functions to [un]register all components
	static HRESULT RegisterAll() ;
	static HRESULT UnregisterAll() ;

	// Function to determine if component can be unloaded
	static HRESULT CanUnloadNow() ;

	static BOOL StartFactories() ;
	static void StopFactories() ;

	// Shut down the application.
	static void CloseExe()
	{
		if (CanUnloadNow() == S_OK)
			::PostThreadMessage(s_dwThreadID, WM_QUIT, 0, 0) ;
	}

	// renvoie le nombre d'objets "actifs" du module :
	static long ActiveComponents()		{ return s_cActiveComponents ;}
} ;

#endif
