UTIL.H
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:22k
源码类别:

Windows编程

开发平台:

Visual C++

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