home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / labrador / labdriv / labdriv.cpp next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  2.6 KB  |  112 lines

  1. // labdriv.cpp : driver for the Labrador sample
  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. #include "prelabdr.h"
  14.  
  15. ///////////////////////////////////////////////////////////////
  16.  
  17. // helper to do print traces
  18. void _cdecl Trace(LPCTSTR lpszFormat, ...)
  19. {
  20.     va_list args;
  21.     va_start(args, lpszFormat);
  22.  
  23.     int nBuf;
  24.     TCHAR szBuffer[512];
  25.  
  26.     nBuf = _vstprintf(szBuffer, lpszFormat, args);
  27.     _ASSERT(nBuf < sizeof(szBuffer));
  28.  
  29.     _tprintf(szBuffer);
  30.     OutputDebugString(szBuffer);
  31.     va_end(args);
  32. }
  33.  
  34. // helper function to do the work
  35. void _cdecl CallLabrador()
  36. {
  37.     WCHAR szTmp[32];
  38.  
  39.     Trace(_T("\nSTARTING\n=============================\n"));
  40.     Trace(_T("Calling CoCreateInstance()...\n"));
  41.     IUnknown* pUnk = NULL;
  42.     HRESULT hRes = CoCreateInstance(CLSID_Labrador, NULL, CLSCTX_LOCAL_SERVER,
  43.         IID_IUnknown, (void**)&pUnk);
  44.     if (FAILED(hRes))
  45.     {
  46.         Trace(_T("Failed to create Labrador\n"));
  47.         return;
  48.     }
  49.     Trace(_T("Object created\n"));
  50.  
  51.     IMammal* pMammal = NULL;
  52.     hRes = pUnk->QueryInterface(IID_IMammal, (LPVOID*)&pMammal);
  53.     pUnk->Release();
  54.     if (FAILED(hRes))
  55.     {
  56.         Trace(_T("QueryInterface() for IMammal failed\n"));
  57.         return;
  58.     }
  59.  
  60.     Trace(_T("Calling through IMammal methods...\n"));
  61.     pMammal->GetSpeciesName(szTmp);
  62.     Trace(_T("Species name is <%ls>\n"), szTmp);
  63.     BOOL bIsAlive;
  64.     pMammal->IsAlive(&bIsAlive);
  65.     if (bIsAlive)
  66.         Trace(_T("And it's alive!\n"));
  67.     else
  68.         Trace(_T("And it's dead!\n"));
  69.  
  70.     IDog* pDog = NULL;
  71.     hRes = pMammal->QueryInterface(IID_IDog, (void**)&pDog);
  72.     pMammal->Release();
  73.     if (FAILED(hRes))
  74.     {
  75.         Trace(_T("QueryInterface() for IDog failed\n"));
  76.         return;
  77.     }
  78.  
  79.     Trace(_T("Calling through IDog methods...\n"));
  80.     BOOL bIsBarking;
  81.     pDog->GetPetName(szTmp);
  82.     Trace(_T("Dog's name is <%ls>\n"), szTmp);
  83.  
  84.     pDog->IsBarking(&bIsBarking);
  85.     if (bIsBarking)
  86.         printf("BARK! BARK! BARK! BARK!\n");
  87.  
  88.     pDog->SetPetName(L"KIVA");
  89.  
  90.     pDog->GetPetName(szTmp);
  91.     printf("Dog's New name is <%ls>\n", szTmp);
  92.  
  93.     Trace(_T("Releasing Object\n"));
  94.     pDog->Release();
  95.     Trace(_T("\nDONE!!!\n=============================\n"));
  96. }
  97.  
  98. int main( int argc, char *argv[ ])
  99. {
  100.  
  101.     if (FAILED(CoInitialize(NULL)))
  102.         return -1;
  103.  
  104.     CallLabrador();
  105.  
  106. #ifdef _DEBUG
  107.     _CrtDumpMemoryLeaks();
  108. #endif
  109.     CoUninitialize();
  110.     return 0;
  111. }
  112.