Bridge between COM and. Net (3) com InterOP mode of the COM Server

Source: Internet
Author: User
Reproduced: VC Knowledge Base http://www.vckbase.com/document/viewdoc? Id = 1630

Bridge between COM and. Net (III)
Com InterOP mode of the COM Server

Author: caeser2

Source code download

  • Connecting COM and. Net (1) -- prerequisites
  • Bridge between COM and. Net (2) -- p-invoke method of COM Server

This section requires readers to be familiar with the message calling principle of COM. For the principle, see Yang's column.

Previous Article:

COM server --> COM Client

...
COM server -->. Net Client
 1. P/invoke
 2. com InterOP(In this section)

The previous section introduced the call Method P/invoke when the COM server is unknown or there is no interface. This time we will discuss the situation of known interfaces, the com InterOP method.

1. common interface function calls

This part of the sample code is called the comp5 project in comp6srcdnet. I am a little familiar with it. In fact, I just want to introduce it. net, so the COM and MFC sections are derived from instructor Yang's "COM Component Design and Application (7) -- Compilation, registration, and call". Only use_net code is written by me, I would like to thank Teacher Yang. Without the tree you planted above, we cannot catch the cold.
Although. net itself is the product of COM Phoenix Nirvana, from many. the net structure can be seen in the working principle of COM, but from. net com calls, we can see that ,. net is used to play down (hide) the interface, which can be seen in the following code.

To call a COM server, you must perform the following three operations:
  1>Use regsvr32.exe to register COM.
  2>Use tlbimp.exe to export the Type Library. The exported file can be a DLL file or a TLB file. The exported file is named as the original com name and followed by LIB by default.
  3>"Add reference". For details, see
Then you can write the call code.

/* Simple2 is a COM server written by instructor Yang. It has two functions: add (INT, INT) and CAT (BSTR, BSTR ).
Use1, 2, 3, 4, and 5 are eight Com calling methods written by instructor Yang... no, I won't count them. Don't believe you can read the code yourself
Use_net is A. Net call I wrote.

On the "solution" Panel, select "Reference", right-click "add reference", and add the type library file generated by tlbimp.exe to the Assembly. Result



You can also use the [dllimport (...)] method to reference COM.
*/
// Write a reference for ease of use
Using namespace simple2lib;

Funclass * m_pcom; // funclass is a bit like the smart pointer xxxptr encapsulated in MFC

// Initialization
Try
{
M_pcom = new funclass; // the. NET Framework automatically connects to the COM server. You do not need to start using it.
}
Catch (...)
{
MessageBox: Show ("Com is not registered, right?");
Close ();
}

// Compute or connection. Well, it's really simple to write this. It can be done in two sentences. the. NET exception mechanism will help us determine whether the user wants to perform addition or string connection.
Try
{
Textbox3-> text = m_pcom-> Add (
Convert: toint32 (textbox1-> text), convert: toint32 (textbox2-> text). tostring ();
}
Catch (...)
{
Textbox3-> text = m_pcom-> CAT (textbox1-> text, textbox2-> text );
}

This is the end, isn't it easy? Readers who are familiar with COM calls can find that many of the COM Initialization and search interfaces are used. net is hidden, and some common parameter Marshal conversion and sending operations are also automatically processed, which enables us to use.. Net client calls the COM server more easily than the COM Client calls the COM server.

Ii. Call the callback Interface

If the Com Message mechanism is not supported, the COM Service is not supported. Let's first look at the call method of the callback interface.
The sample code is called the callbackinterface project in callbackinterface. It is also introduced from instructor Yang's article: "COM Component Design and Application (14th)-events and notifications ". Where:
The simple12 project is renamed callback to implement callback. dll (simple12.dll) with One callback interface );
The Use1 project is renamed use_mfc to call callback. dll with MFC;
Use_net is written in the. NET Framework.

Do not forget the three operations required to call the COM server!

// First, implement the interface, which completely imitates use_mfc.
# Pragma once
Using namespace callbacklib;
Namespace use_net
{
Public _ GC class csink: Public icallback // inherits the self-callback Interface
{
Public:
Csink (textbox * P, label * q): textbox3 (p ){}
PRIVATE:
Textbox * textbox3; // used to display the result
Void callbacklib: icallback: fire_result (INT nresult) // virtual function that implements the callback Interface
{
Textbox3-> text = nresult. tostring ();
}
};
}
// How can I implement only one virtual function? What about others? It's also. net, and it's already done for us.

Csink * sink; // instantiated callback Interface
Event1class * m_spcom; // event1class is a bit like the smart pointer xxxptr of the interface in MFC
Int m_dwcookie; // Save the flag. What, you don't know what this is about? Ask instructor Yang! Pai_^

// Initialization
M_dwcookie = 0;
Sink = new csink (textbox3, label1 );
Try {
M_spcom = new event1class; //. Net automatically connects to the COM Server
}
Catch (...)
{
MessageBox: Show ("said Yang: Have you registered? Is com initialized? ");
Close ();
}

// Connection
M_spcom-> advise (sink, & m_dwcookie );

// Use
Try
{
M_spcom-> Add (convert: toint32 (textbox1-> text), convert: toint32 (textbox2-> text ));
}
Catch (...)
{
MessageBox: Show ("incorrect input format ");
}

// Disconnect
M_spcom-> unadvise (m_dwcookie );
M_dwcookie = 0;


Iii. Call of connection points

