dll.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 4k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /***************************************************************************
  2.  *  dll.cpp
  3.  *
  4.  *  Standard DLL entry-point functions 
  5.  *
  6.  ***************************************************************************/
  7. #include "priv.h"
  8. #define DECLARE_DEBUG
  9. #define SZ_MODULE       "SRVWIZ"
  10. #define SZ_DEBUGSECTION "SrvWiz"
  11. #define SZ_DEBUGINI     "SrvWiz"
  12. #include "debug.h"
  13. HINSTANCE g_hinst = NULL;
  14. LONG    g_cRefDll = 0;      /* Global reference count */
  15. CRITICAL_SECTION g_csDll;   /* The shared critical section */
  16. /*----------------------------------------------------------
  17. Purpose: DllEntryPoint
  18. */
  19. BOOL 
  20. APIENTRY 
  21. DllMain(
  22.     IN HINSTANCE hinst, 
  23.     IN DWORD dwReason, 
  24.     IN LPVOID lpReserved)
  25. {
  26.     switch(dwReason) 
  27.     {
  28.         case DLL_PROCESS_ATTACH:
  29.             InitializeCriticalSection(&g_csDll);
  30.             DisableThreadLibraryCalls(hinst);
  31.             g_hinst = hinst;
  32.             break;
  33.         case DLL_PROCESS_DETACH:
  34.             DeleteCriticalSection(&g_csDll);
  35.             break;
  36.         case DLL_THREAD_ATTACH:
  37.         case DLL_THREAD_DETACH:
  38.         default:
  39.             break;
  40.     } 
  41.     return TRUE;
  42. /*----------------------------------------------------------
  43. Purpose: This function provides the DLL version info.  This 
  44.          allows the caller to distinguish running NT SUR vs.
  45.          Win95 shell vs. Nashville, etc.
  46.          The caller must GetProcAddress this function.  
  47. Returns: NO_ERROR
  48.          ERROR_INVALID_PARAMETER if pinfo is invalid
  49. */
  50. // All we have to do is declare this puppy and CCDllGetVersion does the rest
  51. DLLVER_DUALBINARY(VER_PRODUCTVERSION_DW, VER_PRODUCTBUILD_QFE);
  52. STDAPI_(void) DllAddRef(void)
  53. {
  54.     InterlockedIncrement(&g_cRefDll);
  55. }
  56. STDAPI_(void) DllRelease(void)
  57. {
  58.     InterlockedDecrement(&g_cRefDll);
  59. }
  60. /*****************************************************************************
  61.  *
  62.  *    DllGetClassObject
  63.  *
  64.  *    OLE entry point.  Produces an IClassFactory for the indicated GUID.
  65.  *
  66.  *    The artificial refcount inside DllGetClassObject helps to
  67.  *    avoid the race condition described in DllCanUnloadNow.  It's
  68.  *    not perfect, but it makes the race window much smaller.
  69.  *
  70.  *****************************************************************************/
  71. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppvObj)
  72. {
  73.     HRESULT hres;
  74.     DllAddRef();
  75.     
  76.     if (IsEqualIID(rclsid, CLSID_SrvWiz))
  77.     {
  78.         hres = CSrvWizFactory_Create(rclsid, riid, ppvObj);
  79.     }
  80.     else
  81.     {
  82.         *ppvObj = NULL;
  83.         hres = CLASS_E_CLASSNOTAVAILABLE;
  84.     }
  85.     DllRelease();
  86.     return hres;
  87. }
  88. /*****************************************************************************
  89.  *
  90.  *    DllCanUnloadNow
  91.  *
  92.  *    OLE entry point.  Fail iff there are outstanding refs.
  93.  *
  94.  *    There is an unavoidable race condition between DllCanUnloadNow
  95.  *    and the creation of a new IClassFactory:  Between the time we
  96.  *    return from DllCanUnloadNow() and the caller inspects the value,
  97.  *    another thread in the same process may decide to call
  98.  *    DllGetClassObject, thus suddenly creating an object in this DLL
  99.  *    when there previously was none.
  100.  *
  101.  *    It is the caller's responsibility to prepare for this possibility;
  102.  *    there is nothing we can do about it.
  103.  *
  104.  *****************************************************************************/
  105. STDMETHODIMP DllCanUnloadNow(void)
  106. {
  107.     HRESULT hres;
  108.     ENTERCRITICAL;
  109.     hres = g_cRefDll ? S_FALSE : S_OK;
  110.     LEAVECRITICAL;
  111.     return hres;
  112. }