Part of this section requires the reader to be familiar with the message invocation principle of COM, and the principle of this is as shown in Mr. Yang's column.
COM Server--> COM client
COM server-->. NET Client
2, COM Interop (content of this section)
The last time we introduced the COM server-side unknown or no interface of the call method P/invoke, this time we discuss the situation of known interfaces, Com Interop method.
An ordinary interface function call
This part of the sample code called Comp6srcdnet in the ComP5 project, oh, a bit familiar with it, in fact, my purpose is just to introduce the. NET section, so the COM and MFC parts from Yang Teacher's "COM Component design and application (vii)-compile, register, call", only Use_ NET code is I wrote, I am here to thank teacher Yang, without you planted in front of the tree, we can not enjoy the cool ha ^_^
Although. NET itself is the product of the COM Phoenix Nirvana, COM's shadow can be seen in the workings of many. NET structures, but it is clear from. NET's COM calls that. NET plays a role in diluting (hiding) interfaces, as you can see from the following code.
For a COM server call, you need to do the following three actions:
1> registers com with regsvr32.exe.
2> export type libraries with Tlbimp.exe. The exported file can be either a DLL file or a TLB file, and the file generated after the export is named after the original COM name plus lib.
3> "Add Reference", as detailed below
Then you can write the calling code.
using namespace Simple2Lib;
FunClass *m_pCom; //FunClass有点像MFC中包装过的智能指针xxxPtr
m_pCom=new FunClass; //.net框架自动连接到COM服务器，不用我们动手^_^
//计算 or 连接。呵呵，这么写还真够简单的，两句话搞定，.net的异常机制会帮我们判断用户是想做加法，还是想做字符串连接
This is the end, isn't it easy? Readers familiar with COM calls can find that many of these COM initialization and lookup interfaces are hidden by. NET, and some commonly used parameter marshal conversion marshaling is also handled automatically, which makes it easier for us to invoke a COM server with a. NET client than to invoke a COM server from a COM client.