.Net 与Com组件的互操作

 

1. 平台调用服务[windows32]

*       Also Known as PInvoke

*       Use DllImport Attribute to Import an API Function from an External DLL

声明:

[DllImport("user32.dll", CharSet=CharSet.Ansi)]

  public static extern int MessageBox(int h, string m,

    string c, int type); //必须是静态和外部的

对于Dll,必须声明,然后才能够使用。而对COM则可以自动转换!

调用:

public static void Main ()

   {  string pText = "Hello World !";

      string pCaption = "PInvoke Test";

      MessageBox(0, pText, pCaption, 0);

}

2. COM互操作[.NETCOM的互操作]

*       Exposing .NET Framework Classes to COM

*       Must create COM Callable Wrapper

*       Create CCWs by using Tlbexp.exe

*       Register by using Regasm.exe

 

*       Exposing COM Classes to the .NET Framework

*       较为常用

*       Must create Runtime Callable Wrapper

*       Create RCWs by using Tlbimp.exe

3.举例:[在一个form中调用windows media player控件]

基本步骤:

1.       新建一个windows application的项目

2.       toolbox中选择“choose items…”,vs 2005将扫描系统中所有可用的控件,从中选windows media player控件,这样在tool box中就可以看到该控件

3.       windows media player控件添加到form中,并添加一个buttton,命名为openFile,在加入一个openFileDilalog控件。

4.       openFile控件加入一个单击事件

a)         this.openFileDialog1.ShowDialog();

5.       ” openFileDilalog”控件添加一个事件

a)         this.axWindowsMediaPlayer1.URL = this.openFileDialog1.FileName;

由此,一个简单的调用com的程序已经完成。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值