home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / Profiler / classfactory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.7 KB  |  465 lines

  1. /****************************************************************************************
  2.  * Copyright (c) 1999 Microsoft Corporation.  All Rights Reserved.
  3.  *
  4.  * File:
  5.  *  ClassFactory.cpp
  6.  *
  7.  * Description:
  8.  *    
  9.  *
  10.  *
  11.  ***************************************************************************************/
  12. #include "ClassFactory.h"
  13.  
  14.  
  15. /***************************************************************************************
  16.  ********************                                               ********************
  17.  ********************          CClassFactory Implementation         ********************
  18.  ********************                                               ********************
  19.  ***************************************************************************************/
  20.  
  21. /***************************************************************************************
  22.  *    Method:
  23.  *
  24.  *
  25.  *    Purpose:
  26.  *
  27.  *
  28.  *    Parameters: 
  29.  *
  30.  *
  31.  *    Return value:
  32.  *
  33.  *
  34.  *    Notes:
  35.  *
  36.  ***************************************************************************************/
  37.  /* private */
  38. CClassFactory::CClassFactory()
  39. {
  40.  
  41. } // ctor
  42.  
  43.  
  44. /***************************************************************************************
  45.  *    Method:
  46.  *
  47.  *
  48.  *    Purpose:
  49.  *
  50.  *
  51.  *    Parameters: 
  52.  *
  53.  *
  54.  *    Return value:
  55.  *
  56.  *
  57.  *    Notes:
  58.  *
  59.  ***************************************************************************************/
  60. /* public */
  61. CClassFactory::CClassFactory( const COCLASS_REGISTER *pCoClass ) :
  62.     m_refCount( 1 ), 
  63.     m_pCoClass( pCoClass )
  64. {
  65.  
  66. } // ctor
  67.  
  68.  
  69. /***************************************************************************************
  70.  *    Method:
  71.  *
  72.  *
  73.  *    Purpose:
  74.  *
  75.  *
  76.  *    Parameters: 
  77.  *
  78.  *
  79.  *    Return value:
  80.  *
  81.  *
  82.  *    Notes:
  83.  *
  84.  ***************************************************************************************/
  85. /* public */
  86. CClassFactory::~CClassFactory()
  87. {
  88.     
  89. } // dtor
  90.  
  91.  
  92. /***************************************************************************************
  93.  *    Method:
  94.  *
  95.  *
  96.  *    Purpose:
  97.  *
  98.  *
  99.  *    Parameters: 
  100.  *
  101.  *
  102.  *    Return value:
  103.  *
  104.  *
  105.  *    Notes:
  106.  *
  107.  ***************************************************************************************/
  108. /* public */
  109. ULONG CClassFactory::AddRef()
  110. {
  111.  
  112.       return InterlockedIncrement( &m_refCount );
  113.     
  114. } // CClassFactory::AddRef 
  115.         
  116.  
  117. /***************************************************************************************
  118.  *    Method:
  119.  *
  120.  *
  121.  *    Purpose:
  122.  *
  123.  *
  124.  *    Parameters: 
  125.  *
  126.  *
  127.  *    Return value:
  128.  *
  129.  *
  130.  *    Notes:
  131.  *
  132.  ***************************************************************************************/
  133. /* public */          
  134. ULONG CClassFactory::Release()
  135. {
  136.     long refCount;
  137.  
  138.  
  139.     refCount = InterlockedDecrement( &m_refCount );
  140.     if ( refCount == 0 ) 
  141.         delete this;
  142.  
  143.  
  144.     return refCount;
  145.             
  146. } // CClassFactory::Release
  147.  
  148.  
  149. /***************************************************************************************
  150.  *    Method:
  151.  *
  152.  *
  153.  *    Purpose:
  154.  *
  155.  *
  156.  *    Parameters: 
  157.  *
  158.  *
  159.  *    Return value:
  160.  *
  161.  *
  162.  *    Notes:
  163.  *
  164.  ***************************************************************************************/
  165. /* public */
  166. HRESULT CClassFactory::QueryInterface( REFIID riid, void **ppInterface )
  167. {
  168.     if ( riid == IID_IUnknown )
  169.         *ppInterface = static_cast<IUnknown *>( this );    
  170.  
  171.     else if ( riid == IID_IClassFactory )
  172.         *ppInterface = static_cast<IClassFactory *>( this );
  173.  
  174.     else
  175.     {
  176.         *ppInterface = NULL;            
  177.  
  178.         
  179.         return E_NOINTERFACE;
  180.     }
  181.     
  182.     reinterpret_cast<IUnknown *>( *ppInterface )->AddRef();
  183.  
  184.     
  185.     return S_OK;
  186.  
  187. } // CClassFactory::QueryInterface
  188.  
  189.  
  190. /***************************************************************************************
  191.  *    Method:
  192.  *
  193.  *
  194.  *    Purpose:
  195.  *
  196.  *
  197.  *    Parameters: 
  198.  *
  199.  *
  200.  *    Return value:
  201.  *
  202.  *
  203.  *    Notes:
  204.  *
  205.  ***************************************************************************************/
  206. /* public */ 
  207. HRESULT CClassFactory::CreateInstance( IUnknown    *pUnkOuter,    REFIID riid, void **ppInstance )
  208. {    
  209.     // aggregation is not supported by these objects
  210.     if ( pUnkOuter != NULL )
  211.         return CLASS_E_NOAGGREGATION;
  212.     
  213.     
  214.     // ask the object to create an instance of itself, and check the iid.
  215.     return (*m_pCoClass->pfnCreateObject)( riid, ppInstance );
  216.        
  217. } // CClassFactory::CreateInstance
  218.  
  219.  
  220. /***************************************************************************************
  221.  *    Method:
  222.  *
  223.  *
  224.  *    Purpose:
  225.  *
  226.  *
  227.  *    Parameters: 
  228.  *
  229.  *
  230.  *    Return value:
  231.  *
  232.  *
  233.  *    Notes:
  234.  *
  235.  ***************************************************************************************/
  236. /* public */
  237. HRESULT CClassFactory::LockServer( BOOL fLock )
  238. {
  239.  
  240.     return S_OK;
  241.     
  242. } // CClassFactory::LockServer
  243.  
  244.  
  245. /***************************************************************************************
  246.  ********************                                               ********************
  247.  ********************            Dll Registration Helpers           ********************
  248.  ********************                                               ********************
  249.  ***************************************************************************************/
  250.  
  251. /***************************************************************************************
  252.  *    Method:
  253.  *
  254.  *
  255.  *    Purpose:
  256.  *
  257.  *
  258.  *    Parameters: 
  259.  *
  260.  *
  261.  *    Return value:
  262.  *
  263.  *
  264.  *    Notes:
  265.  *
  266.  ***************************************************************************************/
  267. STDAPI DllRegisterServer()
  268. {
  269.     HRESULT hr = S_OK;
  270.     char  rcModule[_MAX_PATH];    
  271.     const COCLASS_REGISTER *pCoClass;    
  272.  
  273.     
  274.     DllUnregisterServer();
  275.     GetModuleFileNameA( GetModuleInst(), rcModule, NumItems( rcModule ) );
  276.  
  277.     // for each item in the coclass list, register it
  278.     for ( pCoClass = g_CoClasses; (SUCCEEDED( hr ) && (pCoClass->pClsid != NULL)); pCoClass++ )
  279.     {
  280.         // register the class with default values
  281.            hr = REGUTIL::RegisterCOMClass( *pCoClass->pClsid, 
  282.                                         g_szCoclassDesc, 
  283.                                         g_szProgIDPrefix,
  284.                                         g_iVersion, 
  285.                                         pCoClass->szProgID, 
  286.                                         g_szThreadingModel, 
  287.                                         rcModule );                  
  288.     } // for
  289.  
  290.  
  291.     if ( FAILED( hr ) )
  292.         DllUnregisterServer();
  293.     
  294.        
  295.     return hr;
  296.     
  297. } // DllRegisterServer
  298.  
  299.  
  300. /***************************************************************************************
  301.  *    Method:
  302.  *
  303.  *
  304.  *    Purpose:
  305.  *
  306.  *
  307.  *    Parameters: 
  308.  *
  309.  *
  310.  *    Return value:
  311.  *
  312.  *
  313.  *    Notes:
  314.  *
  315.  ***************************************************************************************/
  316. STDAPI DllUnregisterServer()
  317. {
  318.     const COCLASS_REGISTER *pCoClass;    
  319.     
  320.  
  321.     // for each item in the coclass list, unregister it
  322.     for ( pCoClass = g_CoClasses; pCoClass->pClsid != NULL; pCoClass++ )
  323.     {
  324.         REGUTIL::UnregisterCOMClass( *pCoClass->pClsid, 
  325.                                      g_szProgIDPrefix,
  326.                                      g_iVersion, 
  327.                                      pCoClass->szProgID );                                     
  328.     } // for
  329.         
  330.      
  331.     return S_OK;
  332.     
  333. } // DllUnregisterServer
  334.  
  335.  
  336. /***************************************************************************************
  337.  *    Method:
  338.  *
  339.  *
  340.  *    Purpose:
  341.  *
  342.  *
  343.  *    Parameters: 
  344.  *
  345.  *
  346.  *    Return value:
  347.  *
  348.  *
  349.  *    Notes:
  350.  *
  351.  ***************************************************************************************/
  352. STDAPI DllGetClassObject( REFCLSID rclsid, REFIID riid, LPVOID FAR *ppv )                   
  353. {
  354.     CClassFactory *pClassFactory;        
  355.     const COCLASS_REGISTER *pCoClass;    
  356.     HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
  357.  
  358.  
  359.     // scan for the right one
  360.     for ( pCoClass = g_CoClasses; pCoClass->pClsid != NULL; pCoClass++ )
  361.     {
  362.         if ( *pCoClass->pClsid == rclsid )
  363.         {
  364.             pClassFactory = new CClassFactory( pCoClass );
  365.             if ( pClassFactory != NULL )
  366.             {    
  367.                 hr = pClassFactory->QueryInterface( riid, ppv );
  368.                 
  369.                 pClassFactory->Release();
  370.                 break;
  371.             }
  372.             else
  373.             {
  374.                 hr = E_OUTOFMEMORY;
  375.                 break;    
  376.                }
  377.           }
  378.     } // for
  379.     
  380.     
  381.     return hr;
  382.     
  383. } // DllGetClassObject
  384.  
  385.  
  386. /***************************************************************************************
  387.  *    Method:
  388.  *
  389.  *
  390.  *    Purpose:
  391.  *
  392.  *
  393.  *    Parameters: 
  394.  *
  395.  *
  396.  *    Return value:
  397.  *
  398.  *
  399.  *    Notes:
  400.  *
  401.  ***************************************************************************************/
  402. HINSTANCE GetModuleInst()
  403. {
  404.     return g_hInst;
  405.     
  406. } // GetModuleInst
  407.  
  408.  
  409. /***************************************************************************************
  410.  ********************                                               ********************
  411.  ********************            DllMain Implementation             ********************
  412.  ********************                                               ********************
  413.  ***************************************************************************************/
  414.  
  415. /***************************************************************************************
  416.  *    Method:
  417.  *
  418.  *
  419.  *    Purpose:
  420.  *
  421.  *
  422.  *    Parameters: 
  423.  *
  424.  *
  425.  *    Return value:
  426.  *
  427.  *
  428.  *    Notes:
  429.  *
  430.  ***************************************************************************************/
  431. BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved )
  432. {
  433.     // save off the instance handle for later use
  434.     switch ( dwReason )
  435.     {
  436.         case DLL_PROCESS_ATTACH:
  437.             g_hInst = hInstance;
  438.             DisableThreadLibraryCalls( hInstance );
  439.             break;
  440.         
  441.         case DLL_PROCESS_DETACH:
  442.             {
  443.                 // lpReserved == NULL means that we called FreeLibrary()
  444.                 // in that case do nothing
  445.                 if ( (lpReserved != NULL) && (g_pCallbackObject != NULL) )
  446.                     g_pCallbackObject->DllExitShutdown();
  447.  
  448.             }
  449.             break;    
  450.         
  451.         default:
  452.             break;        
  453.     }
  454.     
  455.         
  456.     return TRUE;
  457.  
  458. } // DllMain
  459.  
  460.  
  461. // End of File
  462.  
  463.  
  464.  
  465.