Microsoft Agent's COM Interface Programming

zhaozj2021-02-08  348

Microsoft Agent has a wide range of purposes that we can use it to normal applications for local systems, or embed it into the HTML document for Internet / Intranet. Microsoft Agent supports multiple programming languages ​​such as C / C , Visual Basic, Java, JScript, and VBScript, and provides programmers with two programming methods for OLE automation servers and ActiveX controls. From essentially, these two programming methods belong The category of OLE technology is based on the COM (Component Object Model, component object model). With VC MFC class or VB, the ActiveX programming tool can easily call the ActiveX control, but the ActiveX control hides the details of many OLE technology. If we want to deepen the understanding of a COM object, Use its COM interface to program, starting from this point, this article will introduce the basic programming method of the COM interface of Microsoft Agent, and hope to play the role of tile jade.

OLE Programming Basic Knowledge

Early OLE (now OLE 1) In Windows 3.1, its primary use is to generate a composite document such that a document of an application can include data (objects) of other applications by chain or embedded. As software component technology has become increasingly important, Microsoft designs OLE 2 on the OLE 1, using it to implement two-way grade-reusable software components, and control the version of these components and expand its functionality. . Since the architecture of OLE 2 is designed to become open, OLE 3 or 4 will not appear again in the future. After years of development, Today's OLE has all technologies such as OLE automation, COM, COM , DCOM, and ActiveX, which are ActiveDirectory (a key technology that will be used for NT 5.0), Ole Messaging, DirectX, Active Controls, ActiveX Scripting and Task Scheduler, etc., OLE is no longer an abbreviation of Object Linking and Embedding, which has become a separate word, specifically used to represent Microsoft's software component integration technology.

COM is the foundation of OLE technology. It specifies how objects are communicating with each other, and it is also called COM objects. According to COM, any language can be written within the object, which communicate with the outside world through the interface. The so-called interface refers to a set of specific function calls (methods) provided by the object. Each object can have multiple interfaces, and different objects can implement the same interface, the client program to call the object through the object's interface pointer. Since the OLE specifies that the component is reused on the binary stage, the client cannot directly access the data inside the object, and the properties of the read or setting object are also performed by the interface. Each interface is inherited from a name called an IUNKNOWN interface, both of the three methods of IUNKNOWN: QueryInterface, addRef, and release, the client call QueryInterface can get other interface pointers of the object, addRef and release respectively The reference count adds one and minus one. When the reference count is zero, the object will be released. The general step of customer program calling COM object is to first create an object, then get the required interface pointer, call the corresponding function, finally release the interface pointer and object.

Basic approach to c program calls Microsoft Agent

Based on the basics previously introduced, let's take a look at how to call Microsoft Agent in the C program. Setting and option

The programming tool used in this article is Visual C 5.0, the program is a general WIN32 application, in order to make the program correctly compile the connection and run, you first need to have two files for the COM interface for the NOSOFT Agent of Agtsvr.h and AGTSVR-IC. They can be in Microsoft's MS Agent site (http://www.microsoft.com/workshop/

PROG / AGENT / I found, or download Microsoft's latest Internet client SDK or Platform SDK, followed by adding the following libraries in the Project / Settings / Link menu: OLE32.LIB, Oleaut32.lib, uuid.lib, ODBC32 .lib odbccp32.lib, finally ensures that Microsoft Agent and animated characters are installed in the system.

2. Create a Microsoft Agent object

To create an OLE object, you need to initialize OLE, which is done by the oleinitialize () function. If the OLE initialization is unsuccessful, then the following code cannot be continued, and the object is created by the COCREATEINSTANCE () function:

IF (Failed (Oleinitialize (NULL))) Return -1; // Initialization OL

HRES = COCREATEINSTANCE (CLSID-AgentServer, Null, ClsctX-Server, IID-IAGENT, (LPVOID *) & Pagent); // Create an instance of Microsoft Agent Server

