home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / vcoledb / consumer / atlagent / agentctl.h < prev    next >
C/C++ Source or Header  |  1998-03-26  |  3KB  |  129 lines

  1. // AgentCtl.h : Declaration of the CAgentCtl
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #ifndef __AGENTCTL_H_
  14. #define __AGENTCTL_H_
  15.  
  16. #include "resource.h"       // main symbols
  17. // Change the directory names here if you have installed the Microsoft Agent
  18. // files into a different location
  19. #import "C:\Program Files\Microsoft Agent\agentsvr.exe" no_namespace
  20. #define AGENT_CHARACTER "C:\\Program Files\\Microsoft Agent\\merlinsfx.acs"
  21.  
  22. // Accessor to bring back the instructions from the database
  23. class CInstruction
  24. {
  25. public:
  26.     TCHAR   szCommand[11];
  27.     TCHAR   szText[256];
  28.  
  29. BEGIN_COLUMN_MAP(CInstruction)
  30.     COLUMN_ENTRY(2, szCommand)
  31.     COLUMN_ENTRY(3, szText)
  32. END_COLUMN_MAP()
  33. };
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CAgentCtl
  37. class ATL_NO_VTABLE CAgentCtl :
  38.     public CComObjectRootEx<CComSingleThreadModel>,
  39.     public CComCoClass<CAgentCtl, &CLSID_AgentCtl>,
  40.     public IDispatchImpl<IAgentCtl, &IID_IAgentCtl, &LIBID_ATLAGENTLib>,
  41.     public IObjectSafetyImpl<CAgentCtl, INTERFACESAFE_FOR_UNTRUSTED_CALLER>
  42. {
  43. public:
  44.     CAgentCtl()
  45.     {
  46.         m_nSpeed = 10;
  47.     }
  48.     HRESULT FinalConstruct()
  49.     {
  50.         HRESULT hr = InitializeAgent();
  51.         if (FAILED(hr))
  52.         {
  53.             TCHAR szText[129];
  54.             wsprintf(szText, _T("Couldn't start Agent (0x%X)"), hr);
  55.             MessageBox(NULL, szText, _T("ATLAgent"), 0);
  56.             return hr;
  57.         }
  58.  
  59.         hr = OpenDatabase();
  60.         if (FAILED(hr))
  61.         {
  62.             TCHAR szText[129];
  63.             wsprintf(szText, _T("Couldn't open Agent database (0x%X)"), hr);
  64.             MessageBox(NULL, szText, _T("ATLAgent"), 0);
  65.         }
  66.         return hr;
  67.     }
  68.     HRESULT InitializeAgent()
  69.     {
  70.         HRESULT hr;
  71.         long    lCharID;
  72.         CComPtr<IDispatch>  pdCharacter;
  73.         hr = m_pAgent.CreateInstance(__uuidof(AgentServer));
  74.         if (FAILED(hr))
  75.             return hr;
  76.  
  77.         hr = m_pAgent->Load(AGENT_CHARACTER, &lCharID, &m_nRequestID);
  78.         if (FAILED(hr))
  79.             return hr;
  80.  
  81.         hr = m_pAgent->GetCharacter(lCharID, &pdCharacter);
  82.         if (FAILED(hr))
  83.             return hr;
  84.  
  85.         m_pCharacter = pdCharacter;
  86.         return S_OK;
  87.     }
  88.     HRESULT OpenDatabase()
  89.     {
  90.         CDataSource     db;
  91.  
  92.         // Open the database and create a session
  93.         HRESULT hr = db.Open(_T("MSDASQL"), _T("Agent"), _T("Admin"));
  94.         if (FAILED(hr))
  95.             return hr;
  96.  
  97.         return m_session.Open(db);
  98.     }
  99.  
  100. DECLARE_REGISTRY_RESOURCEID(IDR_AGENTCTL)
  101.  
  102. BEGIN_COM_MAP(CAgentCtl)
  103.     COM_INTERFACE_ENTRY(IAgentCtl)
  104.     COM_INTERFACE_ENTRY(IDispatch)
  105.     COM_INTERFACE_ENTRY(IObjectSafety)
  106. END_COM_MAP()
  107.  
  108. // IAgentCtl
  109. public:
  110.     STDMETHOD(Stop)();
  111.     STDMETHOD(Animate)(/*[in]*/ BSTR Animation);
  112.     STDMETHOD(get_Speed)(/*[out, retval]*/ long *pVal);
  113.     STDMETHOD(put_Speed)(/*[in]*/ long newVal);
  114.     STDMETHOD(SpeakText)(BSTR Text);
  115.     STDMETHOD(SpeakFile)(BSTR FileName);
  116.     STDMETHOD(Play)();
  117.     void ProcessInstruction(const CInstruction& instruction);
  118.  
  119.     // Microsoft Agent stuff
  120.     IAgentPtr           m_pAgent;
  121.     IAgentCharacterPtr  m_pCharacter;
  122.     long                m_nSpeed;
  123.     long                m_nRequestID;
  124.     // DTL stuff
  125.     CSession            m_session;
  126. };
  127.  
  128. #endif //__AGENTCTL_H_
  129.