Asp. NET application tips: The use of unmanaged COM components

Source: Internet
Author: User
Tags net command

As is well known, ASP. NET is created from the common language runtime, which is known as the managed execution environment. The generated code is called managed code. The compiler can generate metadata information from the description of the source code, and the runtime obtains managed code information from the metadata. And the components we write are typically created in an unmanaged environment, and these components often don't contain metadata information, so how do we use that metadata information in managed code?

In order to use unmanaged code in a managed environment, we first have to manually generate metadata information for these existing components. There must be a tool that can produce metadata. The ability to get metadata from somewhere. The source of this metadata is the type library, in fact, all components today have a type library.

To illustrate how to produce metadata information, we may wish to take the Microsoft Agent component as an example. This component is similar to Office's small assistant, it can complete a variety of colorful human actions, and support speech recognition and speech synthesis. The general operating system is installed by default this component, users should not worry about without this component. If your system removes this component, you can go to http://microsoft.com/msagent/downloads.htm to download it. Here's how to use unmanaged COM components in ASP.

1, the first is to create metadata

The Microsoft agent control is installed by default in the%systemroot%\msagent\ directory, first running the Start menu in the Visual Studio.NET command Prompt (Command Prompt) menu item. Execute the following command in this directory.

TlbImp Agentctl.dll/out:agentctl_proxy.dll

With this command we can create a proxy file Agentctl_proxy.dll that contains metadata information. Note that this is not a real dynamic connection library file, but rather an assembly file (Assembly), which is the proxy for our unmanaged COM component.

can also be passed. NET Framework provides the utility AxImp.exe Create, command line as follows, so that it generates two files as shown in: Note AgentObjects.dll and Agentctl_proxy.dll files are exactly the same file.

C:\Program Files\Microsoft Visual Studio 9.0\vc>aximp%systemroot%\msagent\agentctl.dll

2. Install to ASP.

There are two ways to install a metadata file into an ASP. NET Environment:

One is to copy the files to the application's Bin directory.

The second is to copy it to the ASP.

There is a flaw in the second approach, and you must insert a @assembly statement in each ASP. NET page that uses the component. This can be avoided by saving to the bin directory. Assembly will be loaded into all ASP. NET pages of the application. So we'd better put it in the bin directory.

3. Il disassembly (disassembler)

Now it's almost time for most VB or VBScript programmers to have a headache, and we're going to use a disassembler to generate some important information.

Assembly namespace (using @Import statement)

The exact class name (which can be quite different from ASP!)

Data types for methods and properties

Exact spelling of methods and properties. Note that in C #, Bodytext and Bodytext are two completely different variables.

Generating the above information requires the use of IL (intermediate Language) Disassembler intermediate Language disassembler, which you can type Ildasm.exe start the tool after you generate the metadata. You can then use File | Open, select the appropriate file and display the results very similar to the ole/com viewer to get the results.

Note that the generated assembly files are different, the names of namespaces are not the same, and in an ASP. NET page, you often need to use @Import to import namespaces, but their classes, properties, and methods are the same. For ASP. NET programmers, the whole blue symbol is important because they represent a class or an interface. If we expand on these classes or interfaces, we get a list of properties and methods.

All of the methods we see have an accurate description of the data type. The first is the method name, and the colon is followed by the return value type in parentheses, which is the parameter required by the method.

When we talk about describing attributes using the get_ and set_ methods, VBScript programmers may confused, and we know that each property actually corresponds to a private member of the class, and that the read and write of the property is implemented by both functions.

Each property has a red mark next to it, double click on this property, we can get the definition of the property, we can see the property can be get, Set or Get/set.

4, the use of components

When we understand the metadata clearly, the rest of the work is how to use this component in ASP. The following is a code that uses msagent:

<% @Page language= "C #"%>

<% @Import namespace= "Agentobjects"%>

<% axagentobjects.axagent axagent=new axagentobjects.axagent ();

Iagentctlcharacterex Character;

AxAgent.Characters.Load ("Genie", (object) "Genie". ACS ");

Character = axagent.characters["Genie"];

Character.languageid = 0x409;

Display the character. Character.show (NULL);

Character.speak ("Hello World", null);

Response.Write (Character.name);

Character.play ("Wave");

Character.play ("Hide");

%>

After importing the type library, we will then create an instance of this class, with the remainder being basically the same as the ASP application component.

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.