connecting COM to. NET (3) COM Interop method

Source: Internet
Author: User

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.

Previous content:

COM Server--> COM client

...

COM server-->. NET Client

1, P/invoke

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.

/*Simple2是杨老师写的COM服务器,带有Add(int,int)和Cat(BSTR,BSTR)两个函数
Use1、2、3、4、5是杨老师写的8种COM调用方法....不是我不会数数哦,不信你自己去看代码
Use_Net是我写的.net调用
在"解决方案"面版中选择"引用"项,鼠标右键"添加引用",将Tlbimp.exe生成的类型库文件添加进程序集。结果如图

也可以用[DllImport(...)]方法引用COM
*/
//写个引用,方便使用
using namespace Simple2Lib;
FunClass *m_pCom;  //FunClass有点像MFC中包装过的智能指针xxxPtr
//初始化
try
{
m_pCom=new FunClass;  //.net框架自动连接到COM服务器,不用我们动手^_^
}
catch(...)
{
MessageBox::Show("COM没有注册吧?");
Close();
}
//计算 or 连接。呵呵,这么写还真够简单的,两句话搞定,.net的异常机制会帮我们判断用户是想做加法,还是想做字符串连接
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 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.

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.