COM为中间件调用DLL的说明

在《PHP调用COM组件的说明》里,已介绍了PHP调用COM组件的方法。如果能用COM调用DLL,再用PHP调用COM,就可以实现PHP对DLL的调用。即以COM作为中间件,让PHP可以调用现成的DLL,可以很大程度上地减少PHP开发的工作量。

下面将对COM调用DLL的方法进行说明。采用的是msvs.net2005环境下的C#语言。

1.建立DLL

建立类库生成DLL的方法不再说明。需要注意的是如果COM组件具有强命名,一定要也赋予类库强命名,才可以被COM调用;如果COM不具强命名,类库也无需强命名。强命名方法如下:

在“开始—VS2005 —VS Tools—VS命令提示符”,输入:

sn -k ****_Key.snk

将****_Key.snk 添加入项目中(在Program Files\MS Visual Studio 2005\VC下),然后打开AssemblyInfo.cs,并加入下面一行的内容:

[assembly: AssemblyKeyFile("****_Key.snk")]

下面为代码示例:

using System;

using System.Collections.Generic;

using System.Text;

 

namespace PHPInvokeClassLibrary

{

    public class PhpClass

    {

       public string Php_func(string a,string b)

        {

            string c = a + b;

            return c;

        }

    }

}

2.在COM中调用DLL

在COM中类的所有方法都要在类接口中先定义。所以我们要先在类接口中定义一个方法,并赋予其Dispid值,然后在继承的类中实现这个方法,在方法中我们就可以调用我们想要的DLL。COM代码示例如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using System.Reflection;

using System.Data.OleDb;

using System.Data;

using System.Collections;

using System.Collections.Specialized;

using PHPInvokeClassLibrary;

namespace Comtest

{

    //类接口

    [Guid("394BE3FE-18B8-4c5e-B611-75B5C5493A4E")]

    public interface ITest

    {

        [DispId(1)]

        string Test(string test);

        [DispId(2)]       //固定写法,索引号从1开始

        string About();

        [DispId(3)]

        int Add(int a, int b);

 

        //该方法用于调用DLL

        [DispId(4)]

        string PhpFunc();

    }

 

    //事件接口

    [Guid("45875EE5-5C8D-4016-897A-FCC7DD5A6834"),             //固定写法

    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]

    public interface ITest_Events

    {

    }

 

    //类

    [Guid("854C2016-E7BF-41ea-8F09-B873698E9F8E"),           //固定写法

     ClassInterface(ClassInterfaceType.None),

    ComSourceInterfaces(typeof(ITest_Events))]

    public class Class2 : ITest          //该类继承上面的接口,并实现抽象方法

    {

        public string Test(string test)

        {

            return test;

        }

 

        public string About() { return "欢迎访问 http://www.35.com"; }

        public int Add(int a, int b) { return a + b; }

 

        //该方法实现了类接口Dispid(4)的方法,调用了DLL

        public string PhpFunc()

        {

            PhpClass a = new PhpClass();

            string b = a.Php_func("Ha","what?");

            return b;

        }

    }

}

3.PHP调用COM

那么只要在PHP中使用COM类就可以成功调用DLL中我们想要的方法了!代码如下:

<?php

//dl("comtest.dll");

//w32api_register_function("", "FuncDll", "int");

$b=new COM("Comtest.Class2");

$t1=126;

$t2=456; 

$r=$b->add($t1,$t2);

$f=$b->about();

 

//这个动作就在调用DLL了

$z=$b->PhpFunc();

 

echo  $r;  

echo $f;

echo $z;

?>  

 

转载于:https://www.cnblogs.com/YvNin/archive/2010/04/09/1708099.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值