home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / internet / scripting / spruuids / helpers.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-12  |  4.2 KB  |  102 lines

  1. //---------------------------------------------------------------------------
  2. // Helpers.h
  3. //---------------------------------------------------------------------------
  4. // General OLE Automation helper functions.
  5. //---------------------------------------------------------------------------
  6. // (C) Copyright 1995-1997 by Microsoft Corporation.  All rights reserved.
  7. //
  8. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
  9. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  10. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
  11. // PARTICULAR PURPOSE.
  12. //---------------------------------------------------------------------------
  13.  
  14.  
  15. //---------------------------------------------------------------------------
  16. // To make the OLE-related code easier to read
  17. //---------------------------------------------------------------------------
  18. #define CHECK(x)       { hr = (x);  if (hr) { FAIL("Bad hr"); goto CleanUp; } }
  19. #define CHECKSZ(x,sz)  { hr = (x);  if (hr) { FAIL(sz);       goto CleanUp; } }
  20. #define CHECKCL(x)     { hr = (x);  if (hr) { FAIL("Bad hr"); goto CheckLoop; } }
  21. #define ARGSZ(f,sz)    {            if (f)  { FAIL(sz);       return E_INVALIDARG; } }
  22. #define CHECKOS(x) { if (ERROR_SUCCESS!=(x)) { FAIL("Bad"); hr=E_FAIL; goto CleanUp; } }
  23.  
  24. #define RELEASEPTRTYPE(p,typ) \
  25.   {                           \
  26.     if (p)                    \
  27.       {                       \
  28.       typ *__punk = (p);      \
  29.       (p) = NULL;             \
  30.       __punk->Release();      \
  31.       }                       \
  32.   }
  33.  
  34. #define RELEASEPTR(p)     RELEASEPTRTYPE(p,IUnknown)
  35.  
  36.  
  37. //---------------------------------------------------------------------------
  38. // Allocates a temporary buffer that will disappear when it goes out of scope
  39. // NOTE: Be careful of that-- make sure you use the string in the same or
  40. // nested scope in which you created this buffer.  People should not use this
  41. // class directly; use the macro(s) below.
  42. //---------------------------------------------------------------------------
  43. class TempBuffer
  44.   {
  45.   public:
  46.     TempBuffer(ULONG cb)
  47.       {
  48.       m_fAlloc = (cb > 120);
  49.       if (m_fAlloc)
  50.         m_pbBuf = new char[cb];
  51.       else
  52.         m_pbBuf = &m_szBufT;
  53.       }
  54.     ~TempBuffer()
  55.       { if (m_pbBuf && m_fAlloc) delete m_pbBuf; }
  56.     void *GetBuffer(void)
  57.       { return m_pbBuf; }
  58.  
  59.   private:
  60.     void *m_pbBuf;
  61.     char  m_szBufT[120];  // We'll use this temp buffer for small cases.
  62.     int   m_fAlloc;
  63.   };
  64.  
  65.  
  66. //---------------------------------------------------------------------------
  67. // String helpers.
  68. //---------------------------------------------------------------------------
  69. // Given and ANSI String, copy it into a wide buffer.
  70. // NOTE: Be careful about scoping when using this macro!
  71. //
  72. // How to use the below two macros:
  73. //
  74. //  ...
  75. //  LPSTR pszA;
  76. //  pszA = MyGetpszAnsiingRoutine();
  77. //  MAKE_WIDEPTR_FROMANSI(pwsz, pszA);
  78. //  MyUseWideStringRoutine(pwsz);
  79. //  ...
  80. //
  81. // Similarily for MAKE_ANSIPTR_FROMWIDE().  Note that the first param does
  82. // not have to be declared, and no clean up must be done.
  83. //---------------------------------------------------------------------------
  84. #define UNICODE_FROM_ANSI(pwszUnicode, pszAnsi, cb) \
  85.     MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, pwszUnicode, cb);
  86.  
  87. #define MAKE_WIDEPTR_FROMANSI(ptrname, pszAnsi) \
  88.     char *__psz##ptrname = pszAnsi?pszAnsi:""; \
  89.     long __l##ptrname = (lstrlen(__psz##ptrname) + 1) * sizeof(WCHAR); \
  90.     TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  91.     MultiByteToWideChar(CP_ACP, 0, __psz##ptrname, -1, (LPWSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname); \
  92.     LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
  93.  
  94. #define MAKE_ANSIPTR_FROMWIDE(ptrname, pwszUnicode) \
  95.     WCHAR *__pwsz##ptrname = pwszUnicode?pwszUnicode:L""; \
  96.     long __l##ptrname = (lstrlenW(__pwsz##ptrname) + 1) * sizeof(char); \
  97.     TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  98.     WideCharToMultiByte(CP_ACP, 0, __pwsz##ptrname, -1, (LPSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname, NULL, NULL); \
  99.     LPSTR ptrname = (LPSTR)__TempBuffer##ptrname.GetBuffer()
  100.  
  101. //--- EOF -------------------------------------------------------------------
  102.