Using Microsoft Agent Controls in LCCWIN32 - Main File Agentsvr.c

xiaoxiao2021-03-06  14

This is the main program file using the Microsoft Agent control. After compiling, it is always failed at the "Registration IAgentNotifyInsink Interface" in the function agchr_load (), and the initial estimation is the "Initialization IAGentNotify INK Interface" section in the function agchr_load (), the statement "InitiagentNotifyink (& g_agnsink);" did not have successful creation An instance of the IAGENTNOTIFYSINK interface, but I don't know how to solve this problem.

/ ************************************************** ************************* * Agentsvr.c * * This module creates a Microsoft Agent Server control instance, load the Agent role peedy, * Add a custom menu Command, and handle the command event. * Since the Agent server runs in its own memory space, the transmitted string variable needs to be allocated with * sysallocstring () and releases with sysfreestring (). Use these two functions * Requires link library file Oleaut32.lib. *********************************************************** *********************** /

#include "resource.h" #include "agentsvr.h"

#define loadingwstr (ID, PBUF) LoadStringW (g_hinst, (id), (pbuf), Sizeof (PBUF)) #define loadstr (ID, PBUF) LoadString (g_hinst, (id), (pbuf), Sizeof (PBUF)) #define errmsg (s) MessageBox (g_hwndmain, (s), "Oh ...", MB_OK | MB_ICONICONFORMATION #define Errstr (ID, PBUF) LOADSTR ((ID), (PBUF)); errmsg (PBUF) #define ZeromeM (P) Memset (P), 0, SizeOf (P))

#define agent_version_major 2 # define agent_version_minor 0 # define menu_sum 5 # Define buf_size 256

// Extern variblesextern hinstance g_hinst; extern hwnd g_hwndmain

// Global Varibles for agenttypedef struct _Menu {long dwid; wchar wscap [16];

Menu g_agmenu [menu_sum]; menu g_agfile; long g_lrequestid = 0; long g_lnotifysinkid = 0;

IAGENTEX * G_AGENT = NULL; IAGENTCHARACTEREX * G_PEEDY = NULL; IAGENTNOTIFYSINK G_AGNSINK

// Functions' prototypesBOOL AgChr_AddMenus (IAgentCharacterEx *); BOOL AgChr_Load (void); void AgChr_Play_Speak (IAgentCharacterEx *, BSTR, BSTR); void AgChr_SelfIntroduce (IAgentCharacterEx *); void AgChr_Unload (void); BOOL IsValidAgentVersion (IAgentEx *); void ShowLastError (void); / * <----------------------------------------- -------------------------> * // * <- Re-vector all functions of the IAGENTNOTIFYSINK interface, the process of processing, return results - > * // * <- see: Microsoft Platform SDK: Microsoft Agent Start Page -> * / HRESULT STDMETHODCALLTYPE IAgentNotifySink_QueryInterface (IAgentNotifySink * This, GUID * riid, void ** ppvObj) {* ppvObj = NULL;

IF (ISEqualiid (RIID, & IID_IAGENTNOTISIN) {* ppvobj = (lpvoid) this; returnif;} returnif;}

Ulong stdmethodCallType IAGENTNOTIFYSINK_ADDREF (IAGENTNOTISINK * this) {Return 1;}

Ulong stdmethodCallType IAGENTNOTIFYSINK_RELEASE (IAGENTNOTISINK * this) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_GETTYPEINFOCOUNT (IAGENTNOTIFYSINK * THIS, UINT * PCTINFO) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_GETTYPEINFO (IAGENTNOTISINK * THIS, UINT ITINFO, ULONG LCID, VOID ** PPTINFO) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_GETIDSOFNAMES (IAGENTNOTISINK * THIS, GUID * RIID, Char ** RGSZNAMES, UINT CNAMES, ULONG LCID, Long * RGDispid) {Return 1;}

