home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / adsi / sampprov / libmain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-29  |  4.0 KB  |  170 lines

  1. /*++
  2.  
  3. Copyright (c) 1996 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     LibMain.cpp
  8.  
  9. Abstract:
  10.  
  11.     LibMain for ADsSmp.dll
  12.  
  13. Author:
  14.  
  15. Environment:
  16.  
  17.     User mode
  18.  
  19. Revision History :
  20.  
  21. --*/
  22. #include "adssmp.h"
  23. #pragma hdrstop
  24.  
  25.  
  26. //  Globals
  27. HINSTANCE g_hInst = NULL;
  28.  
  29. ULONG g_ulObjCount = 0L;        // Number of objects alive in adssmp.dll
  30. ULONG g_ulLockCount = 0L;       // Number of server locks on adssmp.dll
  31.  
  32.  
  33. //+------------------------------------------------------------------------
  34. //
  35. //  Macro that calculates the number of elements in a statically-defined
  36. //  array.
  37. //-------------------------------------------------------------------------
  38. #define ARRAY_SIZE(_a)  (sizeof(_a) / sizeof(_a[0]))
  39.  
  40. //+------------------------------------------------------------------------
  41. // Create static Namespace and Provider class factory objects
  42. //-------------------------------------------------------------------------
  43. CSampleDSProviderCF g_cfProvider;
  44. CSampleDSNamespaceCF g_cfNamespace;
  45.  
  46.  
  47. //+------------------------------------------------------------------------
  48. //
  49. //  ads class factory lookup table
  50. //
  51. //-------------------------------------------------------------------------
  52.  
  53. struct CLSTABLE
  54. {
  55.     const CLSID *   pclsid;
  56.     IClassFactory * pCF;
  57. };
  58.  
  59.  
  60.  
  61. CLSTABLE g_aclscache[] =
  62. {
  63.     &CLSID_SampleDSProvider,                      &g_cfProvider,
  64.     &CLSID_SampleDSNamespace,                     &g_cfNamespace,
  65. };
  66.  
  67.  
  68. //+---------------------------------------------------------------
  69. //
  70. //  Function:   DllGetClassObject
  71. //
  72. //  Synopsis:   Standard DLL entrypoint for locating class factories
  73. //
  74. //----------------------------------------------------------------
  75.  
  76. STDAPI
  77. DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID FAR* ppv)
  78. {
  79.     HRESULT         hr;
  80.     size_t          i;
  81.  
  82.     for (i = 0; i < ARRAY_SIZE(g_aclscache); i++)
  83.     {
  84.         if (IsEqualCLSID(clsid, *g_aclscache[i].pclsid))
  85.         {
  86.             hr = g_aclscache[i].pCF->QueryInterface(iid, ppv);
  87.             RRETURN(hr);
  88.         }
  89.     }
  90.  
  91.     *ppv = NULL;
  92.  
  93.     return E_NOINTERFACE;
  94. }
  95.  
  96. //+---------------------------------------------------------------
  97. //
  98. //  Function:   DllCanUnloadNow
  99. //
  100. //  Synopsis:   Standard DLL entrypoint to determine if DLL can be unloaded
  101. //
  102. //---------------------------------------------------------------
  103.  
  104. STDAPI DllCanUnloadNow(void) {
  105.     if (0L==g_ulObjCount && 0L==g_ulLockCount) return ResultFromScode(S_OK);
  106.     else return ResultFromScode(S_FALSE);
  107. }     
  108.  
  109. //+---------------------------------------------------------------
  110. //
  111. //  Function:   DllMain
  112. //
  113. //  Synopsis:   Standard DLL initialization entrypoint
  114. //
  115. //---------------------------------------------------------------
  116.  
  117. BOOL WINAPI
  118. LibMain(HINSTANCE hInst, DWORD dwReason, LPVOID pvReserved)
  119. {
  120.         
  121.     // Check our compatibility with the OLE runtime.
  122.     // We are compatible with any later major version,
  123.     // or the same major version with equal or greater minor version.
  124.     if (!IsCompatibleOleVersion(OLE_MAJ_VER, OLE_MIN_VER))
  125.     {
  126.         return FALSE;
  127.     }
  128.  
  129.     switch (dwReason){
  130.     case DLL_PROCESS_ATTACH:
  131.  
  132.         g_hInst = hInst;
  133.         break;
  134.  
  135.  
  136.     case DLL_PROCESS_DETACH:
  137.  
  138.  
  139.     default:
  140.         break;
  141.     }
  142.  
  143.     return TRUE;
  144. }
  145.  
  146. BOOL
  147. IsCompatibleOleVersion(WORD wMaj, WORD wMin)
  148. {
  149.     // Check our compatibility with the OLE runtime.
  150.     // We are compatible with any later major version,
  151.     // or the same major version with equal or greater minor version.
  152.     DWORD ov = OleBuildVersion();
  153.     return HIWORD(ov) > wMaj || (HIWORD(ov) == wMaj && LOWORD(ov) >= wMin);
  154. }
  155.  
  156.  
  157.  
  158. //+---------------------------------------------------------------------------
  159. //
  160. //  Function:   DllMain
  161. //
  162. //  Synopsis:   entry point for NT - post .546
  163. //
  164. //----------------------------------------------------------------------------
  165. EXTERN_C BOOL WINAPI
  166. DllMain(HANDLE hDll, DWORD dwReason, LPVOID lpReserved)
  167. {
  168.     return LibMain((HINSTANCE)hDll, dwReason, lpReserved);
  169. }
  170.