home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / dcom / atlcont / atlcont.cpp < prev    next >
C/C++ Source or Header  |  1998-04-02  |  3KB  |  116 lines

  1. // AtlCont.cpp : Implementation of WinMain
  2.  
  3. // You will need the NT SUR Beta 2 SDK or VC 4.2 or higher in order to build 
  4. // this project.  This is because you will need MIDL 3.00.15 or higher and new
  5. // headers and libs.  If you have VC 4.2 installed, then everything should
  6. // already be configured correctly.
  7.  
  8. // Note: Proxy/Stub Information
  9. //      To build a separate proxy/stub DLL, 
  10. //      run nmake -f AtlContps.mak in the project directory.
  11.  
  12. #include "stdafx.h"
  13. #include "resource.h"
  14. #include "initguid.h"
  15. #include "AtlCont.h"
  16.  
  17. #define IID_DEFINED
  18. #include "AtlCont_i.c"
  19. #include "AtlCont1.h"
  20.  
  21.  
  22. LONG CExeModule::Unlock()
  23. {
  24.     LONG l = CComModule::Unlock();
  25.     if (l == 0)
  26.         PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  27.     return l;
  28. }
  29.  
  30. CExeModule _Module;
  31.  
  32. BEGIN_OBJECT_MAP(ObjectMap)
  33.     OBJECT_ENTRY(CLSID_CAtlCont, CAtlCont)
  34. END_OBJECT_MAP()
  35.  
  36.  
  37. LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
  38. {
  39.     while (*p1 != NULL)
  40.     {
  41.         LPCTSTR p = p2;
  42.         while (*p != NULL)
  43.         {
  44.             if (*p1 == *p++)
  45.                 return p1+1;
  46.         }
  47.         p1++;
  48.     }
  49.     return NULL;
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. //
  54. extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, 
  55.     HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
  56. {
  57.     lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
  58.     HRESULT hRes = CoInitialize(NULL);
  59.     _ASSERTE(SUCCEEDED(hRes));
  60.     hRes = CoInitializeSecurity(NULL, -1, NULL, NULL,
  61.             RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
  62.     _ASSERTE(SUCCEEDED(hRes));
  63.  
  64.     _Module.Init(ObjectMap, hInstance);
  65.     _Module.dwThreadID = GetCurrentThreadId();
  66.     TCHAR szTokens[] = _T("-/");
  67.  
  68.     int nRet = 0;
  69.     BOOL bRun = TRUE;
  70.     LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
  71.     while (lpszToken != NULL)
  72.     {
  73.         if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
  74.         {
  75.             _Module.UpdateRegistryFromResource(IDR_AtlCont, FALSE);
  76.             nRet = _Module.UnregisterServer();
  77.             bRun = FALSE;
  78.             break;
  79.         }
  80.         if (lstrcmpi(lpszToken, _T("RegServer"))==0)
  81.         {
  82.             _Module.UpdateRegistryFromResource(IDR_AtlCont, TRUE);
  83.             nRet = _Module.RegisterServer(TRUE);
  84.             bRun = FALSE;
  85.             break;
  86.         }
  87.         lpszToken = FindOneOf(lpszToken, szTokens);
  88.     }
  89.  
  90.     if (bRun)
  91.     {
  92.         hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, 
  93.             REGCLS_MULTIPLEUSE);
  94.         _ASSERTE(SUCCEEDED(hRes));
  95.  
  96.         CComPtr<IAtlCont> pApp;
  97.         CoCreateInstance(CLSID_CAtlCont, NULL, CLSCTX_INPROC_SERVER, IID_IAtlCont, (void**)&pApp);
  98.         if (pApp)
  99.             pApp->Run();
  100.  
  101.         MSG msg;
  102.         while (GetMessage(&msg, 0, 0, 0))
  103.         {
  104.             TranslateMessage(&msg);
  105.             DispatchMessage(&msg);
  106.         }
  107.  
  108.         _Module.RevokeClassObjects();
  109.     }
  110.     CoUninitialize();
  111. #ifdef _ATL_MIN_CRT
  112.     ExitProcess(0);
  113. #endif
  114.     return nRet;
  115. }
  116.