home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / adosamp / server / adosamp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  4.4 KB  |  171 lines

  1. // Copyright (C) 1992-1998 Microsoft Corporation
  2. // All rights reserved.
  3. //
  4. // This source code is only intended as a supplement to the
  5. // Microsoft Visual C++ Language  Reference and related
  6. // electronic documentation provided with Microsoft Visual C++.
  7. // See these sources for detailed information regarding the
  8. // Microsoft Visual C++ product.
  9.  
  10. // ADOSamp.cpp : Implementation of DLL Exports.
  11.  
  12. // You will need the NT SUR Beta 2 SDK or VC 4.2 or higher in order to build this
  13. // project.  This is because you will need MIDL 3.00.15 or higher and new
  14. // headers and libs.  If you have VC 4.2 or higher installed, then everything should
  15. // already be configured correctly.
  16.  
  17. // Note: Proxy/Stub Information
  18. //      To build a separate proxy/stub DLL,
  19. //      run nmake -f ADOSampps.mak in the project directory.
  20.  
  21. #include "stdafx.h"
  22. #include "resource.h"
  23. #include "initguid.h"
  24. #include "ADOTier.h"
  25.  
  26. #define IID_DEFINED
  27. #include "ADOSamp_i.c"
  28.  
  29. #ifdef _USRDLL
  30. CComModule _Module;
  31. #else // _USRDLL
  32.  
  33. LONG CExeModule::Unlock()
  34. {
  35.     LONG l = CComModule::Unlock();
  36.     if (l == 0)
  37.         PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  38.     return l;
  39. }
  40.  
  41. CExeModule _Module;
  42. #endif // !_USRDLL
  43.  
  44. BEGIN_OBJECT_MAP(ObjectMap)
  45.     OBJECT_ENTRY(CLSID_CADOTier, CADOTier)
  46. END_OBJECT_MAP()
  47.  
  48. #ifdef _USRDLL
  49. /////////////////////////////////////////////////////////////////////////////
  50. // DLL Entry Point
  51.  
  52. extern "C"
  53. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  54. {
  55.     if (dwReason == DLL_PROCESS_ATTACH)
  56.     {
  57.         _Module.Init(ObjectMap, hInstance);
  58.         DisableThreadLibraryCalls(hInstance);
  59.     }
  60.     else if (dwReason == DLL_PROCESS_DETACH)
  61.         _Module.Term();
  62.     return TRUE;    // ok
  63. }
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // Used to determine whether the DLL can be unloaded by OLE
  67.  
  68. STDAPI DllCanUnloadNow(void)
  69. {
  70.     return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  71. }
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // Returns a class factory to create an object of the requested type
  75.  
  76. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  77. {
  78.     return _Module.GetClassObject(rclsid, riid, ppv);
  79. }
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // DllRegisterServer - Adds entries to the system registry
  83.  
  84. STDAPI DllRegisterServer(void)
  85. {
  86.     // registers object, typelib and all interfaces in typelib
  87.     return _Module.RegisterServer(TRUE);
  88. }
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // DllUnregisterServer - Removes entries from the system registry
  92.  
  93. STDAPI DllUnregisterServer(void)
  94. {
  95.     _Module.UnregisterServer();
  96.     return S_OK;
  97. }
  98.  
  99. #else // _USRDLL
  100.  
  101. LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
  102. {
  103.     while (*p1 != NULL)
  104.     {
  105.         LPCTSTR p = p2;
  106.         while (*p != NULL)
  107.         {
  108.             if (*p1 == *p++)
  109.                 return p1+1;
  110.         }
  111.         p1++;
  112.     }
  113.     return NULL;
  114. }
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117. //
  118. extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
  119.     HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
  120. {
  121.     lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
  122.     HRESULT hRes = CoInitialize(NULL);
  123. //  If you are running on NT 4.0 or higher you can use the following call
  124. //  instead to make the EXE free threaded.
  125. //  This means that calls come in on a random RPC thread
  126. //  HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  127.     _ASSERTE(SUCCEEDED(hRes));
  128.     _Module.Init(ObjectMap, hInstance);
  129.     _Module.dwThreadID = GetCurrentThreadId();
  130.     TCHAR szTokens[] = _T("-/");
  131.  
  132.     int nRet = 0;
  133.     BOOL bRun = TRUE;
  134.     LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
  135.     while (lpszToken != NULL)
  136.     {
  137.         if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
  138.         {
  139.             _Module.UpdateRegistryFromResource(IDR_ADOSamp, FALSE);
  140.             nRet = _Module.UnregisterServer();
  141.             bRun = FALSE;
  142.             break;
  143.         }
  144.         if (lstrcmpi(lpszToken, _T("RegServer"))==0)
  145.         {
  146.             _Module.UpdateRegistryFromResource(IDR_ADOSamp, TRUE);
  147.             nRet = _Module.RegisterServer(TRUE);
  148.             bRun = FALSE;
  149.             break;
  150.         }
  151.         lpszToken = FindOneOf(lpszToken, szTokens);
  152.     }
  153.  
  154.     if (bRun)
  155.     {
  156.         hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
  157.             REGCLS_MULTIPLEUSE);
  158.         _ASSERTE(SUCCEEDED(hRes));
  159.  
  160.         MSG msg;
  161.         while (GetMessage(&msg, 0, 0, 0))
  162.             DispatchMessage(&msg);
  163.  
  164.         _Module.RevokeClassObjects();
  165.     }
  166.  
  167.     CoUninitialize();
  168.     return nRet;
  169. }
  170. #endif // !_USRDLL
  171.