www.pudn.com > SimpleInput.rar > ClassFactory.cpp


// ClassFactory.cpp: implementation of the CClassFactory class. 
// 
////////////////////////////////////////////////////////////////////// 
#include "stdafx.h" 
#include  
#include  
#include "InputMethod.h" 
#include "ClassFactory.h" 
#include "Common.h" 
 
 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CClassFactory::CClassFactory(long *plDllCnt,HINSTANCE hInst) 
{ 
	m_lRef = 1;     // Set ref count to 1 on create. 
	m_plDllCnt = plDllCnt; 
	m_hInst = hInst; 
} 
 
CClassFactory::~CClassFactory() 
{ 
 
} 
 
 
//--------------------------------------------------------------------- 
//Description: 
//	Increment object ref count 
//---------------------------------------------------------------------- 
STDMETHODIMP CClassFactory::QueryInterface(REFIID riid, LPVOID *ppv) 
{ 
  
    // If caller wants our IUnknown or IClassFactory object,  
    // return a pointer to the object. 
    if (IsEqualIID (riid, IID_IUnknown) || IsEqualIID (riid, IID_IClassFactory))  
	{    
        *ppv = (LPVOID)this;     // Return pointer to object. 
        AddRef();                // Increment ref to prevent delete on return. 
 
		WRITELOG("CClassFactory::QueryInterface() return NOERROR"); 
 
        return NOERROR; 
    } 
    *ppv = NULL; 
 
 
	WRITELOG("CClassFactory::QueryInterface() return E_NOINTERFACE"); 
 
    return (E_NOINTERFACE); 
 
} 
 
 
//--------------------------------------------------------------------- 
//Description: 
//	Increment object ref count 
//---------------------------------------------------------------------- 
STDMETHODIMP_(ULONG) CClassFactory::AddRef() 
{ 
    ULONG cnt; 
    
    cnt = (ULONG)InterlockedIncrement (&m_lRef); 
    return cnt; 
} 
 
 
//--------------------------------------------------------------------- 
//Description: 
//	Increment object ref count 
//---------------------------------------------------------------------- 
STDMETHODIMP_(ULONG) CClassFactory::Release() 
{ 
    ULONG cnt; 
    
    cnt = (ULONG)InterlockedDecrement (&m_lRef); 
    if (cnt == 0) 
	{ 
        delete this; 
    } 
	return cnt; 
} 
 
 
//--------------------------------------------------------------------- 
//Description: 
//	CreateInstance - Called to have class factory object create other objects 
//---------------------------------------------------------------------- 
STDMETHODIMP CClassFactory::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppv) 
{ 
	 
    CInputMethod *pInputMethod; 
    HRESULT hr; 
    
    if (pUnkOuter) 
	{ 
        return (CLASS_E_NOAGGREGATION); 
	} 
    
	 
    if (IsEqualIID (riid, IID_IUnknown) || IsEqualIID (riid, IID_IInputMethod) || IsEqualIID (riid, IID_IInputMethod2)) 
	{		 
        // Create Input method object. 
        pInputMethod = new CInputMethod(m_plDllCnt,m_hInst); 
        if (!pInputMethod) 
		{ 
			WRITELOG("CClassFactory::CreateInstance : Failed in creating pInputMethod"); 
            return E_OUTOFMEMORY; 
		} 
        // See if object exports the proper interface. 
        hr = pInputMethod->QueryInterface (riid, ppv); 
        // This will cause an obj delete unless interface found. 
        pInputMethod->Release (); 
         
		WRITELOG("CClassFactory::CreateInstance : pInputMethod is ok and then return hr"); 
 
		return hr; 
				 
    }  
	 
	WRITELOG("CClassFactory::CreateInstance :return E_NOINTERFACE"); 
 
    return E_NOINTERFACE; 
} 
 
//--------------------------------------------------------------------- 
//Description: 
//	Increment object ref count 
//---------------------------------------------------------------------- 
STDMETHODIMP CClassFactory::LockServer(BOOL fLock) 
{ 
	 
	if (fLock) 
	{ 
        InterlockedIncrement (m_plDllCnt); 
    } 
	else 
	{ 
        InterlockedDecrement (m_plDllCnt); 
    } 
	 
	return NOERROR; 
}