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

  1. // util.cpp - Common utilities for printing out messages
  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 <objbase.h>
  14. #include <stdio.h> //sprintf..
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17.  
  18. #include "util.h"
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. //
  22.  
  23. void ErrorMessage(LPCTSTR message, HRESULT hr)
  24. {
  25.     const char* sz ;
  26.     if (message == NULL)
  27.         sz = "The following error occured." ;
  28.     else
  29.         sz = message;
  30.  
  31.     void* pMsgBuf;
  32.  
  33.     ::FormatMessage(
  34.          FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  35.          NULL,
  36.          hr,
  37.          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  38.          (LPTSTR) &pMsgBuf,
  39.          0,
  40.          NULL
  41.     );
  42.  
  43.     TCHAR buf[1024];
  44.     wsprintf(buf, "%s\r\nError: (0x%x) - %s", sz, hr, (LPTSTR) pMsgBuf);
  45.  
  46.     MessageBox(NULL, buf, "Utility Error Message Box.", MB_OK);
  47.  
  48.     // Free the buffer.
  49.     LocalFree(pMsgBuf);
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. //  Check to see if both interfaces are on the same component.
  54.  
  55. BOOL InterfacesAreOnSameComponent(IUnknown* p1, IUnknown* p2)
  56. {
  57.     HRESULT hr = S_OK ;
  58.  
  59.     // Get the real IUnknown for the first interface.
  60.     IUnknown* pReal1 = NULL ;
  61.     hr = p1->QueryInterface(IID_IUnknown, (void**)&pReal1) ;
  62.     assert(SUCCEEDED(hr)) ;
  63.  
  64.     // Get the real IUnknown for the second interface.
  65.     IUnknown* pReal2 = NULL ;
  66.     hr = p2->QueryInterface(IID_IUnknown, (void**)&pReal2) ;
  67.     assert(SUCCEEDED(hr)) ;
  68.  
  69.     // Compare the IUnknown pointers.
  70.     BOOL bReturn = (pReal1 == pReal2) ;
  71.  
  72.     // Cleanup
  73.     pReal1->Release() ;
  74.     pReal2->Release() ;
  75.  
  76.     // Return the value.
  77.     return bReturn;
  78. }
  79.  
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. //  IsValidAddress
  83.  
  84. BOOL IsValidAddress(const void* lp, UINT nBytes, BOOL bReadWrite)
  85. {
  86.     return (lp != NULL && !::IsBadReadPtr(lp, nBytes) &&
  87.         (!bReadWrite || !::IsBadWritePtr((LPVOID)lp, nBytes)));
  88. }
  89.  
  90.  
  91. ///////////////////////////////////////////////////////////
  92. //
  93. //  Trace
  94. //
  95. void __cdecl MyTrace(LPCTSTR lpszFormat, ...)
  96. {
  97.     va_list args;
  98.     va_start(args, lpszFormat);
  99.  
  100.     int nBuf;
  101.     TCHAR szBuffer[512];
  102.  
  103.     nBuf = _vsnprintf(szBuffer, sizeof(szBuffer)*sizeof(TCHAR), lpszFormat, args);
  104.  
  105.     // was there an error? was the expanded string too long?
  106.     assert(nBuf > 0);
  107.  
  108.     OutputDebugString(szBuffer) ;
  109.  
  110.     va_end(args);
  111. }
  112.