home *** CD-ROM | disk | FTP | other *** search
/ Developing for Microsoft …tive Animated Characters / DEV_AGENTA.ISO / Examples / c / hello / Hello.cpp next >
C/C++ Source or Header  |  1997-08-20  |  6KB  |  273 lines

  1. #ifndef STRICT
  2. #define STRICT
  3. #endif
  4.  
  5.  
  6. //==========================================================================
  7. //
  8. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  9. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  11. //  PURPOSE.
  12. //
  13. //  Copyright (C) 1997 Microsoft Corporation.  All Rights Reserved.
  14. //
  15. //--------------------------------------------------------------------------
  16. //
  17. // This sample demonstrates the simplest Microsoft Agent application
  18. //
  19. //==========================================================================
  20.  
  21.  
  22. #include <windows.h>
  23. #include <tchar.h>
  24. #include "AgtSvr.h"
  25. #include "AgtSvr_i.c"
  26.  
  27.  
  28. static const LPWSTR kpwszCharacter = L"\\program files\\microsoft agent\\characters\\genie.acs";
  29. static const LPTSTR kpszAppTitle = _T("Microsoft Agent Samples");
  30.  
  31.  
  32. // This function checks to see if the version of Microsoft Agent
  33. // installed on the user's system is compatible with the version
  34. // we compiled with, i.e. it's version is greater than or equal to
  35. // constants defined in AgtSvr.h.  Microsoft Agent guarantees
  36. // backward compatibility.
  37.  
  38. BOOL IsValidAgentVersion(IAgent *pAgent) {
  39.  
  40.     HRESULT hRes;
  41.     IDispatch *pdAgent = NULL;
  42.     ITypeInfo *pTypeInfo = NULL;
  43.     ITypeLib *pTypeLib = NULL;
  44.     TLIBATTR *pTypeLibAttr = NULL;
  45.     BOOL bValid = FALSE;
  46.     UINT uiIndex;
  47.  
  48.     __try {
  49.  
  50.         // Query for IDispatch
  51.  
  52.         hRes = pAgent->QueryInterface(IID_IDispatch, (LPVOID *)&pdAgent);
  53.  
  54.         if (FAILED(hRes))
  55.             __leave;
  56.  
  57.         // Get the TypeInfo
  58.  
  59.         hRes = pdAgent->GetTypeInfo(0, 0, &pTypeInfo);
  60.  
  61.         if (FAILED(hRes))
  62.             __leave;
  63.  
  64.         // Get it's containing TypeLib
  65.  
  66.         hRes = pTypeInfo->GetContainingTypeLib(&pTypeLib, &uiIndex);
  67.  
  68.         if (FAILED(hRes))
  69.             __leave;
  70.  
  71.         // Get the attributes of the TypeLib
  72.  
  73.         hRes = pTypeLib->GetLibAttr(&pTypeLibAttr);
  74.  
  75.         if (FAILED(hRes))
  76.             __leave;
  77.  
  78.         // Check the major and minor versions of the type library
  79.         // to those in AgentServer.h.
  80.  
  81.         if ((pTypeLibAttr->wMajorVerNum > AGENT_VERSION_MAJOR) ||
  82.             ((pTypeLibAttr->wMajorVerNum == AGENT_VERSION_MAJOR) &&
  83.              (pTypeLibAttr->wMinorVerNum >= AGENT_VERSION_MINOR)))
  84.             bValid = TRUE;
  85.  
  86.     }
  87.     __finally {
  88.     }
  89.  
  90.     if (pTypeLib) {
  91.  
  92.         if (pTypeLibAttr)
  93.             pTypeLib->ReleaseTLibAttr(pTypeLibAttr);
  94.  
  95.         pTypeLib->Release();
  96.     }
  97.  
  98.     if (pTypeInfo)
  99.         pTypeInfo->Release();
  100.  
  101.     if (pdAgent)
  102.         pdAgent->Release();
  103.  
  104.     return bValid;
  105. }
  106.  
  107.  
  108. extern "C" int PASCAL WinMain(HINSTANCE hInst,
  109.                               HINSTANCE hInstPrev,
  110.                               LPSTR lpCmdLine,
  111.                               int nCmdShow) {
  112.  
  113.     HRESULT                hRes;
  114.     _TCHAR                szError[256];
  115.     VARIANT                vPath;
  116.     BSTR                bszSpeak;
  117.     long                lCharID;
  118.     long                lRequestID;
  119.     IAgent               *pAgent;
  120.     IDispatch           *pdCharacter;
  121.     IAgentCharacter    *pCharacter = NULL;
  122.     
  123.     // Initialize OLE
  124.  
  125.     if (FAILED(OleInitialize(NULL))) {
  126.         MessageBox(NULL, 
  127.                    _T("There was an error initializing OLE."), 
  128.                    kpszAppTitle, 
  129.                    MB_OK | MB_ICONERROR);
  130.         return -1;
  131.     }
  132.  
  133.     // Create an instance of the Agent server
  134.  
  135.     hRes = CoCreateInstance(CLSID_AgentServer,
  136.                             NULL,
  137.                             CLSCTX_SERVER,
  138.                             IID_IAgent,
  139.                             (LPVOID *)&pAgent);
  140.     if (FAILED(hRes)) {
  141.  
  142.         wsprintf(szError, _T("There was an error initializing Microsoft Agent, code = 0x%x"), hRes);
  143.  
  144.         MessageBox(NULL, 
  145.                    szError, 
  146.                    kpszAppTitle, 
  147.                    MB_OK | MB_ICONERROR | MB_TOPMOST);
  148.         return -1;
  149.     }
  150.  
  151.     // Check to see if it is a compatible version
  152.  
  153.     if (!IsValidAgentVersion(pAgent)) {
  154.  
  155.         pAgent->Release();
  156.  
  157.         MessageBox(NULL,
  158.                    _T("The version of Microsoft Agent installed on this system is out of date.\nUpgrade to a new version of Microsoft Agent."),
  159.                    kpszAppTitle,
  160.                    MB_OK | MB_ICONEXCLAMATION | MB_TOPMOST);
  161.         return -1;
  162.     }
  163.  
  164.     // Create a variant to store the full path of the character to load
  165.  
  166.     VariantInit(&vPath);
  167.  
  168.     vPath.vt = VT_BSTR;
  169.     vPath.bstrVal = SysAllocString(kpwszCharacter);
  170.  
  171.     if (vPath.bstrVal == NULL) {
  172.  
  173.         MessageBox(NULL, 
  174.                    _T("Out of memory!"), 
  175.                    kpszAppTitle, 
  176.                    MB_OK | MB_ICONERROR | MB_TOPMOST);
  177.  
  178.         return -1;
  179.     }
  180.  
  181.     __try {
  182.  
  183.         // Load the character
  184.  
  185.         hRes = pAgent->Load(vPath, &lCharID, &lRequestID);
  186.  
  187.         if (FAILED(hRes))
  188.             __leave;
  189.  
  190.         // Get it's dispinterface
  191.  
  192.         hRes = pAgent->GetCharacter(lCharID, &pdCharacter);
  193.  
  194.         if (FAILED(hRes))
  195.             __leave;
  196.  
  197.         // Query for IAgentCharacter
  198.  
  199.         hRes = pdCharacter->QueryInterface(IID_IAgentCharacter, (LPVOID *)&pCharacter);
  200.  
  201.         // Release the IDispatch
  202.  
  203.         pdCharacter->Release();
  204.  
  205.         // Did we get the IAgentCharacter interface?
  206.  
  207.         if (FAILED(hRes))
  208.             __leave;
  209.  
  210.         // Show the character.  The first parameter tells Microsoft
  211.         // Agent to show the character by playing an animation.
  212.  
  213.         hRes = pCharacter->Show(FALSE, &lRequestID);
  214.  
  215.         if (FAILED(hRes))
  216.             __leave;
  217.  
  218.         // Make the character speak
  219.  
  220.         bszSpeak = SysAllocString(L"Hello World!");
  221.  
  222.         hRes = pCharacter->Speak(bszSpeak, NULL, &lRequestID);
  223.  
  224.         SysFreeString(bszSpeak);
  225.  
  226.         if (FAILED(hRes))
  227.             __leave;
  228.  
  229.         // This is a very simplistic sample.  Sleep for 10 seconds
  230.         // and then die. 
  231.  
  232.         Sleep(10000);
  233.  
  234.     }
  235.     __finally {
  236.  
  237.         if (FAILED(hRes)) {
  238.  
  239.             wsprintf(szError, _T("An error occurred in Microsoft Agent, code = 0x%x"), hRes);
  240.  
  241.             MessageBox(NULL, 
  242.                        szError, 
  243.                        kpszAppTitle, 
  244.                        MB_OK | MB_ICONERROR | MB_TOPMOST);
  245.         }
  246.     }
  247.  
  248.     // Clean up
  249.  
  250.     if (pCharacter) {
  251.  
  252.         // Release the character interface
  253.  
  254.         pCharacter->Release();
  255.  
  256.         // Unload the character.  NOTE:  releasing the character
  257.         // interface does NOT make the character go away.  You must
  258.         // call Unload.
  259.  
  260.         pAgent->Unload(lCharID);
  261.     }
  262.     
  263.     // Release the Agent
  264.  
  265.     pAgent->Release();
  266.  
  267.     VariantClear(&vPath);
  268.  
  269.     OleUninitialize();
  270.  
  271.     return 0;
  272. }
  273.