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

  1. // stlcoll.cpp : Implementation of WinMain
  2.  
  3. // Copyright (C) 1992-1998 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6. // This source code is only intended as a supplement to the
  7. // Microsoft Visual C++ Language  Reference and related
  8. // electronic documentation provided with Microsoft Visual C++.
  9. // See these sources for detailed information regarding the
  10. // Microsoft Visual C++ product.
  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 stlcollps.mak in the project directory.
  20.  
  21. #include "stdafx.h"
  22. #include "resource.h"
  23. #include "initguid.h"
  24. #include "Stlcoll_.h"
  25.  
  26. #define IID_DEFINED
  27. #include "stlcoll_i.c"
  28.  
  29. #ifndef _USRDLL
  30.  
  31. LONG CExeModule::Unlock()
  32. {
  33.     LONG l = CComModule::Unlock();
  34.     if (l == 0)
  35.         PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  36.     return l;
  37. }
  38.  
  39. CExeModule _Module;
  40. #else // _USRDLL
  41. CComModule _Module;
  42. #endif // _USRDLL
  43.  
  44. BEGIN_OBJECT_MAP(ObjectMap)
  45.     OBJECT_ENTRY(CLSID_CStlMaps, CStlMaps)
  46.     OBJECT_ENTRY(CLSID_CStlMapStringToMyObject, CStlMapStringToMyObject)
  47.     OBJECT_ENTRY(CLSID_CStlMapDWordToMyStruct, CStlMapDWordToMyStruct)
  48.     OBJECT_ENTRY(CLSID_CStlMapStringToString, CStlMapStringToString)
  49.     OBJECT_ENTRY(CLSID_CStlLists, CStlLists)
  50.     OBJECT_ENTRY(CLSID_CStlArrays, CStlArrays)
  51.     OBJECT_ENTRY(CLSID_CStlDWordArray, CStlDWordArray)
  52. END_OBJECT_MAP()
  53.  
  54. #ifndef _USRDLL
  55.  
  56. LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
  57. {
  58.     while (*p1 != NULL)
  59.     {
  60.         LPCTSTR p = p2;
  61.         while (*p != NULL)
  62.         {
  63.             if (*p1 == *p++)
  64.                 return p1+1;
  65.         }
  66.         p1++;
  67.     }
  68.     return NULL;
  69. }
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. //
  73. extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
  74.     HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
  75. {
  76.     lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
  77.     HRESULT hRes = CoInitialize(NULL);
  78. //  If you are running on NT 4.0 or higher you can use the following call
  79. //  instead to make the EXE free threaded.
  80. //  This means that calls come in on a random RPC thread
  81. //  HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  82.     _ASSERTE(SUCCEEDED(hRes));
  83.     _Module.Init(ObjectMap, hInstance);
  84.     _Module.dwThreadID = GetCurrentThreadId();
  85.     TCHAR szTokens[] = _T("-/");
  86.  
  87.     int nRet = 0;
  88.     BOOL bRun = TRUE;
  89.     LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
  90.     while (lpszToken != NULL)
  91.     {
  92.         if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
  93.         {
  94.             _Module.UpdateRegistryFromResource(IDR_Stlcoll, FALSE);
  95.             nRet = _Module.UnregisterServer();
  96.             bRun = FALSE;
  97.             break;
  98.         }
  99.         if (lstrcmpi(lpszToken, _T("RegServer"))==0)
  100.         {
  101.             _Module.UpdateRegistryFromResource(IDR_Stlcoll, TRUE);
  102.             nRet = _Module.RegisterServer(TRUE);
  103.             bRun = FALSE;
  104.             break;
  105.         }
  106.         lpszToken = FindOneOf(lpszToken, szTokens);
  107.     }
  108.  
  109.     if (bRun)
  110.     {
  111.         hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
  112.             REGCLS_MULTIPLEUSE);
  113.         _ASSERTE(SUCCEEDED(hRes));
  114.  
  115.         MSG msg;
  116.         while (GetMessage(&msg, 0, 0, 0))
  117.             DispatchMessage(&msg);
  118.  
  119.         _Module.RevokeClassObjects();
  120.     }
  121.  
  122.     CoUninitialize();
  123.     return nRet;
  124. }
  125.  
  126. #else // _USRDLL
  127.  
  128. class CStlcollApp : public CWinApp
  129. {
  130. public:
  131.     virtual BOOL InitInstance();
  132.     virtual int ExitInstance();
  133. };
  134.  
  135. CStlcollApp theApp;
  136.  
  137. BOOL CStlcollApp::InitInstance()
  138. {
  139.     _Module.Init(ObjectMap, m_hInstance);
  140.     return CWinApp::InitInstance();
  141. }
  142.  
  143. int CStlcollApp::ExitInstance()
  144. {
  145.     _Module.Term();
  146.     return CWinApp::ExitInstance();
  147. }
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. // Used to determine whether the DLL can be unloaded by OLE
  151.  
  152. STDAPI DllCanUnloadNow(void)
  153. {
  154.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  155.     return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  156. }
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159. // Returns a class factory to create an object of the requested type
  160.  
  161. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  162. {
  163.     return _Module.GetClassObject(rclsid, riid, ppv);
  164. }
  165.  
  166. /////////////////////////////////////////////////////////////////////////////
  167. // DllRegisterServer - Adds entries to the system registry
  168.  
  169. STDAPI DllRegisterServer(void)
  170. {
  171.     // registers object, typelib and all interfaces in typelib
  172.     return _Module.RegisterServer(TRUE);
  173. }
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176. // DllUnregisterServer - Removes entries from the system registry
  177.  
  178. STDAPI DllUnregisterServer(void)
  179. {
  180.     _Module.UnregisterServer();
  181.     return S_OK;
  182. }
  183.  
  184. #endif //_USRDLL
  185.