HRESULT STDMETHODCALLTYPE IAgentNotifySink_Invoke (IAgentNotifySink * This, LONG dispidMember, GUID * riid, ULONG lcid, USHORT wFlags, DISPPARAMS * pdispparams, VARIANT * pvarResult, EXCEPINFO * pexcepinfo, UINT * puArgErr) {return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_COMMAND (IAGENTNOTISINK * THIS, Long DWCommandid, IUNKNOWN * PUNKUSERINPUT) {IF (this == NULL) RETURN-1; IF (dwcommandid == 0) Return 0;

IF (dwcommandid == g_agmenu [0] .dwid) {// Earn stamp Return 1;}

IF (dwcommandid == g_agmenu [1] .dwid) {// option setting return 2;}

IF (dwcommandid == g_agmenu [2] .dwid) {// function description Return 3;}

IF (dwcommandid == g_agmenu [3] .dwid) {// About ... Return 4;}

IF (dwcommandid == g_agmenu [4] .dwid) {// exits the program Return 5;}

Return -1;

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_ACTIVATEINPUTSTATE (IAGENTNOTIFYSINK * THIS, Long DWcharid, long bactiVated) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_RESTART (IAGENTNOTIFYSINK * this) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_SHUTDOWN (IAGENTNOTIFYSINK * this) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_VISibleState (IAGENTNOTISINK * THIS, Long DWCHARID, long bvisible, long dwcause) {Return 1;}

HRESULTNOTIDCALLTYPE IAGENTNOTIFYSINK_CLICK (IAGENTNOTIFYSINK * THIS, Long DWcharid, short fwkeys, long x, long y) {return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_DBLCLICK (IAGENTNOTISINK * THIS, Long DWcharid, short fwkeys, long x, long y) {return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_DRAGSTART (IAGENTNOTISINK * THIS, Long DWcharid, Short Fwkeys, long x, long y) {return 1;}

HRESULT STDMETIDCALLTYPE IAGENTNOTIFYSINK_DRAGCOMPLETE (IAGENTNOTISINK * THIS, Long DWcharid, Short Fwkeys, long x, long y) {return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_REQUESTSTART (IAGENTNOTISINK * THIS, Long DwRequestid) {Return 1;}

HRESULT STDMETHODCALLTYPE IAgentNotifySink_RequestComplete (IAgentNotifySink * This, LONG dwRequestID, LONG hrStatus) {return 1;} HRESULT STDMETHODCALLTYPE IAgentNotifySink_BookMark (IAgentNotifySink * This, LONG dwBookMarkID) {return 1;}

HRESULT STDMETIDCALLTYPE IAGENTNOTIFYSINK_IDLE (IAGENTNOTIFYSINK * THIS, Long DWcharid, long bstart) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_MOVE (IAGENTNOTISINK * THIS, Long DWcharid, long x, long y, long dwcause) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_SIZE (IAGENTNOTISINK * THIS, long dwcharid, long lwidth, long lheight) {Return 1;}

HRESULT STDMETHODCALLTYPE IAGENTNOTIFYSINK_BalloonVisibleState (IAGENTNOTIFYSINK * THIS, long dwcharid, long bvisible) {return 1;}

static IAgentNotifySinkVtbl NotifySinkVtbl = {IAgentNotifySink_QueryInterface, IAgentNotifySink_AddRef, IAgentNotifySink_Release, IAgentNotifySink_GetTypeInfoCount, IAgentNotifySink_GetTypeInfo, IAgentNotifySink_GetIDsOfNames, IAgentNotifySink_Invoke, IAgentNotifySink_Command, IAgentNotifySink_ActivateInputState, IAgentNotifySink_Restart, IAgentNotifySink_Shutdown, IAgentNotifySink_VisibleState, IAgentNotifySink_Click, IAgentNotifySink_DblClick, IAgentNotifySink_DragStart, IAgentNotifySink_DragComplete, IAgentNotifySink_RequestStart, IAgentNotifySink_RequestComplete, IAgentNotifySink_BookMark, IAgentNotifySink_Idle, IAgentNotifySink_Move, IAgentNotifySink_Size, IAgentNotifySink_BalloonVisibleState }

Void InitiagentNotifySink (iAgentNotifyink * NotifySink) {notifysink-> lpvtbl = & notifysinkvtbl;

// Increase the reference count IAGENTNOTIFYSINK_ADDREF (NotifySink);

Void UninitiagentNotifySink (iAgentNotifyink * NotifySink) {// Release IAGENTNOTIFYSINK interface IAGENTNOTIFYSINK_RELEASE (NotifySink);

Notifysink-> lpvtbl = null;} / * <--------------------------------------- -----------------------------> * /

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ Procedure: Void showLastError (void) Purpose: Show recent error message Input: None Output: None ------- -------------------------------------------------- --------------- * / void showlasterror (void) {lpvoid lpmsgbuf;

FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) & lpMsgBuf, 0, NULL);

// Display the string. MessageBox (g_hwndmain, lpmsgbuf, "getlasterror", mb_ok | mb_iconification;

// free the buffer. Localfree (lpmsgbuf);

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ Procedure: BOOL IsValidagentVersion (iAgentex *) Purpose: The version of the Agent Server control installed in the system is required to require Input: iAGentex * - Specify Agent Server Control Output: True - Requires False - Do not require ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------- * / bool isvalidagentVersion (iAgentex * Pagent) {Short Smajor, Sminor;

/ / Get the version information of the Agent Server control in the system IAGENTEX_GETVERSION (PAGENT, & SMAJOR, & SMINOR);

IF ((Smajor> Agent_Version_major) || ((Smajor == Agent_Version_major) && (SMINOR> = Agent_Version_MINOR)) Return True;

Return false;}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ Procedure: BOOL AGCHR_ADDMENUS (iAgentCharacterex *) PURPOSE: Add Custom Menu Input: IAgentChacTerex * - Specify Agent Role Output: True - Add success FALSE - Add failed ------------------------------------------------------------------------------------------------------------------------------------------------------------ ---------------------------- / BOOL AGCHR_ADDMENUS (IAGENTCHARACTEREX * PEEDY) {iAgentCommandsex * agcmds = null; HRESULT HR; BOOL Bladded = True; static bstr bscmd = null; char buf [2] [buf_size]; int i; zeromem (buf);

/ / Get the IAGENTCOMMANDSEX interface hr = iAgentCharacterex_QueryInterface (PEEDY, & IID_IAGENTCOMMANDSEX, & AGCMDS); if (Failed (HR)) {Errstr (IDS_ERROR, BUF [0]); return false;}

LOADSTR (IDS_ERROR 1, BUF [0]); for (i = 0; i

// Release the IAGENTCOMMANDSEX interface IAGENTCOMMANDSEX_RELEASE (AGCMDS);

IF (! bladded) errmsg (buf [0]);

Return Bladded;}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ Procedure: BOOL AGCHR_LOAD (VOID) PURPOSE: Create an Agent Server instance and load the Agent role peedy. Input: None output: True - Load success FALSE - loading failed ------------------------------------- --------------------------------- * / bool agchr_load (void) {variant vPath; HRESULT HR; IDISPATCH * pdcharacter = null; idispatch * pdnsink = null; char buf [2] [buf_size];

ZeromeM (g_agmenu); g_agfile.dwid = 0; ZeromeM (g_agfile.wscap); ZeromeM (BUF); Coinitialize;

// Create an Agent Server Instance HR = CocreateInstance (& CLSID_AGENTSERVER, NULL, CLSCTX_LOCAL_SERVER, & IID_IAGENTEX, & G_AGENT); IF (Failed (HR)) {Errstr (IDS_ERROR 3, BUF [0]); return false;}

/ / Check the version of Agent Server (! IsvalidagentVersion (g_Agent)) {loadingStr (IDS_ERROR 4, BUF [1]); WSPrintf (buf [0], buf [1], agent_version_major, agent_version_minor); errmsg (buf [0 ]); RETURN FALSE;

// Initialize the IAGENTNOTIFYSINK Interface InitiagentNotifySink; & gg_agnsink;

// Register the IAGENTNOTIFYSINK interface hr = iAGENTEX_REGISTER (g_Agent, (iUnknown *) & g_agnsink, & g_lnotify; showLastError (); WSPrintf (buf [0], "HRESULT:% D", HR); errmsg (BUF [0]); IF (FAILED (HR)) {Errstr (IDS_ERROR 10, BUF [0]); Return False;}

// Load Agent Role Peedy LoadWStr (IDS_SMALL, G_AGFILE.WSCAP); Variantinit (& VPATH); // Initialize OLE Variable vPath.vt = vt_bstr; / / Indicate Variable Type of Unicode Strings vPath.Bstrval = SysallocString (g_agfile.wscap ); Hr = iAGENTEX_LOAD (g_agent, vpath, & (g_agfile.dwid), & g_lrequestid); VariantClear (& vpath); // Clear OLE Variable IF (Failed (HR)) {Errstr (IDS_ERROR 5, BUF [0]); Return false;}

/ / Get the IDispatch interface hr = iAGENTEX_GETCHARACTER (G_Agent, g_agfile.dwid, & pdcharacter); if (Ids_ERROR 6, BUF [0]); Return False;}

// Get IAgentCharacterEx interfaces hr = IDispatch_QueryInterface (pdCharacter, & IID_IAgentCharacter, & g_Peedy); IDispatch_Release (pdCharacter); // release IDispath if (FAILED (hr)) {ErrStr (IDS_ERROR 7, buf [0]); return FALSE;}

// Set the language type of the Agent role: Simplified Chinese HR = IAGENTCHARACTEREX_SETLANGUAGEID (g_peedy, 0x0804); if (Failed (HR)) {Errstr (IDS_ERROR 8, BUF [0]); return false;} // Add custom menu IF (! agchr_addmenus) {returnaf false;}

// peedy self-introduction agchr_selfintroduce (g_peedy);

Return True;}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ Procedure: Void Agchr_unload (void) PURPOSE: Uninstall Agent Role PEEDY, release the Agent Server instance. Input: None Output: None ------------------------------------------------------------------------------------------------------------------------------------ ---------------------------- * / void agchr_unload (void) {HRESULT HR; char buf [buf_size];

ZeromeM (BUF);

IF (g_peedy! = null) {// iAgentCharacterex_stopall (g_peedy, 0xffffff); IAGENTCHARACTEREX_HIDE (g_peedy, true, & g_lRequestid); agentcharacterex_release (g_peedy);}

IF (g_Agent! = null) {if (g_agfile.dwid! = 0) {hr = agentex_unload (g_Agent, g_agfile.dwid); // Uninstall the animated character data IF (Failed (HR)) {Errstr (IDS_ERROR 9, BUF }}

/ / Dance the IAGENTNOTIFYSINK interface if (g_lnotifysinkid! = 0) IAGENTEX_UNREGISTER (G_Agent, g_lnotifysink); UninitiaGentNotifyink (& g_agnsink);

IAGENTEX_RELEASE (G_Agent); // Release Agent Object}

Couninitialize ();

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ Procedure: Void Agchr_Play_Speak (iAgentCharacterex *, BSTR, BSTR) PURPOSE: Agent Role Play Action, and say a knot. Input: iAgentCharacterex * - Specify Agent Role BSTR - Specify Action BSTR - Specify Word Output: None ------------------------------- ----------------------------------------- * / void Agchr_play_speak (iAgentCharacterex * peedy, BSTR BSAction, BSTR BSWORDS) {Static Bstr Bsbuf = NULL;

if (bsAction = NULL!) {bsBuf = SysAllocString (bsAction); IAgentCharacterEx_Play (peedy, bsBuf, & g_lRequestID); SysFreeString (bsBuf);} if (! bsWords = NULL) {bsBuf = SysAllocString (bsWords); IAgentCharacterEx_Speak (peedy, bsBuf , Null, & g_lrequestid; sysfreeestring (bsbuf);}} / * ----------------------------------- ------------------------------------- procedure: void Agchr_selfintroduce (iAgentChacTerex *) Purpose: Agent Role Self Introduce. Input: iAgentCharacterex * - Specify Agent Role Output: None --------------------------------------- --------------------------------- * / void agchr_selfintroduce (iAgentChacTerex * peedy) {wchar wswords [buf_size];

//Move the character and make it it is visible agentcharacterex_moveto (PEEDY, 200, 200, 1000, & g_lrequestid); IAGENTCHARACTEREX_SHOW (PEEDY, FALSE, & G_LREQUESTID);

// And play an intro animations LoadWStr (IDS_WORDS, wsWords); AgChr_Play_Speak (peedy, L "GetAttention", wsWords); LoadWStr (IDS_WORDS 1, wsWords); AgChr_Play_Speak (peedy, L "Greet", wsWords); LoadWStr (IDS_WORDS 2, WSWORDS; agchr_play_speak (peedy, l "explain", wswords; agchr_play_speak (peedy, l "restpose", null;}

转载请注明原文地址:https://www.9cbs.com/read-49474.html

New Post(0)