Use an assembly in a hosted environment in an unmanaged environment)

Source: Internet
Author: User
Tags export class hosting

 

Use an assembly in a hosted environment (for conversion) in an unmanaged Environment)

From: http://www.wwit.cn/www/down/Article.Asp? Id = 790

2 recommended


From. after the advent of net, the VC ++ and vbprogramming models we used to have had a great transformation. The concept of hosting and non-hosting is constantly coming into the eyes of programmers. We can say that. net is gradually becoming one of the mainstream programming languages and platforms. Today, I will not discuss with you who are willing to talk about the ups and downs, or whether this programming model (or method) is good or not, all programmers will discuss and confirm it in practice! Today, I have encountered the issue of mutual Assembly calling in hosting and non-hosting environments. I think we should share with you that no matter how simple it is, I just hope that in the ever-changing development model era, everyone must grasp the changing characteristics of things. I would like to share with you the following!
The Microsoft. NET environment allows unmanaged clients to transparently call. Net Assembly through the encapsulation classes generated by the Microsoft. NET environment ). The com encapsulation class that can be called is actually a proxy of the Assembly in the hosting environment. It is generated by the metadata contained in the Assembly. The original data is mainly used to describe Assembly data such as class declaration, method, and version information (similar to the TLB file of COM ). Therefore, we can use encapsulated classes to easily call the assembly in the hosted environment like using COM components.

COM Client cannot directly use. Net assembly. When a call action occurs, the. NET public language runtime generates a proxy class for the hosted object. The main role of this proxy class is to schedule between hosted code and unmanaged code. This is similar to the external al of COM.
The encapsulation class retains the reference counting style of traditional COM objects. When the reference count is zero, the encapsulation class Automatically releases the reference to the hosted assembly. When the managed assembly is released, it is also calibrated as a collection object by the. NET garbage collector.
Type Library exporter (tlbexp.exe) is a command line tool provided by the. NET environment. This tool can be used to export class libraries (*. TLB files) in the. NET hosting environment ). Tlbexp.exe is a TLB file created based on metadata in the hosted environment assembly. Applications with unmanaged code use this TLB file to bind an assembly in the. NET environment. Since one. Net assembly may also reference other assembly
Tlbexp.exe may generate one or more TLB files. The tlbexp.exe file can only generate the TLB file, but is not registered for it. To register a registry, use the regasm.exe tool, which is a command line-based tool.
The tlbexp.exe tool changes each class with public attributes to the coclass of the Type Library. The coclass name is consistent with the class name in the program set. All displayed interfaces are output to the Type Library. The methods and attributes of classes that implement interfaces will not be output to the Type Library.
After being exported by tlbexp.exe, the methods and attributes of interfaces and interfaces in the hosted environment are converted to com interfaces. By default, the COM component supports double interfaces. You can use classinterfaceattribute to set interface attributes.
For example:
Public interface ihello
{
Void sayhello ();
}
Public class imp_hello: ihello
{
Public void ()
{
System. Console. writeline ("Hello World ");
}
}
The tlbexp.exe tool generates the following IDL (Interface Definition Language ):
[UUID (xxxxxxxxxxxxxxxx), dual, odl, oleautomation]
Interface ihello: idispatch
{
Hresult sayhello ();
}
[UUID (xxxxxxxxxxxxxxx)]
Coclass imp_hello;
{
Interface ihello;
}
Note: because we want to export interfaces and implementation classes, the above section C # code uses the public keyword to modify interfaces and implementation classes. Otherwise, even if a TLB file is generated, it does not contain descriptions about interfaces and implementation classes.
Before you call an assembly in an unmanaged environment, you must register the assembly to the system registry .. Netenvironment uses regasm.exe to complete registration.
The registration tool adds the following items to the registry:

The identifier CLSID of a class in the. NET hosting environment is added to hkey_classes_root \ CLSID.

Another table item in hkey_classes_root \ CLSID \ <class I D> has two sub-items,
One is implementedcategories, which usually places the progid of the class, and the other is the name used by inprocserver32 to specify. dll.
When the com client attempts to load a class of a hosted environment assembly, the. NET runtime environment reads the Assembly data from the system registry and sends it to the Assembly parser. The parser then locates the Assembly based on the basic information such as the name and version number. This assembly can be correctly located by the Assembly parser only when it is strongly named (strong name) or placed in the root directory or subdirectory of the application. Note: When you do not use a strong name, add the/codebase parameter during registration.

The process of using. Net Assembly through Visual Basic 6.0:
1. In the project menu of Visual Basic 6.0, click references
The references dialog box is displayed.
Pull the scroll bar and find the entry to be referenced. OK
Create an assembly class object in the. NET environment, as follows:
Dim server as XXXXX
Object Method

In Visual C ++ 6.0, you can simply and directly import the TLB file as follows:
# Import "xxxxxx. TLB"
Note that the named_guids attribute must be added after the import; otherwise, the guid of the interface will not be defined in the project.

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.