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. The Code is as 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 plug-in called "Interoperability assembly" generated by IDE for us, and this plug-in is a 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: Under Program FilesMicrosoft SDKsWindowsv7.0Ain
Find SimpleComServer and double-click
According to the COM rule: 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; // defines the COM interface
ComCalcClass comObj = new ComCalcClass (); // defines the COM class
ItfComInterface = (_ comc1c) comObj; // converts it to a reference to the COM interface
Console. WriteLine ("COM server says 10 + 832 is {0}", itfComInterface. Add (10,832); // call the COM interface method
<2> COM calls. NET
Remember, the only way for COM clients to interact with COM classes is to use interface references instead of object references. . 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 published 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 the class interface is not generated for the class. If no interface is explicitly implemented, the class can only provide post-bound access through the IDispatch interface. This is the recommended setting for ClassInterfaceAttribute. The only way to publish a function through an interface explicitly implemented by the class is to use ClassInterfaceType. None.
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
Invitation month Note: The copyright of this article is jointly owned by the invitation month and CSDN. For more information, see the source.