VB6 編寫 ActiveX DLL (項目 P_test, 類 c_test)
' c_test.cls
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 Test_Number = -1
If num > 0 Then Test_Number = 1
If num = 0 Then Test_Number = 0
End Function
編譯與註冊
- 編譯生成
p_test.dll。
- 命令行:
regsvr32 p_test.dll (需引用 Microsoft Active Server Pages Object Library)。
PHP 調用
<?php
$com = new COM("p_test.c_test");
$result = $com->Test_Number(-454);
echo $result; // 輸出 -1
?>
優勢與場景
- 比調用 JavaBean 簡單 (原生 Windows COM 支持)。
- 可封裝核心業務邏輯 (加密、算法、數據庫連接池) 實現「源碼隱藏」。
- 性能優於跨進程調用,適合高併發 Windows 宿主環境。