IF (Failed (HRES) RETURN -1;

The first parameter of COCREATEINSTANCE () is an object's CLSID (class code), Microsoft Agent Server's CLSID to define CLSID-AgentServer in the AgtsVR-IC file, this 128-bit encoding uniquely identifies the Agent server, the server is located And the information such as run parameters is placed in the system registry; the second parameter is in general to null; the third parameter is used to indicate the operational environment of the object, such as remote or local, which is set to CLSCTX-Server; The four parameters indicate the ID of the interface used to communicate with the object, which is also a 128-bit encoding. The interface ID of the agent is IID-IAGent; the fifth parameter is used to receive the interface pointer of the IAGent.

If the Microsoft Agent Server has not run in memory, CoCreateInstance () will start it and create an Agent object if the server has run, then CoCreateInstance () will connect with it and create an Agent object. When all Agent objects are released, the server exits automatically.

3. Mount animation

The following code calls the IAgent :: Load () method to load the data of an animated person, because the Agent server runs in its own memory space, the transmitted string variable needs to allocate memory: SysallocString ():

Variantinit (& VPATH); / / Initializing the OLE variable

vpath.vt = vt-bstr; / / indicates a string of the variable type Unicode

vpath.bstrval = sysallocstring (kpwszcharacter);

// kpwszcharacter is the storage path of the animated character data hres = Pagent-> Load (vPath, & lCharid, & lrequestid);

// Load data, the character ID returns in LCharid

HRES = PAGENT-> GetCharacter (Lcharid, & Pdcharacter);

/ / Get the IDispatch interface pointer call idispatch :: queryInterface () method to get // iAgentCharacter interface pointer:

HRES = PDCharacter-> queryinterface (iid-iAgentcharacter, (lpvoid *) & pcharacter);

PDCharacter-> Release (); // Release IDispath

The various methods supported by animated characters can be called through the IAgentCharacter interface:

HRES = PCharacter-> show (false, & lrequestid); // Show animation characters

HRES = PCharacter-> MoveTo (320, 240, 100, & lrequestid); // Mobile animation people to the center of the screen

Bszspeak = sysallocstring (l "Hello World!"); // Assign strings

HRES = PCharacter-> Speak (bszspeak, null, & lrequestid); // Let the animated characters speak

Sysfreestring (BSZSpeak); // Release the string of memory

4. Release object

The program needs to release the created Agent object before exiting:

IF (pCharacter) {

PCharacter-> release (); // Release the IAGENTCHARACTER interface

Pagent-> unload (lcharid); // Uninstall the animated character data

}

Pagent-> Release (); // Release the Agent object

VariantClear (& vpath); / / Clear the OLE variable

Further programming points

The previous introduction is the most basic steps to call the Microsoft Agent server. In order to complete the actual tasks, the client should also consider some of the programming points below according to their own situation.

1. Check the version of Agent Server

OLE requires components or objects with backward compatibility, high version objects support all interfaces and properties of low version objects, which can easily upgrade components. The client should usually check the object's version, and the object can only be called when the version number of the object installed in the system is higher or equal to the desired version number. The following isvalidAgentVersion () function checks the version number of the Microsoft Agent and compares it with the version of the definition in the Agtsvr.h file:

Bool isvalidagentVersion (iAgent * Pagent) {

Idispatch * pdagent = null;

ITYPEINFO * ptypeinfo = null;

ITypelib * ptypelib = null;

TLIBATTR * ptypelibattr = null;

Bool bvalid = false;

Uint uiindex;

Pagent-> queryinterface (iid-idispatch, (lpvoid *) & pdagent); pdagent-> gettypeInfo (0, 0, & ptypeinfo); // acquisition type information

PtypeInfo-> getContainingTypelib (& Ptypelib, & UiIndex); // Get Type Library

ptypelib-> getlibattr (& ptypelibattr); // Get the properties in the type library

IF ((ptypelibattr-> wmajorvernum> Agent-Version-Major) || ((ptypelibattr-> wmajorvernum == agent-version-major) && (ptypelibattr-> wminorvernum> = agent-version -minor))))))))

BVALID = true; // The expected version number is defined in the Agtsvr.h file

