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

  1. // Drive.cpp : Implementation of WinMain
  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. // You will need the NT SUR Beta 2 SDK or VC 4.2 in order to build this
  14. // project.  This is because you will need MIDL 3.00.15 or higher and new
  15. // headers and libs.  If you have VC 4.2 installed, then everything should
  16. // already be configured correctly.
  17.  
  18. #include "predrive.h"
  19. #include "initguid.h"
  20. #include "..\connect.h"
  21.  
  22. #define IID_DEFINED
  23. #include "..\connect_i.c"
  24.  
  25. CComModule _Module;
  26.  
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CDriver
  30.  
  31. class CDriver :
  32.     public IDispatchImpl<IRandomEvent, &IID_IRandomEvent, &LIBID_CONNECTLib>,
  33.     public CComObjectRoot
  34. {
  35. public:
  36.     CDriver() {}
  37. BEGIN_COM_MAP(CDriver)
  38.     COM_INTERFACE_ENTRY(IDispatch)
  39.     COM_INTERFACE_ENTRY(IRandomEvent)
  40. END_COM_MAP()
  41.  
  42. // IRandomEvent
  43.     STDMETHOD(Fire)(long l)
  44.     {
  45.         _tprintf(_T("%d\n"), l);
  46.         return S_OK;
  47.     }
  48. };
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. //
  52.  
  53. extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  54.     LPTSTR lpCmdLine, int nShowCmd)
  55. {
  56.     HRESULT hRes = CoInitialize(NULL);
  57. //  If you are running on NT 4.0 or higher you can use the following call
  58. //  instead to make the EXE free threaded.
  59. //  This means that calls come in on a random RPC thread
  60. //  HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  61.     ATLASSERT(SUCCEEDED(hRes));
  62.     _Module.Init(NULL, hInstance);
  63.  
  64.     CComObject<CDriver>* pDriver;
  65.     IRandom* pM;
  66.     CoCreateInstance(CLSID_CoRandom, NULL, CLSCTX_ALL, IID_IRandom, (void**)&pM);
  67.     ATLASSERT(pM != NULL);
  68.  
  69.     long nID;
  70.     pM->Start(&nID);
  71.     DWORD dwTick = GetTickCount();
  72.     while (GetTickCount()-dwTick < 3000)
  73.     {
  74.         CComObject<CDriver>::CreateInstance(&pDriver);
  75.         DWORD dwAdvise = 0;
  76.         puts("Connect");
  77.         hRes = AtlAdvise(pM, pDriver->GetUnknown(), IID_IRandomEvent, &dwAdvise);
  78.         Sleep(1);
  79.         AtlUnadvise(pM, IID_IRandomEvent, dwAdvise);
  80.         puts("Disconnect");
  81.         Sleep(1);
  82.     }
  83.     pM->Stop(nID);
  84.  
  85.     pM->Release();
  86.  
  87.     CoUninitialize();
  88.     return 0;
  89. }
  90.