明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

PHP4調(diào)用自己編寫的COM組件

[摘要]搞完了PHP4調(diào)用JavaBean,又想去試試調(diào)用COM,開始以為很難,自己用VB6寫了一個(gè)Active Dll在PHP4中調(diào)用,馬上成功,比調(diào)用javabean方便多了,下面講一下我的步驟。一:用VB6寫Activex Dll 代碼如下: Option Explicit Private MySc...
搞完了PHP4調(diào)用JavaBean,又想去試試調(diào)用COM,開始以為很難,自己用VB6寫了一個(gè)Active Dll在PHP4中調(diào)用,馬上成功,比調(diào)用javabean方便多了,下面講一下我的步驟。

一:用VB6寫Activex Dll

代碼如下:

Option Explicit

Private MyScriptingContext As ScriptingContext

Private MyApplication As Application

Private MyRequest As Request Private MyResponse As Response

Private MyServer As Server

Private MySession As Session Public

Sub OnStartPage(PassedScriptingContext As ScriptingContext)

Set MyScriptingContext = PassedScriptingContext

Set MyApplication = MyScriptingContext.Application

Set MyRequest = MyScriptingContext.Request

Set MyResponse = MyScriptingContext.Response

Set MyServer = MyScriptingContext.Server

Set MySession = MyScriptingContext.Session

End Sub

Public Sub OnEndPage()

Set MyScriptingContext = Nothing

Set MyApplication = Nothing

Set MyRequest = Nothing

Set MyResponse = Nothing

Set MyServer = Nothing

Set MySession = Nothing

End Sub

Public Function Test_Number(num) As Variant

If num < 0 Then Get_Number_Attrib = -1

If num > 0 Then Get_Number_Attrib = 1

If num = 0 Then Get_Number_Attrib = 0

End Function

具體方法如下:新建一個(gè)VB6工程,ActiveX Dll將工程命名為P_test,類名為c_test

類的文件內(nèi)容如上。

編譯生成p_test.dll文件

二:注冊

提示符下運(yùn)行:regsvr32 p_test.dll

三:編寫php文件,test.php4代碼如下:

<?

$b=new COM("p_test.c_test");

$a=$b->Test_Number(-454);

echo $a;

?>

運(yùn)行php4文件將顯示-1

可能遇到的問題是,編譯工程時(shí)通不過,要將

Microsoft Active Server Pages Object Library

引用進(jìn)來,具體實(shí)現(xiàn)“Project->References”找到改庫,并勾上

相比之下,PHP4調(diào)用com應(yīng)該比PHP4調(diào)用javabean好哦,因?yàn)楫吘故荕s系統(tǒng)嘛。大家也可以去自己編寫調(diào)用數(shù)據(jù)庫的控件,用PHP4調(diào)用,從某種程度上,和PHP調(diào)用javabean一樣,可以說,實(shí)現(xiàn)了“隱藏源代碼”。