home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / oleaut / lines / point.cpp < prev    next >
C/C++ Source or Header  |  1997-07-31  |  6KB  |  323 lines

  1. /*************************************************************************
  2. **
  3. **  This is a part of the Microsoft Source Code Samples.
  4. **
  5. **  Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  6. **
  7. **  This source code is only intended as a supplement to Microsoft Development
  8. **  Tools and/or WinHelp documentation.  See these sources for detailed
  9. **  information regarding the Microsoft samples programs.
  10. **
  11. **  OLE Automation Points Object.
  12. **
  13. **  point.cpp
  14. **
  15. **  CPoint implementation
  16. **
  17. **  Written by Microsoft Product Support Services, Windows Developer Support
  18. **
  19. *************************************************************************/
  20.  
  21. #include <windows.h>
  22. #include <windowsx.h>
  23. #ifdef WIN16   
  24.   #include <ole2.h>
  25.   #include <compobj.h>    
  26.   #include <dispatch.h> 
  27.   #include <variant.h>
  28.   #include <olenls.h>  
  29. #endif      
  30. #include "lines.h"  
  31.  
  32. /*
  33.  * CPoint::Create
  34.  *
  35.  * Purpose:
  36.  *  Creates an instance of the Point automation object and initializes it.
  37.  *
  38.  * Parameters:
  39.  *  ppPoint    Returns Point automation object.
  40.  *
  41.  * Return Value:
  42.  *  HRESULT
  43.  *
  44.  */
  45. HRESULT 
  46. CPoint::Create(CPoint FAR* FAR* ppPoint ) 
  47. {   
  48.     HRESULT hr;
  49.     CPoint FAR* pPoint = NULL;
  50.      
  51.     *ppPoint = NULL;
  52.     
  53.     pPoint = new CPoint();
  54.     if (pPoint == NULL)
  55.         goto error;
  56.     
  57.     // Load type information for the point from type library. 
  58.     hr = LoadTypeInfo(&pPoint->m_ptinfo, IID_IPoint);
  59.     if (FAILED(hr))
  60.         goto error;
  61.  
  62.     *ppPoint = pPoint;
  63.     return NOERROR;
  64.     
  65. error:                        
  66.     if (pPoint == NULL)
  67.         return E_OUTOFMEMORY; 
  68.                     
  69.     if (pPoint->m_ptinfo)
  70.         pPoint->m_ptinfo->Release();
  71.     
  72.     // Set to NULL to prevent destructor from attempting to free again
  73.     pPoint->m_ptinfo = NULL; 
  74.     
  75.     delete pPoint;
  76.     return hr;
  77. }
  78.  
  79. /*
  80.  * CPoint::CPoint
  81.  *
  82.  * Purpose:
  83.  *  Constructor for CPoint object. Initializes members to NULL.
  84.  *
  85.  */
  86. #pragma warning (disable : 4355)
  87. CPoint::CPoint() : m_SupportErrorInfo(this, IID_IPoint)
  88. #pragma warning (default : 4355)
  89. {   
  90.     m_cRef = 0;   
  91.     m_cInternalRef = 0;
  92.     m_ptinfo = NULL;
  93.     m_nX = -1;
  94.     m_nY = -1;    
  95. }
  96.  
  97. /*
  98.  * CPoint::~CPoint
  99.  *
  100.  * Purpose:
  101.  *  Destructor for CPoint object. Frees Point message BSTR and default
  102.  *  IDispatch implementation. Closes the aplication.
  103.  *
  104.  */
  105. CPoint::~CPoint()
  106. {            
  107.      if (m_ptinfo) m_ptinfo->Release();  
  108. }
  109.  
  110. /*
  111.  * CPoint::QueryInterface, AddRef, Release
  112.  *
  113.  * Purpose:
  114.  *  Implements IUnknown::QueryInterface, AddRef, Release
  115.  *
  116.  */
  117. STDMETHODIMP
  118. CPoint::QueryInterface(REFIID iid, void FAR* FAR* ppv) 
  119. {   
  120.     *ppv = NULL;
  121.     
  122.     if (iid == IID_IUnknown || iid == IID_IDispatch || iid == IID_IPoint)
  123.         *ppv = this;   
  124.     else if (iid == IID_ISupportErrorInfo)
  125.         *ppv = &m_SupportErrorInfo;
  126.     else return E_NOINTERFACE; 
  127.  
  128.     AddRef();
  129.     return NOERROR;    
  130. }
  131.  
  132.  
  133. STDMETHODIMP_(ULONG)
  134. CPoint::AddRef(void)
  135. #ifdef _DEBUG  
  136.     TCHAR ach[50];
  137.     wsprintf(ach, TEXT("Ref = %ld, Point\r\n"), m_cRef+1); 
  138.     OutputDebugString(ach); 
  139. #endif  
  140.  
  141.     return ++m_cRef;
  142. }
  143.  
  144. STDMETHODIMP_(ULONG)
  145. CPoint::Release(void)
  146. {
  147. #ifdef _DEBUG  
  148.     TCHAR ach[50];
  149.     wsprintf(ach, TEXT("Ref = %ld, Point\r\n"), m_cRef-1); 
  150.     OutputDebugString(ach);   
  151. #endif
  152.     
  153.     if(--m_cRef == 0)
  154.     {
  155.         delete this;
  156.         return 0;
  157.     }
  158.     return m_cRef;
  159. }
  160.  
  161. /*
  162.  * CPoint::GetTypeInfoCount
  163.  *
  164.  * Purpose:
  165.  *  Implements IDispatch::GetTypeInfoCount.
  166.  *
  167.  */
  168. STDMETHODIMP
  169. CPoint::GetTypeInfoCount(UINT FAR* pctinfo)
  170. {
  171.     *pctinfo = 1;
  172.     return NOERROR;
  173. }
  174.  
  175. /*
  176.  * CPoint::GetTypeInfo
  177.  *
  178.  * Purpose:
  179.  *  Implements IDispatch::GetTypeInfo. 
  180.  *
  181.  */
  182. STDMETHODIMP
  183. CPoint::GetTypeInfo(
  184.       UINT itinfo,
  185.       LCID lcid,
  186.       ITypeInfo FAR* FAR* pptinfo)
  187. {    
  188.     *pptinfo = NULL;
  189.      
  190.     if(itinfo != 0)
  191.         return DISP_E_BADINDEX;
  192.     
  193.     m_ptinfo->AddRef(); 
  194.     *pptinfo = m_ptinfo;
  195.     
  196.     return NOERROR;
  197. }
  198.  
  199. /*
  200.  * CPoint::GetIDsOfNames
  201.  *
  202.  * Purpose:
  203.  *  Implements IDispatch::GetIDsOfNames.  The standard implementation, DispGetIDsOfNames,
  204.  *  is used.
  205.  *
  206.  */
  207. STDMETHODIMP 
  208. CPoint::GetIDsOfNames(
  209.       REFIID riid,
  210.       OLECHAR FAR* FAR* rgszNames,
  211.       UINT cNames,
  212.       LCID lcid,
  213.       DISPID FAR* rgdispid)
  214. {
  215.     return DispGetIDsOfNames(m_ptinfo, rgszNames, cNames, rgdispid);
  216. }
  217.  
  218. /*
  219.  * CPoint::Invoke
  220.  *
  221.  * Purpose:
  222.  *  Implements IDispatch::Invoke.  The standard implementation, DispInvoke,
  223.  *  is used.
  224.  *
  225.  */
  226. STDMETHODIMP
  227. CPoint::Invoke(
  228.       DISPID dispidMember,
  229.       REFIID riid,
  230.       LCID lcid,
  231.       WORD wFlags,
  232.       DISPPARAMS FAR* pdispparams,
  233.       VARIANT FAR* pvarResult,
  234.       EXCEPINFO FAR* pexcepinfo,
  235.       UINT FAR* puArgErr)
  236. {  
  237.     return DispInvoke(
  238.         this, m_ptinfo,
  239.         dispidMember, wFlags, pdispparams,
  240.         pvarResult, pexcepinfo, puArgErr);
  241. }
  242.  
  243. STDMETHODIMP
  244. CPoint::get_x(int FAR* pnX)
  245. {
  246.     *pnX = m_nX;   
  247.     return NOERROR;
  248. }
  249.  
  250. STDMETHODIMP
  251. CPoint::put_x(int nX)
  252. {
  253.     m_nX = nX;
  254.     return NOERROR;
  255. }
  256.  
  257. STDMETHODIMP
  258. CPoint::get_y(int FAR* pnY)
  259. {
  260.     *pnY = m_nY; 
  261.     return NOERROR;
  262. }
  263.  
  264. STDMETHODIMP
  265. CPoint::put_y(int nY)
  266. {
  267.     m_nY = nY;  
  268.     return NOERROR;
  269. }  
  270.  
  271. /* 
  272.  *
  273.  * The following methods are not exposed through Automation
  274.  *
  275.  */
  276.  
  277. /*
  278.  * CPoint::InternalAddRef, InternalRelease
  279.  *
  280.  * Purpose:  
  281.  *  Implements an internal ref count for the use of the points collection.
  282.  *  The points collection does not have duplicates, instead a reference count is incremented.
  283.  *  A point is removed from the collection when the reference count drops to 0.
  284.  *
  285.  */
  286. STDMETHODIMP_(ULONG)
  287. CPoint::InternalAddRef(void)
  288. #ifdef _DEBUG  
  289.     TCHAR ach[50];
  290.     wsprintf(ach, TEXT("Internal Ref = %ld, Point\r\n"), m_cInternalRef+1); 
  291.     OutputDebugString(ach); 
  292. #endif  
  293.  
  294.     return ++m_cInternalRef;
  295. }
  296.  
  297. STDMETHODIMP_(ULONG)
  298. CPoint::InternalRelease(void)
  299. {
  300. #ifdef _DEBUG  
  301.     TCHAR ach[50];
  302.     wsprintf(ach, TEXT("Internal Ref = %ld, Point\r\n"), m_cInternalRef-1); 
  303.     OutputDebugString(ach);   
  304. #endif
  305.     
  306.     return --m_cInternalRef;
  307. }    
  308.  
  309. STDMETHODIMP_(int)
  310. CPoint::get_x(void)
  311. {
  312.     return m_nX;
  313. }
  314.  
  315.  
  316. STDMETHODIMP_(int)
  317. CPoint::get_y(void)
  318. {
  319.     return m_nY;
  320. }
  321.