home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / adsi / sampprov / util.h < prev   
Encoding:
C/C++ Source or Header  |  1997-07-29  |  21.7 KB  |  507 lines

  1. /*++
  2.  
  3. Copyright (c) 1996 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     Util.h
  8.  
  9. Abstract:
  10.  
  11.     Definition of Utilities for use
  12.  
  13. Author:
  14.  
  15. Environment:
  16.  
  17.     User mode
  18.  
  19. Revision History :
  20.  
  21. --*/
  22. #ifndef _UTIL_H_
  23. #define _UTIL_H_
  24.  
  25. #include "formtrck.h"
  26.  
  27. //+---------------------------------------------------------------------
  28. //
  29. //   Generally useful #defines and inline functions for OLE2.
  30. //
  31. //------------------------------------------------------------------------
  32.  
  33. // These are the major and minor version returned by OleBuildVersion
  34. #define OLE_MAJ_VER 0x0003
  35. #define OLE_MIN_VER 0x003A
  36.  
  37.  
  38. //---------------------------------------------------------------
  39. //  SCODE and HRESULT macros
  40. //---------------------------------------------------------------
  41.  
  42. #define OK(r)       (SUCCEEDED(r))
  43. #define NOTOK(r)    (FAILED(r))
  44.  
  45.  
  46. //---------------------------------------------------------------
  47. //  IUnknown
  48. //---------------------------------------------------------------
  49.  
  50. #define InterlockedIncrement(plong) (++(*(plong)))
  51. #define InterlockedDecrement(plong) (--(*(plong)))
  52.  
  53. #define ADsIncrement(__ul) InterlockedIncrement((long *) &__ul)
  54. #define ADsDecrement(__ul) InterlockedDecrement((long *) &__ul)
  55.  
  56.  
  57.  
  58. #define DECLARE_ADs_IUNKNOWN_METHODS                              \
  59.     STDMETHOD(QueryInterface) (REFIID riid, LPVOID * ppv);          \
  60.     STDMETHOD_(ULONG, AddRef) (void);                               \
  61.     STDMETHOD_(ULONG, Release) (void);
  62.  
  63.  
  64.  
  65. #define DECLARE_ADs_STANDARD_IUNKNOWN(cls)                        \
  66.     STDMETHOD(QueryInterface) (REFIID riid, LPVOID * ppv);          \
  67.     ULONG _ulRefs;                                                  \
  68.     STDMETHOD_(ULONG, AddRef) (void)                                \
  69.         {                                                           \
  70.             ADsIncrement(_ulRefs);                                \
  71.             return _ulRefs;                                         \
  72.         }                                                           \
  73.     STDMETHOD_(ULONG, Release) (void)                               \
  74.         {                                                           \
  75.             if (!ADsDecrement(_ulRefs))                           \
  76.             {                                                       \
  77.                 ADsIncrement(_ulRefs);                            \
  78.                 delete this;                                        \
  79.                 return 0;                                           \
  80.             }                                                       \
  81.             return _ulRefs;                                         \
  82.         }
  83.  
  84.  
  85.  
  86. #define DECLARE_ADs_DELEGATING_IUNKNOWN(cls)                      \
  87.     IUnknown * _pUnkOuter;                                          \
  88.     STDMETHOD(QueryInterface) (REFIID iid, LPVOID * ppv)            \
  89.         { return _pUnkOuter->QueryInterface(iid, ppv); }            \
  90.     STDMETHOD_(ULONG, AddRef) (void)                                \
  91.         { return _pUnkOuter->AddRef(); }                            \
  92.     STDMETHOD_(ULONG, Release) (void)                               \
  93.         { return _pUnkOuter->Release(); }
  94.  
  95.  
  96. #if DBG == 0
  97. //
  98. // Retail versions of these macros
  99. //
  100.  
  101. #define DECLARE_ADs_PRIVATE_IUNKNOWN(cls)                         \
  102.     class PrivateUnknown : public IUnknown                          \
  103.     {                                                               \
  104.     private:                                                        \
  105.         ULONG   _ulRefs;                                            \
  106.         cls *   My##cls(void)                                       \
  107.             { return CONTAINING_RECORD(this, cls, _PrivUnk); }      \
  108.                                                                     \
  109.     public:                                                         \
  110.         PrivateUnknown(void)                                        \
  111.             { _ulRefs = 1; }                                        \
  112.                                                                     \
  113.         DECLARE_ADs_IUNKNOWN_METHODS                              \
  114.     };                                                              \
  115.     friend class PrivateUnknown;                                    \
  116.     PrivateUnknown _PrivUnk;
  117.  
  118.  
  119.  
  120. #define IMPLEMENT_ADs_PRIVATE_IUNKNOWN(cls)                       \
  121.     STDMETHODIMP_(ULONG) cls::PrivateUnknown::AddRef( )             \
  122.     {                                                               \
  123.         ADsIncrement(_ulRefs);                                    \
  124.         return _ulRefs;                                             \
  125.     }                                                               \
  126.     STDMETHODIMP_(ULONG) cls::PrivateUnknown::Release( )            \
  127.     {                                                               \
  128.         if (!ADsDecrement(_ulRefs))                               \
  129.         {                                                           \
  130.             ADsIncrement(_ulRefs);                                \
  131.             delete My##cls();                                       \
  132.             return 0;                                               \
  133.         }                                                           \
  134.         return _ulRefs;                                             \
  135.     }
  136.  
  137.  
  138.  
  139. #define DECLARE_ADs_COMPOUND_IUNKNOWN(cls)                        \
  140.     class PrivateUnknown : public IUnknown                          \
  141.     {                                                               \
  142.         friend class cls;                                           \
  143.                                                                     \
  144.     public:                                                         \
  145.         PrivateUnknown(void)                                        \
  146.             { _ulRefs = 1; _ulAllRefs = 1; }                        \
  147.                                                                     \
  148.         DECLARE_ADs_IUNKNOWN_METHODS                              \
  149.                                                                     \
  150.     private:                                                        \
  151.         ULONG   _ulRefs;                                            \
  152.         ULONG   _ulAllRefs;                                         \
  153.                                                                     \
  154.         cls *   My##cls(void)                                       \
  155.             { return CONTAINING_RECORD(this, cls, _PrivUnk); }      \
  156.     };                                                              \
  157.     friend class PrivateUnknown;                                    \
  158.     PrivateUnknown _PrivUnk;                                        \
  159.                                                                     \
  160.     ULONG   SubAddRef(void);                                        \
  161.     ULONG   SubRelease(void);
  162.  
  163.  
  164.  
  165. #define IMPLEMENT_ADs_COMPOUND_IUNKNOWN(cls)                      \
  166.     STDMETHODIMP_(ULONG) cls::PrivateUnknown::AddRef( )             \
  167.     {                                                               \
  168.         ADsIncrement(_ulAllRefs);                                 \
  169.         ADsIncrement(_ulRefs);                                    \
  170.         return _ulRefs;                                             \
  171.     }                                                               \
  172.     STDMETHODIMP_(ULONG) cls::PrivateUnknown::Release( )            \
  173.     {                                                               \
  174.         if (!ADsDecrement(_ulRefs))                               \
  175.         {                                                           \
  176.             My##cls()->Passivate();                                 \
  177.         }                                                           \
  178.         if (!ADsDecrement(_ulAllRefs))                            \
  179.         {                                                           \
  180.             ADsIncrement(_ulAllRefs);                             \
  181.             delete My##cls();                                       \
  182.             return 0;                                               \
  183.         }                                                           \
  184.         return _ulRefs;                                             \
  185.     }                                                               \
  186.     ULONG cls::SubAddRef( )                                         \
  187.     {                                                               \
  188.         return ADsIncrement(_PrivUnk._ulAllRefs);                 \
  189.     }                                                               \
  190.     ULONG cls::SubRelease( )                                        \
  191.     {                                                               \
  192.         ULONG ul;                                                   \
  193.                                                                     \
  194.         ul = ADsDecrement(_PrivUnk._ulAllRefs);                   \
  195.         if (!ul)                                                    \
  196.         {                                                           \
  197.             ADsIncrement(_PrivUnk._ulAllRefs);                    \
  198.             delete this;                                            \
  199.         }                                                           \
  200.                                                                     \
  201.         return ul;                                                  \
  202.     }
  203.  
  204. #else  // DBG == 0
  205.  
  206. //
  207. // Debug versions of these macros
  208. //
  209.  
  210. #define DECLARE_ADs_PRIVATE_IUNKNOWN(cls)                         \
  211.     class PrivateUnknown : protected ObjectTracker,                 \
  212.                            public IUnknown                          \
  213.     {                                                               \
  214.     private:                                                        \
  215.         cls *   My##cls(void)                                       \
  216.             { return CONTAINING_RECORD(this, cls, _PrivUnk); }      \
  217.                                                                     \
  218.     public:                                                         \
  219.         PrivateUnknown(void)                                        \
  220.             { _ulRefs = 1; TrackClassName(#cls); }                  \
  221.                                                                     \
  222.         DECLARE_ADs_IUNKNOWN_METHODS                              \
  223.     };                                                              \
  224.     friend class PrivateUnknown;                                    \
  225.     PrivateUnknown _PrivUnk;
  226.  
  227.  
  228.  
  229. #define IMPLEMENT_ADs_PRIVATE_IUNKNOWN(cls)                       \
  230.     STDMETHODIMP_(ULONG) cls::PrivateUnknown::AddRef( )             \
  231.     {                                                               \
  232.         StdAddRef();                                                \
  233.         return _ulRefs;                                             \
  234.     }                                                               \
  235.     STDMETHODIMP_(ULONG) cls::PrivateUnknown::Release( )            \
  236.     {                                                               \
  237.         if (!StdRelease())                                          \
  238.         {                                                           \
  239.             ADsIncrement(_ulRefs);                                \
  240.             delete My##cls();                                       \
  241.             return 0;                                               \
  242.         }                                                           \
  243.         return _ulRefs;                                             \
  244.     }
  245.  
  246.  
  247.  
  248. #define DECLARE_ADs_COMPOUND_IUNKNOWN(cls)                        \
  249.     class PrivateUnknown : protected ObjectTracker,                 \
  250.                            public IUnknown                          \
  251.     {                                                               \
  252.         friend class cls;                                           \
  253.                                                                     \
  254.     public:                                                         \
  255.         PrivateUnknown(void)                                        \
  256.             { _ulNRefs = 1; _ulRefs = 1; TrackClassName(#cls); }    \
  257.                                                                     \
  258.         DECLARE_ADs_IUNKNOWN_METHODS                              \
  259.                                                                     \
  260.     private:                                                        \
  261.         ULONG   _ulNRefs;                                           \
  262.                                                                     \
  263.         cls *   My##cls(void)                                       \
  264.             { return CONTAINING_RECORD(this, cls, _PrivUnk); }      \
  265.     };                                                              \
  266.     friend class PrivateUnknown;                                    \
  267.     PrivateUnknown _PrivUnk;                                        \
  268.                                                                     \
  269.     ULONG   SubAddRef(void);                                        \
  270.     ULONG   SubRelease(void);
  271.  
  272.  
  273.  
  274. #define IMPLEMENT_ADs_COMPOUND_IUNKNOWN(cls)                      \
  275.     STDMETHODIMP_(ULONG) cls::PrivateUnknown::AddRef( )             \
  276.     {                                                               \
  277.         StdAddRef();                                                \
  278.         ADsIncrement(_ulNRefs);                                   \
  279.         return _ulNRefs;                                            \
  280.     }                                                               \
  281.     STDMETHODIMP_(ULONG) cls::PrivateUnknown::Release( )            \
  282.     {                                                               \
  283.         if (!ADsDecrement(_ulNRefs))                              \
  284.         {                                                           \
  285.             My##cls()->Passivate();                                 \
  286.         }                                                           \
  287.         if (!StdRelease())                                          \
  288.         {                                                           \
  289.             ADsIncrement(_ulRefs);                                \
  290.             delete My##cls();                                       \
  291.             return 0;                                               \
  292.         }                                                           \
  293.         return _ulNRefs;                                            \
  294.     }                                                               \
  295.     ULONG cls::SubAddRef( )                                         \
  296.     {                                                               \
  297.         return _PrivUnk.StdAddRef();                                \
  298.     }                                                               \
  299.     ULONG cls::SubRelease( )                                        \
  300.     {                                                               \
  301.         ULONG ul;                                                   \
  302.                                                                     \
  303.         ul = _PrivUnk.StdRelease();                                 \
  304.         if (!ul)                                                    \
  305.         {                                                           \
  306.             ADsIncrement(_PrivUnk._ulRefs);                       \
  307.             delete this;                                            \
  308.         }                                                           \
  309.                                                                     \
  310.         return ul;                                                  \
  311.     }
  312.  
  313. #endif // DBG == 0
  314.  
  315. #define DECLARE_ADs_SUBOBJECT_IUNKNOWN(cls, parent_cls, member)   \
  316.     DECLARE_ADs_IUNKNOWN_METHODS                                  \
  317.     parent_cls * My##parent_cls(void);                              \
  318.     void Detach(void)                                               \
  319.         { ; }
  320.  
  321.  
  322. #define IMPLEMENT_ADs_SUBOBJECT_IUNKNOWN(cls, parent_cls, member) \
  323.     inline parent_cls * cls::My##parent_cls(void)                   \
  324.     {                                                               \
  325.         return CONTAINING_RECORD(this, parent_cls, member);         \
  326.     }                                                               \
  327.     STDMETHODIMP_(ULONG) cls::AddRef( )                             \
  328.         { return My##parent_cls()->SubAddRef(); }                   \
  329.     STDMETHODIMP_(ULONG) cls::Release( )                            \
  330.         { return My##parent_cls()->SubRelease(); }
  331.  
  332.  
  333.  
  334. //+------------------------------------------------------------------------
  335. //
  336. //  NO_COPY *declares* the constructors and assignment operator for copying.
  337. //  By not *defining* these functions, you can prevent your class from
  338. //  accidentally being copied or assigned -- you will be notified by
  339. //  a linkage error.
  340. //
  341. //-------------------------------------------------------------------------
  342.  
  343. #define NO_COPY(cls)    \
  344.     cls(const cls&);    \
  345.     cls& operator=(const cls&);
  346.  
  347.  
  348. //+---------------------------------------------------------------------
  349. //
  350. //  Miscellaneous useful OLE helper and debugging functions
  351. //
  352. //----------------------------------------------------------------------
  353.  
  354. //
  355. //  Some convenient OLE-related definitions and declarations
  356. //
  357.  
  358. typedef  unsigned short far * LPUSHORT;
  359.  
  360. #define OLEMISC_STREAMABLE 1024
  361.  
  362. IsCompatibleOleVersion(WORD wMaj, WORD wMin);
  363.  
  364.  
  365. //#if DBG == 1
  366. #if 0
  367. STDAPI  CheckAndReturnResult(
  368.                 HRESULT hr,
  369.                 LPSTR   lpstrFile,
  370.                 UINT    line,
  371.                 int     cSuccess,
  372.                 ...);
  373.  
  374. STDAPI_(void)   CheckResult(HRESULT hr, LPSTR lpstrFile, UINT line);
  375. STDAPI_(void)   PrintIID(DWORD dwFlags, REFIID riid);
  376. STDAPI          PrintHRESULT(DWORD dwFlags, HRESULT hr);
  377.  
  378. #define SRETURN(hr) \
  379.     return CheckAndReturnResult((hr), __FILE__, __LINE__, -1)
  380. #define RRETURN(hr) \
  381.     return CheckAndReturnResult((hr), __FILE__, __LINE__, 0)
  382. #define RRETURN1(hr, s1) \
  383.     return CheckAndReturnResult((hr), __FILE__, __LINE__, 1, (s1))
  384. #define RRETURN2(hr, s1, s2) \
  385.     return CheckAndReturnResult((hr), __FILE__, __LINE__, 2, (s1), (s2))
  386. #define RRETURN3(hr, s1, s2, s3) \
  387.     return CheckAndReturnResult((hr), __FILE__, __LINE__, 3, (s1), (s2), (s3))
  388.  
  389. #define WARN_ERROR(hr)  CheckResult((hr), __FILE__, __LINE__)
  390.  
  391. #define TRETURN(hr)         return PrintHRESULT(DEB_TRACE, (hr))
  392. #define TRACEIID(iid)       PrintIID(DEB_TRACE, iid)
  393. #define TRACEHRESULT(hr)    PrintHRESULT(DEB_TRACE, (hr))
  394.  
  395. #else   // DBG == 0
  396.  
  397. #define SRETURN(hr)                 return (hr)
  398. #define RRETURN(hr)                 return (hr)
  399. #define RRETURN1(hr, s1)            return (hr)
  400. #define RRETURN2(hr, s1, s2)        return (hr)
  401. #define RRETURN3(hr, s1, s2, s3)    return (hr)
  402.  
  403. #define WARN_ERROR(hr)
  404.  
  405. #define TRETURN(hr)     return (hr)
  406. #define TRACEIID(iid)
  407. #define TRACEHRESULT(hr)
  408.  
  409. #endif  // DBG
  410.  
  411.  
  412.  
  413. //+---------------------------------------------------------------------
  414. //
  415. //  Interface wrapper for tracing method invocations
  416. //
  417. //----------------------------------------------------------------------
  418.  
  419. #if DBG == 1
  420.  
  421. LPVOID WatchInterface(REFIID riid, LPVOID pv, LPWSTR lpstr);
  422. #define WATCHINTERFACE(iid, p, lpstr)  WatchInterface(iid, p, lpstr)
  423.  
  424. #else   // DBG == 0
  425.  
  426. #define WATCHINTERFACE(iid, p, lpstr)  (p)
  427.  
  428. #endif  // DBG
  429.  
  430.  
  431. //+---------------------------------------------------------------------
  432. //
  433. //  Standard IClassFactory implementation
  434. //
  435. //----------------------------------------------------------------------
  436.  
  437. //+---------------------------------------------------------------
  438. //
  439. //  Class:      StdClassFactory
  440. //
  441. //  Purpose:    Standard implementation of a class factory object
  442. //
  443. //  Notes:          **************!!!!!!!!!!!!!!!!!*************
  444. //              TAKE NOTE --- The implementation of Release on this
  445. //              class does not perform a delete.  This is so you can
  446. //              make the class factory a global static variable.
  447. //              Use the CDynamicCF class below for an object
  448. //              which is not global static data.
  449. //
  450. //              ALSO - The refcount is initialized to 0, NOT 1!
  451. //
  452. //---------------------------------------------------------------
  453.  
  454. class StdClassFactory: public IClassFactory
  455. {
  456. public:
  457.     StdClassFactory(void) : _ulRefs(1) {};
  458.  
  459.     // IUnknown methods
  460.     DECLARE_ADs_IUNKNOWN_METHODS;
  461.  
  462.     // IClassFactory methods
  463.     STDMETHOD(LockServer) (BOOL fLock);
  464.  
  465.     // CreateInstance is left pure virtual.
  466.  
  467. protected:
  468.     ULONG _ulRefs;
  469. };
  470.  
  471.  
  472.  
  473. //+---------------------------------------------------------------------------
  474. //
  475. //  Class:      CDynamicCF (DYNCF)
  476. //
  477. //  Purpose:    Class factory which exists on the heap, and whose Release
  478. //              method does the normal thing.
  479. //
  480. //  Interface:  DECLARE_ADs_STANDARD_IUNKNOWN -- IUnknown methods
  481. //
  482. //              LockServer             -- Per IClassFactory.
  483. //              CDynamicCF             -- ctor.
  484. //              ~CDynamicCF            -- dtor.
  485. //
  486. //  History:    6-22-94   adams   Created
  487. //              7-13-94   adams   Moved from ADs\inc\dyncf.hxx
  488. //
  489. //----------------------------------------------------------------------------
  490.  
  491. class CDynamicCF: public IClassFactory
  492. {
  493. public:
  494.     // IUnknown methods
  495.     DECLARE_ADs_STANDARD_IUNKNOWN(CDynamicCF)
  496.  
  497.     // IClassFactory methods
  498.     STDMETHOD(LockServer) (BOOL fLock);
  499.  
  500. protected:
  501.             CDynamicCF(void);
  502.     virtual ~CDynamicCF(void);
  503. };
  504.  
  505.  
  506. #endif //__UTILS_H_
  507.