home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / atltangram / atlmodel / atlmodel.cpp next >
C/C++ Source or Header  |  1998-03-26  |  4KB  |  164 lines

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