Let COM use. NET assembly

Source: Internet
Author: User
Tags constructor visual studio
In addition to providing back-compatible,. NET Framework also supports forward compatibility, which is meant to be exposed. NET assembly is possible for COM utilization. Now let's see how we can get COM to leverage. NET assembly.

Calling. NET assembly from COM


Now let's look at the principle of using. NET assembly from COM, as shown in Figure A.
Figure A


Using. NET assembly from COM


Similar to the process of. NET calling COM objects, here we are. NET assembly uses a package class that you can access through a COM application. This encapsulation class is called the COM Callable Encapsulation (CCW). Speaking for a long time, how can we use Visual Studio.NET to achieve these goals?

Start the Visual Studio.NET first, and then create a new item in the class library type, the name is dotnetforcom (for example, you can certainly name yourself). Add a default constructor and some public methods to the new class that should be available in COM. Once you are ready to do so, right-click the item in Solution Explorer and select the Properties pop-up Item property page. Then select the Configuration property and build option on the property page. So you will appear in front of the picture as shown in Figure B.
Figure B
Using Property Pages


Select Register for COM Interop (shown in Figure B) to check the box. Now, when you construct the project. NET assembly automatically registers COM Interop. So you can use the assembly via COM. For example, when you are programming with Visual Basic 6.0, if you start a new Visual Basic 6 project and choose the Project menu and references, then. NET assembly can be referenced via COM (see Figure C).
Figure C

Visual Basic Project References

So far, you can access it by Visual Basic 6. NET assembly to provide functionality.

Outside of Visual Studio.NET


Here's what we'll discuss when we don't use Visual Studio.NET. At this point to let. NET assembly is exposed to COM with additional requirements.

Provides a default builder


First of all. NET assembly must provide a default constructor. This is because the COM customer does not support it. NET parameterized constructor, so you have to make sure you provide the default constructor for the class. You can also write a constructor with a parameter with the default constructor, but the default constructor needs to instantiate the. NET assembly from a COM client.

Build Type library


Now for. NET assembly to build the type library and generate relevant entries in the system registry. The following two ways can be used:
· Use the Type Library Exporter tool provided by the. NET Framework SDK to generate type libraries for. NET assembly. Then register the type library with the Assembly Registration tool.
TlbExp test.dll/out:test.tlb Regasm Test.dll
• Generate a type library directly and register it with RegAsm.
Regasm test.dll/tlb:test.tlb

The. NET assembly must have a strong name and reside in the global Assembly cache. To do this you need to use the Strong Name tool (Sn.exe) to generate a registry key value for the assembly. After completing this operation, the GAC tool (Gacutil.exe) is used to add the assembly to the global assembly cache. You can also add the assembly to the GAC using the following statement:
Gacutil–i SampleAssembly.dll

You can then add a reference to the. NET assembly and apply it to it by COM. The CCW is generated when the. NET assembly is invoked by COM, and it acts as a bridge between the managed and the non regulated domains.

Modifying collection behavior with attributes


Property (Attributes) can be used to modify the collection (marshaling) behavior that is applied by the aggregator (marshaler). These attributes are grouped into 3 types, including:
    1. The attributes that you apply at design time.
    2. Properties applied by the interop tool
    3. Attributes that are applied by you or interop.

GuidAttribute, Progldattribute, MarshalAsAttribute, and ComVisibleAttribute are the most commonly used properties.
GuidAttribute and ProgIdAttribute are used to set the GUID and ProgID of the class.

MarshalAsAttribute is optional. Because all data types have their default collection behavior. In the case of data types, such as String types, this type of data is perfectly possible as a collection of types, and this property is essential. The string data type of the NET Framework means the following types under no managed code:
    • LPStr
    • LPWStr
    • LPTStr
    • BStr

The default behavior is to bring the string together as a BSTR. This property can be used to modify the above behavior for any other type of pipe that is not managed.
ComVisibleAttribute is used to control the visibility of types within the assembly. The public type within the assembly is visible by default. So the attribute is used to hide certain types.

For a full understanding of attributes, see MSDN.

Meaning of Interop


It is also important to remember that invoking COM objects from. NET affects the performance of the system, and vice versa, because of the aggregation. Again, this performance impact has nothing to do with the data type. Simple data types, such as integers and bytes, do not affect performance. The reason is that such data types are the same in both managed and out of memory, so interop tasks are the same. The influence of the collection behavior was minimized. The string data type degrades performance because it represents a difference in the managed and not-managed memory. A simple data type, that is, a data type that has the same representation in a managed and blittable code, is called a data type, and other data types that have an nonblittable are called the data types. Interop marshaler provides support for all built-in data types. For complex data types, you need to manually edit the MSIL code.

Take the tested code


. NET Framework provides the appropriate implementation capabilities for interoperability between existing COM and emerging. NET assemblies. These functions are implemented by encapsulating the class, which is like a bridge between the managed and the managed code. Developers then decide whether to use interop and where to write new code. Our principle is to try to use tested code instead of writing new code that requires testing.

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.