Simple Example of interaction between COM and. net

Source: Internet
Author: User

This article uses VB6 as an example to introduce two simple examples of. Net referencing COM and COM referencing. net.

<1>. Net calls com

In order to be simple, we use VB6, which is said to be an old antique about open source, to create a simple COM.

Create an ActiveX dll project in VB6, and there is only one class comc1c. CLs,CodeAs follows:

Note: Set compatibility and generate DLL

Then, the console project was created in vs2010. Microsoft was very surprised that the project in VB6 was called a project, and it was renamed "project" since vs2002 ". Add a reference to the created com

Running effect:

Maybe many people will be curious about this section of C # code. When will this _ comc1c be generated? Why does it call a method instead of a class object?

First, let's take a look at what vs ide has done for us after referencing com.

Please note that, InterOP. simplecomserver. dll is a "interoperability" generated by IDE for us.ProgramAnd it is the bridge between COM and. net.

Through the object view, we can see that _ comc1c is generated in this DLL. We can use oleview.exe to check the typelib of InterOP. simplecomserver. dll.

If you have installed VB6, you can find oleview.exe in the vbdirectory and run it. If you have not installed oleview.exe

E: \ Program Files \ microsoft sdks \ windows \ v7.0a \ bin

Find simplecomserver and double-click

Based on COM rules:The only way for the COM Client to interact with the com class is to use interface reference instead of object reference.

If a C ++-based COM Client is created, you need to pay special attention to the query process for a specific interface. If the interface is no longer used, you must release it.

A client created in VB6 automatically has a default Interface.

Now return to the C # code,

 _ Comc1c itfcominterface  =     Null  ;  //  Define COM interface  
Comcalcclass comobj = New Comcalcclass (); // Define com class
Itfcominterface = (_ Comc1c) comobj; // Convert to COM interface reference
Console. writeline ( " COM server says 10 + 832 is {0} " , Itfcominterface. Add ( 10 , 832 )); // Method for calling the COM interface

<2> com calls. net

Remember,The only way for the COM Client to interact with the com class is to use interface reference instead of object reference. . Net does not require any interfaces.In this case, make sure that each public member is exposed as the default Interface.

We need to set the [classinterface] feature. Its attribute value can be one of the following three enumerated values:

Autodual:Indicates that a double class interface is automatically generated for the class and made public to com. Generate type information for this type of interface and publish it in the Type Library. Because of the version control restrictions described in classinterfaceattribute, autodual is strongly recommended.

Autodispatch: Indicates that this class only supports post-binding of COM clients. When a request is sent, the dispinterface of this class is automatically made public to the COM Client. The Type Library generated by tlbexp.exe (Type Library Export Program) does not contain the dispinterface type information to prevent the client from caching the interface's dispid. Because the client can only be bound to an interface later, dispinterface does not have version control problems described in classinterfaceattribute.
This is the default setting of classinterfaceattribute.


None: Indicates that a class interface is not generated for a class. If no interface is explicitly implemented, the class can onlyIdispatchInterface provides later bound access. This isClassinterfaceattribute. The only way to publish a function through an interface explicitly implemented by the class is to useClassinterfacetype. None.

For details, see msdn:Http://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.classinterfacetype.aspx)

Create a vs2010 project.

Note: as a best practice, you should deploy the. NET program to GAC. In this case, an SNK file is required for signature. If it is not deployed to GAC, copy it to the same path as the com application.

Choose GAC deployment

Deployed successfully!

We need to generate the necessary com class library. You can use the tlbexp.exe command. Of course, you can also use the UI.

Now we can use oleview.exe to view

Create a VB standard EXE project and add references


Source code download


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.