The sample code for this part is the dispconnect project in dispconnect ~~~ This is an article from instructor Yang: COM Component Design and Application (16) -- connection point. Where:
Simple16 project name is changed to singleconnect to implement singleconnect. dll with one connection point;
The multconnect project name remains unchanged to implement multconnect. dll with two connection points;
The Use Project is changed to use_mfc, and singleconnect. dll is called using MFC;
The usemult project is changed to usemult_mfc, which is used to call multconnect. dll and fix the following error in the header file :)
Use_net is written in the. NET Framework and is used to call singleconnect. dll with. net;
Usemult_net is written in the. NET Framework and is called by. net. dll.

Do not forget the three operations required to call one COM server !, This time we will call two com!

// Use_net
Dispconnectclass * sink; // declare the packaging class object, a bit like the smart pointer xxxptr encapsulated by the COM Client

// Initialization
Try
{
Sink = new dispconnectclass; //. Net helps us automatically connect to the interface
}
Catch (...)
{
MessageBox: Show ("instructor Yang said: no registration or initialization? ");
Close ();
}

// Connection
// Replaces the connection point model with the event/delegate model .. The. NET Framework has automatically wrapped up the delegate.
Sink-> result + = new _ idispconnectevents_resulteventhandler (this, my_resultevent );

// Callback function
PRIVATE: System: void my_resultevent (int e) // The long type of ATL is converted to int by Marshal.
{
Textbox3-> text = E. tostring (); // textbox3 is used to display the result.
}

// Use
Try
{
Sink-> Add (convert: toint32 (textbox1-> text), convert: toint32 (textbox2-> text ));
}
Catch (...)
{
MessageBox: Show ("the input number format is incorrect! ");
}

// Disconnect
// This step is not required, and. NET will automatically process it when the program closes ().

Similarly, usemult_net of multiple connection points can be well understood.

Dispconnectclass * sink; // declare the packaging class object, a bit like the smart pointer xxxptr encapsulated by the COM Client
// Initialization
Try
{
Sink = new dispconnectclass; //. Net automatically connects to the interface and is automatically disconnected when you close ().
}
Catch (...)
{
MessageBox: Show ("instructor Yang said: no registration or initialization? ");
Close ();
}
// Connection
// The connection point model is replaced with the event and delegate model .. The. NET Framework has automatically wrapped up the delegate.
// Strange. You cannot see the timer event in the "Object timer"... bug. Why are you so stressed? I wrote a few. Net articles and found that there are so many. Net articles.
Sink-> timer + = new _ idispconnectevents2_timereventhandler (this, my_timeevent );
Sink-> result + = new _ idispconnectevents_resulteventhandler (this, my_resultevent );
// Callback function
PRIVATE: System: void my_timeevent (Object * E) // variant is Marshal as object *
{
Label1-> text = e-> tostring (); // label1 is used to display the time
}
PRIVATE: System: void my_resultevent (int e) // The long type of ATL is converted to int by Marshal.
{
Textbox3-> text = E. tostring (); // textbox3 is used to display the result.
}
// Use
// Start timing
Sinks-> settimer (1000 );
....
// Stop timing
Sink-> killtimer ();
....
// Add (INT, INT)
Try
{
Sink-> Add (convert: toint32 (textbox1-> text), convert: toint32 (textbox2-> text ));
}
Catch (...)
{
MessageBox: Show ("the input number format is incorrect! ");
}
// Disconnect
// This step is not required, and. NET will automatically process it when the program closes ().

Iv. ActiveX call

This principle is troublesome, but it is very easy to use. msdn uses the mediaplayer control (msdxm. ocx) as an example. If you are interested, you can try it. I have packed it into tools. Here I will change to an activex file and download axdemo. dll from the file tools. It is an article by instructor Yang ~~ I have not published the Code yet, so I will first post the code on behalf of the teacher ^ _ ^, axdemo. for DLL code, you can view the activexdemo project in the example file activexdemo. This is my exclusive sensitive information, handwritten by instructor Yang, which has not yet been disclosed ~~
Axdemo is an ActiveX written by instructor Yang using vc6. Note that it cannot be re-compiled with another version of VC. The control is called the UI class;
Use_mfc and use_net are called by me.
I will not talk much about the call steps of MFC. I will tell you later about the call steps of. Net:
1.Regsvr32.exe registration:

    

2.Add the control to the Toolbox (the "insert ActiveX Control" item does not exist in the context menu of the. NET form Editor ):

    ->

If not, it cannot be found in the list. In this case, you can (the compiler will automatically register ):

    

3.Draw the control in the form. The compiler automatically calls the aximp.exe export Type Library:

    

4.Compilation and debugging:

    

What makes ActiveX more comfortable than other COM components is that attributes and events can be directly adjusted in the "attribute" Panel, truly visual programming.

V. Others

Unfortunately, I am using vs 2003 (. net V1.1) does not support code-Level C ++ InterOP methods, so I can only write it here on the COM server. We will discuss it in the next section.. net.
The tools folder in the package for download provides several tools

Name Regsvr32.exe Tlbimp.exe Aximp.exe Ildasm.exe
Function Register COM controls Export the COM control Type Library The type definition in the com Type Library of ActiveX control is converted to a Windows form control. Decompile the program. net. Both. EXE and. DLL files can be used.
Basic operations Registration: regsvr32 DLL file name Export: tlbimp DLL file name

Export: aximp DLL or ocx file name

The program is customized.
Reverse registration: regsvr32/u DLL file name

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.