IF (ptypelib) {

IF (ptypelibattr) ptypelib-> releaselibattr (ptypelibattr);

Ptypelib-> Release ();

IF (ptypeinfo) ptypeinfo-> release ();

IF (pdagent) pDagent-> release ();

Return bvalid;}

2. Implement the IAGENTNOTIFYSINK interface

In order to process the user's input, understand the status of the Agent object, the client should implement the IAGENTNOTIFYSINK interface to receive an event of the Agent object. IAGENTNOTIFYSINK declarations and default implementations can be found in Notify.h and notify.cpp in Platform SDK or Internet Clinet SDK, and client programs should modify certain event processing functions as needed.

. The following code registers the IAgentNotifySink interface to the Agent object, where AgentNotifySink is inherited from IAGENTNOTIFYSINK:

Psink = new agentNotifysink;

psink-> addref (); // increase the reference count

HRES = PAGENT-> Register (iUnknown *) psink, & lnotifysinkid; // registration

...

IF (psink) {

Pagent-> unregister (lnotifysinkid); // Logout IAGENTNOTIFYSINK interface

Psink-> release ();

The two events most interested in the client are RequestComplete and Command. The Agent server uses asynchronously to handle various requests for the client, so that the client can make your own work while requiring the service. When the server completes a request, it will inspire the RequestComplete event, and the client can determine which request is Already ended and processed. The Command event is excited when the user uses a mouse or microphone to issue a command to the animation, and the client program can understand the specific information of the command through the IAGENTUSERINPUT interface.

3. Custom command

The Agent server provides some default commands for each animated person. These commands appear in the Association menu or command window, which can add custom commands to the IAGENTCOMMANDS interface. In order to get the interface pointer of the IAGENTCOMMANDS, you should use the parameter IID-IAGENTCOMMANDS to call the iAgentCharacter :: queryinterface (), iAgentCommands's add () or INSERT () method can join the custom command, and set the CAPTION, Visible, and Voice properties to indicate the command. Whether it is displayed, displayed in the associated menu or in the command window.

The Agent server gives each command to a ID value, the client can use this ID value to call the IAGENTCOMMANDS :: getcommands :: getcommands :: getCommand () method to get the IAGENTCOMMAND interface pointer of each command, thereby adjusting the various properties of a single command.

4. WAV file instead of voice synthesis

Microsoft Agent currently only supports English speech-integrated functions. You can only use WAV files when you output Chinese. If the path to the IAGENTCHARACTER :: Speak () method passes the path of a WAV file, then the Agent server automatically plays this WAV file, and display the text contained in the first parameter in the text balloon, if give to the second The parameter passes a path to the LWV file with audio festival, and you do not need to provide text in the first parameter because there is a text message in the LWV file. When using the LWV file, the nozzle action of the animation person can be consistent with the output of the voice, so it should be used to use the LWV file as possible in the possible situation, which can be generated by Microsoft Agent Linguistic Information Sound Editing Tool to build a WAV file.

5. Other COM interfaces

In addition to the previously mentioned interfaces, Agent servers have other COM interfaces. IAGENTCOMMANDWINDOW allows the client to access or set the properties of the command window, including location, size and whether it is visible. IAGENTSPEECHINPUTPROPERTIES allows the client to access the properties of the voice input function, most of which are read-only. IAGENTAUDIOOOUTPROPERTIES allows client programs to read partial properties of the speech output function. IAG

ENTPROPERTYSHEET allows the client to access or set an agent table of the Agent server. IAGENTBalloon allows the client to access the properties of the text balloon, which can set a few properties, such as whether it is visible and the font name. For specific definitions and purposes for these interfaces, please refer to the help documentation of Microsoft Agent.

to sum up

Microsoft Agent is a newer technology that belongs to OLE, which involves deeper programming theory. This article is only the most basic method of use from the OLE automation service perspective, and interested readers can refer to Microsoft Press Publishing. The "Inside OLE" (second edition) one book further understands the knowledge of OLE programming and participates in Microsoft Agent News Discussion group (the news server is msnews.microsoft.com, discussion group is Microsoft.Public.msAgent).

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

New Post(0)