搞完了PHP4调用JavaBean,又想去试试调用COM,开始以为很难,自己用VB6写了一个Active Dll在PHP4中调用,马上成功,比调用javabean方便多了,下面讲一下我的步骤。 php程序员站
一:用VB6写Activex Dll php程序员站
代码如下: php程序员之家
Option Explicit php程序员之家
Private MyScriptingContext As ScriptingContext www.phperz.com
Private MyApplication As Application php程序员站
Private MyRequest As Request Private MyResponse As Response www.phperz.com
Private MyServer As Server phperz.com
Private MySession As Session Public phperz.com
Sub OnStartPage(PassedScriptingContext As ScriptingContext) php程序员站
Set MyScriptingContext = PassedScriptingContext www.phperz.com
Set MyApplication = MyScriptingContext.Application www.phperz.com
Set MyRequest = MyScriptingContext.Request www.phperz.com
Set MyResponse = MyScriptingContext.Response php程序员站
Set MyServer = MyScriptingContext.Server phperz.com
Set MySession = MyScriptingContext.Session phperz.com
End Sub php程序员站
Public Sub OnEndPage() phperz.com
Set MyScriptingContext = Nothing www.phperz.com
Set MyApplication = Nothing www.phperz.com
Set MyRequest = Nothing www.phperz.com
Set MyResponse = Nothing www.phperz.com
Set MyServer = Nothing phperz.com
Set MySession = Nothing php程序员站
End Sub www.phperz.com
Public Function Test_Number(num) As Variant www.phperz.com
If num < 0 Then Get_Number_Attrib = -1 phperz~com
If num > 0 Then Get_Number_Attrib = 1 www.phperz.com
If num = 0 Then Get_Number_Attrib = 0 www.phperz.com
End Function www.phperz.com
具体方法如下:新建一个VB6工程,ActiveX Dll将工程命名为P_test,类名为c_test www.phperz.com
类的文件内容如上。 phperz~com
编译生成p_test.dll文件 php程序员站
二:注册 phperz.com
提示符下运行:regsvr32 p_test.dll www.phperz.com
三:编写php文件,test.php4代码如下: www.phperz.com
<? phperz.com
$b=new COM("p_test.c_test"); phperz.com
$a=$b->Test_Number(-454); phperz.com
echo $a; phperz.com
?> phperz.com
运行php4文件将显示-1 www.phperz.com
可能遇到的问题是,编译工程时通不过,要将 phperz~com
Microsoft Active Server Pages Object Library phperz.com
引用进来,具体实现“Project->References”找到改库,并勾上 www.phperz.com
相比之下,PHP4调用com应该比PHP4调用javabean好哦,因为毕竟是Ms系统嘛。大家也可以去自己编写调用数据库的控件,用PHP4调用,从某种程度上,和PHP调用javabean一样,可以说,实现了“隐藏源代码”。 www~phperz~com
|