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