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 / spoly2 / cpoint.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-01  |  8.2 KB  |  363 lines

  1. /*** 
  2. *cpoint.cpp
  3. *
  4. *  This is a part of the Microsoft Source Code Samples.
  5. *
  6. *  Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  7. *
  8. *  This source code is only intended as a supplement to Microsoft Development
  9. *  Tools and/or WinHelp documentation.  See these sources for detailed
  10. *  information regarding the Microsoft samples programs.
  11. *
  12. *Purpose:
  13. *  This module implements the CPoint and CPointCF classes.
  14. *
  15. *  This module is intended as a sample implementation of the IDispatch
  16. *  interface, and its purpose is to demonstrate how an object can
  17. *  expose methods and properties for programatic and cross-process
  18. *  access via the IDispatch interface.
  19. *
  20. *Implementation Notes:
  21. *
  22. *****************************************************************************/
  23.  
  24. #include "spoly.h"
  25. #include "cpoint.h"
  26.  
  27.  
  28. CPoint::CPoint()
  29. {
  30.     m_x = 0;
  31.     m_y = 0;
  32.     m_refs = 0;
  33.     m_ptinfo = NULL;
  34. }
  35.  
  36. /***
  37. *CPoint::Create(void)
  38. *Purpose:
  39. *  Create an instance of a CPoint object.
  40. *
  41. *Entry:
  42. *  None
  43. *
  44. *Exit:
  45. *  returns a CPoint*, NULL if creation failed.
  46. *
  47. ***********************************************************************/
  48. CPoint FAR*
  49. CPoint::Create()
  50. {
  51.     HRESULT hresult;
  52.     CPoint FAR* ppoint;
  53.     ITypeInfo FAR* ptinfo;
  54. extern INTERFACEDATA NEAR g_idataCPoint;
  55.  
  56.  
  57.     if((ppoint = new FAR CPoint()) == NULL)
  58.       return NULL;
  59.     ppoint->AddRef();
  60.  
  61.     hresult =
  62.       CreateDispTypeInfo(&g_idataCPoint, LOCALE_SYSTEM_DEFAULT, &ptinfo);
  63.     if(hresult != NOERROR)
  64.       goto LError0;
  65.  
  66.     ppoint->m_ptinfo = ptinfo;
  67.  
  68.     return ppoint;
  69.  
  70. LError0:;
  71.     ppoint->Release();
  72.  
  73.     return NULL;
  74. }
  75.  
  76.  
  77. //---------------------------------------------------------------------
  78. //                     IUnknown Methods
  79. //---------------------------------------------------------------------
  80.  
  81.  
  82. STDMETHODIMP
  83. CPoint::QueryInterface(REFIID riid, void FAR* FAR* ppv)
  84. {
  85.     if(IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IDispatch)){
  86.       *ppv = this;
  87.       AddRef();
  88.       return NOERROR;
  89.     }
  90.     *ppv = NULL;
  91.     return E_NOINTERFACE;
  92. }
  93.  
  94.  
  95. STDMETHODIMP_(unsigned long)
  96. CPoint::AddRef(void)
  97. {
  98.     return ++m_refs;
  99. }
  100.  
  101.  
  102. STDMETHODIMP_(unsigned long)
  103. CPoint::Release(void)
  104. {
  105.     if(--m_refs == 0){
  106.       if(m_ptinfo != NULL){
  107.     m_ptinfo->Release();
  108.       }
  109.       delete this;
  110.       return 0;
  111.     }
  112.     return m_refs;
  113. }
  114.  
  115.  
  116. //---------------------------------------------------------------------
  117. //                     IDispatch methods
  118. //---------------------------------------------------------------------
  119.  
  120.  
  121. STDMETHODIMP
  122. CPoint::GetTypeInfoCount(unsigned int FAR* pctinfo)
  123. {
  124.     // this object has a single *introduced* interface
  125.     //
  126.     *pctinfo = 1;
  127.  
  128.     return NOERROR;
  129. }
  130.  
  131.  
  132. STDMETHODIMP
  133. CPoint::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  134. {
  135.     UNUSED(lcid);
  136.  
  137.     if(itinfo != 0)
  138.       return DISP_E_BADINDEX;
  139.  
  140.     m_ptinfo->AddRef();
  141.     *pptinfo = m_ptinfo;
  142.  
  143.     return NOERROR;
  144. }
  145.  
  146.  
  147. /***
  148. *HRESULT CPoint::GetIDsOfNames(REFIID, char**, unsigned int, LCID, DISPID*)
  149. *Purpose:
  150. *  This method translates the given array of names to a corresponding
  151. *  array of DISPIDs.
  152. *
  153. *  Index 0 of the name array is the member name, and indices 1-N if
  154. *  present represent named parameters on that member.
  155. *
  156. *  The local ID ('lcid') is unused by this naive implementation. A more
  157. *  sophisticated implementation, sensitive to localization and natural
  158. *  language support would use the locale ID to interpret the given names
  159. *  in a correct locale specific context.
  160. *
  161. *Entry:
  162. *  rgszNames = pointer to an array of names
  163. *  cNames = the number of names in the rgszNames array
  164. *  lcid = the callers locale ID
  165. *
  166. *Exit:
  167. *  return value = HRESULT
  168. *  rgid = array of name IDs corresponding to the rgszNames array
  169. *    this array will contain -1 for each entry that is not known.
  170. *
  171. ***********************************************************************/
  172. STDMETHODIMP
  173. CPoint::GetIDsOfNames(
  174.     REFIID riid,
  175.     OLECHAR FAR* FAR* rgszNames,
  176.     unsigned int cNames,
  177.     LCID lcid,
  178.     DISPID FAR* rgdispid)
  179. {
  180.     UNUSED(lcid);
  181.  
  182.     if(!IsEqualIID(riid,IID_NULL))
  183.       return DISP_E_UNKNOWNINTERFACE;
  184.  
  185.     return DispGetIDsOfNames(m_ptinfo, rgszNames, cNames, rgdispid);
  186. }
  187.  
  188.  
  189. /***
  190. *HRESULT CPoint::Invoke(...)
  191. *Purpose:
  192. *  Dispatch a method or property request for objects of type CPoint.
  193. *
  194. *  see the IDispatch document for more information, and a general
  195. *  description of this method.
  196. *
  197. *Entry:
  198. *  dispidMember = the DISPID of the member being requested
  199. *
  200. *  riid = reference to the interface ID of the interface on this object
  201. *    that the requested member belongs to. IID_NULL means to interpret
  202. *    the member as belonging to the implementation defined "default"
  203. *    or "primary" interface.
  204. *
  205. *  lcid = the caller's locale ID
  206. *
  207. *  wFlags = flags indicating the type of access being requested
  208. *
  209. *  pdispparams = pointer to the DISPPARAMS struct containing the
  210. *    requested members arguments (if any) and its named parameter
  211. *    DISPIDs (if any).
  212. *
  213. *Exit:
  214. *  return value = HRESULT
  215. *   see the IDispatch spec for a description of possible success codes.
  216. *
  217. *  pvarResult = pointer to a caller allocated VARIANT containing
  218. *    the members return value (if any).
  219. *
  220. *  pexcepinfo = caller allocated exception info structure, this will
  221. *    be filled in only if an exception was raised that must be passed
  222. *    up through Invoke to an enclosing handler.
  223. *
  224. *  puArgErr = pointer to a caller allocated UINT, that will contain the
  225. *    index of the offending argument if a DISP_E_TYPEMISMATCH error
  226. *    was returned indicating that one of the arguments was of an
  227. *    incorrect type and/or could not be reasonably coerced to a proper
  228. *    type.
  229. *
  230. ***********************************************************************/
  231. STDMETHODIMP
  232. CPoint::Invoke(
  233.     DISPID dispidMember,
  234.     REFIID riid,
  235.     LCID lcid,
  236.     unsigned short wFlags,
  237.     DISPPARAMS FAR* pdispparams,
  238.     VARIANT FAR* pvarResult,
  239.     EXCEPINFO FAR* pexcepinfo,
  240.     unsigned int FAR* puArgErr)
  241. {
  242.     UNUSED(lcid);
  243.  
  244.     if(!IsEqualIID(riid, IID_NULL))
  245.       return DISP_E_UNKNOWNINTERFACE;
  246.  
  247.     return DispInvoke(
  248.       this, m_ptinfo,
  249.       dispidMember, wFlags, pdispparams,
  250.       pvarResult, pexcepinfo, puArgErr);
  251. }
  252.  
  253.  
  254. //---------------------------------------------------------------------
  255. //                       Introduced methods
  256. //---------------------------------------------------------------------
  257.  
  258. short METHODCALLTYPE EXPORT
  259. CPoint::GetX()
  260. {
  261.     return m_x;
  262. }
  263.  
  264. void METHODCALLTYPE EXPORT
  265. CPoint::SetX(short x)
  266. {
  267.     m_x = x;
  268. }
  269.  
  270. short METHODCALLTYPE EXPORT
  271. CPoint::GetY()
  272. {
  273.     return m_y;
  274. }
  275.  
  276. void METHODCALLTYPE EXPORT
  277. CPoint::SetY(short y)
  278. {
  279.     m_y = y;
  280. }
  281.  
  282.  
  283. //---------------------------------------------------------------------
  284. //         Implementation of the CPoint Class Factory
  285. //---------------------------------------------------------------------
  286.  
  287. CPointCF::CPointCF()
  288. {
  289.     m_refs = 0;
  290. }
  291.  
  292. IClassFactory FAR*
  293. CPointCF::Create()
  294. {
  295.     CPointCF FAR* pCF;
  296.  
  297.     if((pCF = new FAR CPointCF()) == NULL)
  298.       return NULL;
  299.     pCF->AddRef();
  300.     return pCF;
  301. }
  302.  
  303. STDMETHODIMP
  304. CPointCF::QueryInterface(REFIID riid, void FAR* FAR* ppv) 
  305. {
  306.     if(IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory)){
  307.       *ppv = this;
  308.       ++m_refs;
  309.       return NOERROR;
  310.     }
  311.     *ppv = NULL;
  312.     return E_NOINTERFACE;
  313. }
  314.  
  315. STDMETHODIMP_(unsigned long)
  316. CPointCF::AddRef(void)
  317. {
  318.     return ++m_refs;
  319. }
  320.  
  321. STDMETHODIMP_(unsigned long)
  322. CPointCF::Release(void)
  323. {
  324.     if(--m_refs == 0){
  325.       delete this;
  326.       return 0;
  327.     }
  328.     return m_refs;
  329. }
  330.  
  331. STDMETHODIMP
  332. CPointCF::CreateInstance(
  333.     IUnknown FAR* punkOuter,
  334.     REFIID riid,
  335.     void FAR* FAR* ppv)
  336. {
  337.     HRESULT hresult;
  338.     CPoint FAR *ppoint;
  339.  
  340.     UNUSED(punkOuter);
  341.  
  342.     if((ppoint = CPoint::Create()) == NULL){
  343.       *ppv = NULL;
  344.       return E_OUTOFMEMORY;
  345.     }
  346.     hresult = ppoint->QueryInterface(riid, ppv);
  347.     ppoint->Release();
  348.     return hresult;
  349. }
  350.  
  351. STDMETHODIMP
  352. #ifdef _MAC
  353. CPointCF::LockServer(unsigned long fLock)
  354. #else
  355. CPointCF::LockServer(BOOL fLock)
  356. #endif
  357. {
  358.     UNUSED(fLock);
  359.  
  360.     return NOERROR;
  361. }
  362.  
  363.