C # based Interface Basics Tutorial Six

Source: Internet
Author: User
Tags definition command line include interface net ole access visual studio

Section sixth, Interface conversion

C # supports not only the. Net platform, but also the COM platform. To support COM and. net,c# contains a unique language attribute called attributes. A property is actually a C # class that provides meta information by modifying the source code. property enables C # to support specific technologies, such as COM and. Net, without interfering with the language specification itself. C # provides an attribute class that converts a COM interface to a C # interface. Other attribute classes convert a COM class to a C # class. Performing these transformations does not require any IDL or class factories.

any COM components that are deployed now can be used in interface transformations. Normally, the required adjustments are made entirely automatically.

in particular, you can use the Run-time callable Wrapper (RCW) to access COM components from the. NET framework. This wrapper converts COM interfaces provided by COM components to interfaces that are compatible with the. NET Framework. For OLE Automation interfaces, the RCW can be automatically generated from the type library, and for non-OLE automation interfaces, developers can write custom RCW that manually map the type provided by the COM interface to a type that is compatible with the. NET Framework.

Referencing COM components using ComImport
COM Interop provides access to existing COM components without having to modify the original components. Using ComImport To reference COM components often involves the following issues:

1, create COM objects.

2, determine whether the COM interface is implemented by the object.

3, invoke the method on the COM interface.

4. Implements objects and interfaces that can be invoked by COM clients.

Create a COM class wrapper

to make C # code reference COM objects and interfaces, you need to include the definitions of COM interfaces in C #. The easiest way to do this is to use TlbImp.exe (type Library Importer), which is a command-line tool that is included in the. NET Framework SDK. TLBIMP converts a COM type library to. NET Framework metadata, effectively creating a managed wrapper that can be invoked from any managed language. The. NET framework metadata created with TLBIMP can be included in the C # build with the/r compiler option. If you are using the Visual Studio development environment, you simply add a reference to the COM type library, and this transformation is done automatically for you.

TLBIMP performs the following transformations:

1. COM coclass is converted to a C # class with parameterless constructors.

2. The COM structure is converted to a C # structure with public fields.

a good way to check TLBIMP output is to run the. NET Framework SDK Command line tool Ildasm.exe (Microsoft Intermediate Language disassembler) to view the results of the conversion.

Although TLBIMP is the preferred method of converting a COM definition to C #, it is not available at any time (for example, you cannot use this method when there is no COM-defined type library or when TLBIMP cannot handle the definition in the type library). In these cases, another method is to use the C # property to manually define the COM definition in C # source code. After you create a C # source map, you can generate managed wrappers by simply compiling the C # source code.

The main attributes that you need to understand to perform COM mappings include:

1, ComImport: It marks the class as an externally implemented COM class.

2. Guid: It is used to specify a universal unique identifier (UUID) for a class or interface.

3, InterfaceType, which specifies whether the interface derives from the IUnknown or from the IDispatch.

4, PreserveSig, which specifies whether native return values should be converted from HRESULT to. NET Framework exceptions.
Declaring COM coclass

COM coclass is represented as a class in C #. These classes must have the ComImport property associated with them. The following restrictions apply to these classes:

1. A class cannot inherit from any other class.

2, the class can not implement any interface.

4. The class must also have a GUID attribute for which the globally unique identifier (GUID) is set.

The following example declares a coclass in C #:

Declare a COM class Filgraphmanager
[ComImport, Guid ("e436ebb3-524f-11ce-9f53-0020af0ba770")]
Class Filgraphmanager
{ } 

The C # compiler adds a parameterless constructor that can be called to create instances of COM coclass.

Total 4 page: prev 1 [2] [3] [4] Next page



Related Article

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.