home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / inproc / server / varmap.cpp < prev    next >
C/C++ Source or Header  |  1998-04-02  |  5KB  |  187 lines

  1. // varmap.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "inproc.h"
  15. #include "enumvar.h"
  16. #include "varassoc.h"
  17. #include "varmap.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CVariantMap
  26.  
  27. IMPLEMENT_DYNCREATE(CVariantMap, CCmdTarget)
  28.  
  29. CVariantMap::CVariantMap()
  30. {
  31.     EnableAutomation();
  32.     
  33.     // To keep the application running as long as an OLE automation 
  34.     //  object is active, the constructor calls AfxOleLockApp.
  35.     
  36.     AfxOleLockApp();
  37. }
  38.  
  39. CVariantMap::~CVariantMap()
  40. {
  41.     // To terminate the application when all objects created with
  42.     //  with OLE automation, the destructor calls AfxOleUnlockApp.
  43.     
  44.     AfxOleUnlockApp();
  45. }
  46.  
  47. void CVariantMap::OnFinalRelease()
  48. {
  49.     // When the last reference for an automation object is released
  50.     //  OnFinalRelease is called.  This implementation deletes the 
  51.     //  object.  Add additional cleanup required for your object before
  52.     //  deleting it from memory.
  53.  
  54.     delete this;
  55. }
  56.  
  57. BEGIN_MESSAGE_MAP(CVariantMap, CCmdTarget)
  58.     //{{AFX_MSG_MAP(CVariantMap)
  59.         // NOTE - the ClassWizard will add and remove mapping macros here.
  60.     //}}AFX_MSG_MAP
  61. END_MESSAGE_MAP()
  62.  
  63. // {7ACE7860-9A1C-11cd-9A90-00DD01113F12}
  64. IMPLEMENT_OLECREATE(CVariantMap, "mfc.inproc.varmap", 
  65.     0x7ace7860, 0x9a1c, 0x11cd, 0x9a, 0x90, 0x0, 0xdd, 0x1, 0x11, 0x3f, 0x12);
  66.  
  67. BEGIN_DISPATCH_MAP(CVariantMap, CCmdTarget)
  68.     //{{AFX_DISPATCH_MAP(CVariantMap)
  69.     DISP_PROPERTY(CVariantMap, "str1", m_str1, VT_BSTR)
  70.     DISP_PROPERTY(CVariantMap, "i1", m_i1, VT_I4)
  71.     DISP_PROPERTY_EX(CVariantMap, "str2", GetStr2, SetStr2, VT_BSTR)
  72.     DISP_PROPERTY_EX(CVariantMap, "i2", GetI2, SetI2, VT_I4)
  73.     DISP_PROPERTY_EX(CVariantMap, "Count", GetCount, SetNotSupported, VT_I4)
  74.     DISP_FUNCTION(CVariantMap, "SetAt", SetAt, VT_EMPTY, VTS_VARIANT VTS_VARIANT)
  75.     DISP_FUNCTION(CVariantMap, "RemoveAll", RemoveAll, VT_EMPTY, VTS_NONE)
  76.     DISP_FUNCTION(CVariantMap, "RemoveKey", RemoveKey, VT_EMPTY, VTS_VARIANT)
  77.     DISP_FUNCTION(CVariantMap, "IsEmpty", IsEmpty, VT_BOOL, VTS_NONE)
  78.     DISP_PROPERTY_PARAM(CVariantMap, "Item", GetItem, SetNotSupported, VT_VARIANT, VTS_VARIANT)
  79.     //}}AFX_DISPATCH_MAP
  80.     DISP_PROPERTY_EX_ID(CVariantMap, "_NewEnum", DISPID_NEWENUM, GetNewEnum, SetNotSupported, VT_UNKNOWN)
  81.     DISP_DEFVALUE(CVariantMap, "Item")
  82. END_DISPATCH_MAP()
  83.  
  84. // {747205C0-F9F0-11cd-8C3D-00AA004BB3B7}
  85. static const IID IID_IVariantMap = { 0x747205c0, 0xf9f0, 0x11cd, 
  86.     { 0x8c, 0x3d, 0x0, 0xaa, 0x0, 0x4b, 0xb3, 0xb7 } };
  87.  
  88. // Note: we add support for IID_IVariantMap to support typesafe binding
  89. // from VBA.  This IID must match the GUID that is attached to the 
  90. // dispinterface in the .ODL file.
  91.  
  92. BEGIN_INTERFACE_MAP(CVariantMap, CCmdTarget)
  93.     INTERFACE_PART(CVariantMap, IID_IVariantMap, Dispatch)
  94. END_INTERFACE_MAP()
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CVariantMap message handlers
  98.  
  99. LPUNKNOWN CVariantMap::GetNewEnum()
  100. {
  101.     CEnumVariant* pEnum = new CEnumVariant;
  102.     int nCount = m_map.GetCount();
  103.     VARIANT* pContents = new VARIANT[nCount];
  104.     int i = 0;
  105.  
  106.     TRY
  107.     {
  108.         POSITION pos = m_map.GetStartPosition();
  109.         while (pos != NULL)
  110.         {
  111.             ASSERT(i < nCount);
  112.  
  113.             // get next value from the map and create a CVariantAssoc from it
  114.             CVariantAssoc* pAssoc = new CVariantAssoc;
  115.             m_map.GetNextAssoc(pos, pAssoc->m_varKey, pAssoc->m_varValue);
  116.  
  117.             // stuff it into a variant
  118.             pContents[i].pdispVal = pAssoc->GetIDispatch(FALSE);
  119.             pContents[i].vt = VT_DISPATCH;
  120.             ++i;
  121.         }
  122.     }
  123.     CATCH_ALL(e)
  124.     {
  125.         while (--i >= 0)
  126.             VariantClear(&pContents[i]);
  127.  
  128.         THROW_LAST();
  129.     }
  130.     END_CATCH_ALL
  131.     pEnum->SetContents(pContents, nCount);
  132.  
  133.     return pEnum->GetInterface(&IID_IUnknown);
  134. }
  135.  
  136. void CVariantMap::SetAt(const VARIANT FAR& from, const VARIANT FAR& to) 
  137. {
  138.     m_map.SetAt(from, to);
  139. }
  140.  
  141. VARIANT CVariantMap::GetItem(const VARIANT FAR& from) 
  142. {
  143.     COleVariant varResult;
  144.     m_map.Lookup(from, varResult);
  145.     return varResult.Detach();
  146. }
  147.  
  148. void CVariantMap::RemoveAll() 
  149. {
  150.     m_map.RemoveAll();
  151. }
  152.  
  153. void CVariantMap::RemoveKey(const VARIANT FAR& key) 
  154. {
  155.     m_map.RemoveKey(key);
  156. }
  157.  
  158. long CVariantMap::GetCount() 
  159. {
  160.     return m_map.GetCount();
  161. }
  162.  
  163. BOOL CVariantMap::IsEmpty() 
  164. {
  165.     return m_map.IsEmpty();
  166. }
  167.  
  168. BSTR CVariantMap::GetStr2() 
  169. {
  170.     return m_str2.AllocSysString();
  171. }
  172.  
  173. void CVariantMap::SetStr2(LPCTSTR lpszNewValue) 
  174. {
  175.     m_str2 = lpszNewValue;
  176. }
  177.  
  178. long CVariantMap::GetI2() 
  179. {
  180.     return m_i2;
  181. }
  182.  
  183. void CVariantMap::SetI2(long nNewValue) 
  184. {
  185.     m_i2 = nNewValue;
  186. }
  187.