COM interface programming using the Microsoft Agent

Source: Internet
Author: User
Tags object model ole

The Microsoft agent has a fairly wide range of uses that we can either add to a common application for use by the local system, or embed it in an HTML document for internet/intranet use. The Microsoft agent supports a variety of programming languages, such as C + +, Visual Basic, Java, JScript, and VBScript, and provides programmers with two programming methods for OLE Automation servers and ActiveX controls, essentially Both of these programming methods are in the category of OLE technology and are built on the basis of COM (Component object model, component objects models). Using VC + + MFC class Library or VB, and other support ActiveX programming tools can easily invoke ActiveX control, but the ActiveX control is a lot of OLE technical details are hidden, if we want to deepen the understanding of a COM object, You should use its COM interface directly to program, from this point, this article will introduce Microsoft Agent's COM interface Basic programming method, hope to be able to play a role.

Basic knowledge of OLE programming

The first time OLE (now called OLE 1) appears in Windows 3.1, the primary purpose of which is to generate a composite document that enables a document of an application to contain data (objects) of other applications in a chained or embedded manner. As software component technology becomes increasingly important, Microsoft is in OLE 1

, OLE 2 is designed to enable reusable software components at the binary level, and it is fairly easy to control the versions of these components and extend their functionality. Because the architecture of OLE 2 is designed to be open and extensible, OLE 3 or 4 will not appear again. After years of development, OLE now includes a number of 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, the foundation of many new technologies, OLE is no longer an acronym for Object linking and embedding, it becomes a separate word, Specifically used to represent Microsoft's software component integration technology.

COM is the foundation of OLE Technology, which prescribes how objects communicate with each other, and objects that conform to COM specifications are also called COM objects. According to COM, objects can be written in any language, using interfaces (Interface) to communicate with the outside world. An interface is a set of specific function calls (methods) provided by an object, each object can have multiple interfaces, different objects can implement the same interface, and the client program invokes the object's function through the object's interface pointer. Because OLE stipulates that components are reusable at the binary level, client programs cannot directly access the data inside the object, and the properties of the object are read or set through an interface. Each of these interfaces is inherited from a IUnknown interface, and must be implemented in IUnknown three ways: QueryInterface, AddRef, and release, client invocation QueryInterface can get other interface pointers to objects , AddRef and release add an object's reference count plus one and minus one, and the object is freed when the reference count is zero. The general step for a client to invoke a COM object is to first create an object, then obtain the desired interface pointer, invoke the appropriate functionality, and finally release the interface pointer and object.

Basic method for C + + program to invoke Microsoft Agent

Based on the basics described earlier, let's look at how the Microsoft Agent is invoked in a C + + program.

1. Settings and Options

The programming tools used in this article are Visual C + + 5.0, programs for general WIN32 applications, and to enable programs to compile connections and run correctly, you first need to have AgtSvr.h and agtsvr-i.c two files that define COM interfaces for Microsoft agents , which are available at Microsoft's MS Agent site (http://www.microsoft.com/workshop/

prog/agent/, or download Microsoft's latest Internet Client SDK or Platform SDK, and then add the following library to the Project/settings/link menu: Ole32.lib, Oleaut32.lib, Uuid.lib, Odbc32.lib Odbccp32.lib, and finally make sure that the Microsoft Agent and animated character data are installed in the system.

2. Create Microsoft Agent Object

OLE objects need to be initialized before the OLE object is created, which is done by the OleInitialize () function, and if OLE initialization is unsuccessful, the following code cannot be continued, and the creation of the object is done by the CoCreateInstance () function:

if (FAILED(OleInitialize(NULL))) return -1;//初始化OLE
hRes = CoCreateInstance(CLSID—AgentServer,NULL,CLSCTX—SERVER,IID—IAgent,(LPVOID *)&pAgent);//创建Microsoft Agent Server的实例
if (FAILED(hRes)) return -1;

The first parameter of CoCreateInstance () is the CLSID of the object (class code), and the CLSID of Microsoft Agent Server is the clsid-agentserver defined in the Agtsvr-i.c file. This 128-bit encoding uniquely identifies the agent server, where information such as the path and running parameters of the server is placed in the system registry, and the second parameter is generally null; the third parameter is used to indicate the environment in which the object is running, such as remote or local, which is set to Clsctx-server The fourth parameter indicates 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, and the fifth parameter is the interface pointer used to receive the iagent.

If Microsoft Agent Server is not already running in memory, then CoCreateInstance () starts it and creates an Agent object, and if the server is running, CoCreateInstance () is connected to it and an agent object is created. When all the agent objects are freed, the server exits automatically.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.