• 沒有找到結果。

18-4 將 ASP 程式碼封裝成 DLL

在文檔中 第 19 章 ASP 程式設計 (頁 28-33)

由於ASP 之程式,不論是在伺服端或主控端執行,都是以明碼之 Script

格式呈現,惡意聯網人員可以輕易得知原始程式,因而遭受到無謂之入侵和

破壞。為提高程式之隱密性和安全性,可將ASP 程式封裝成 DLL 格式,以

下詳細介紹DLL 之製作程序與技巧。

1. 啟動 VB 應用程式。

2. 點選應用系統格式-Active DLL。

3. 開啟系統設計介面

系統自動產生一個名稱為Class1 之類別模組,專案名稱預設為

Project1,可將 Class1 和 Project1 改名為適當名稱。

4. 點取主功能表之“Project”之 “References”,開啟物件參照選單,請選 入下列物件:

Microsoft Active Server Pages Object Library

Microsoft Active Data Objects 2.8 Library

Microsoft ADO Ext. 2.8 for DLL & Security

Microsoft Active Server Pages ObjectContext Object Library

COM+ Services TypeLibrary 5. 在 Class1 開頭加入下列指令:

Public ObjectContext As ObjectContext

Public Application As ASPTypeLibrary.Application Public Server As ASPTypeLibrary.Server

Public Session As ASPTypeLibrary.Session Public Response As ASPTypeLibrary.Response Public Request As ASPTypeLibrary.Request

註:以上變數為ASP 程式與 DLL 程式溝通之介面

6. 在 Class1 加入下列兩個事件程序:

Private Sub Class_Initialize() ‘啟動 DLL 程式時,執行前置處理程序 On Error Resume Next

Set objContext = GetObjectContext

Set Application = objContext.Item("Application") Set Server = objContext.Item("Server")

Set Session = objContext.Item("Session") Set Request = objContext.Item("Request") Set Response = objContext.Item("Response")

‘ 此處可加入其它自定程式碼 End Sub

Private Sub Class_Terminate() ‘結束 DLL 程式時,清除系統變數 On Error Resume Next

‘ 此處可加入其它自定程式碼 Set Application = Nothing Set Server = Nothing Set Session = Nothing Set Request = Nothing Set Response = Nothing Set objContext = Nothing End Sub

7. 在類別內加入屬性欄位,做為 ASP 與 DLL 存取資料溝通欄位。

讀取資料欄:

Public Property Get 欄位名稱() As 資料型態 欄位名稱= 系統變數

End Property

寫入資料欄:

Public Property Let 欄位名稱(ByVal 接收變數 As 資料型態) 系統變數=接收變數

End Property

8. 若有需要,可依步驟 6 與 7 加入其它類別。

9. 存檔。

10. 點取主功能表之“File”之 “Make *.dll”功能,將專案轉成 DLL 檔。

11. 將DLL 檔註冊:點取桌面之【開始】、【執行】,在輸入方塊內輸入左列指

令:regsvr DLL 檔所在目錄及檔名,例如,

regsvr D:\Mysql\ASP\AspDoMysql.dll 12. DLL 檔之引用

在原ASP 程式,將原來以 <% ……%>之程式碼移除,改以下列指令取代:

<%

Dim AspObj

Set AspObj = Server.CreateObject(“AspDoMysql.Class1”)

‘其中 AspDoMysql 為自行開發之 DLL 檔,Class1 為類別名稱 AspObj .欄位名稱 = 傳入值

‘傳入預設參數給 DLL 檔 AspObj .程序名稱 傳入參數

‘執行 DLL 檔內之特定程序 變數 = AspObj .欄位名稱

‘取回 DLL 檔處理結果 Set AspObj = Nothing

%>

13. 應用範例:

下列指令為顯示Customer 資料表之原始 ASP 程式碼:

Dim Con Dim Rs

Set Con= Server.CreateObject("ADODB.Connection")

cn_str = "Driver={MySQL ODBC 3.51 Driver}; Server=主機位址; " & _

"User=用戶帳號; Password=連線密碼;" Database=連線資料庫;” & _

“Option= 16437"

Con.ConnectionString = cn_str Con.Open

Sql = “Select * From Customer”

Set Rs= Server.CreateObject("ADODB.Recordset")

Rs.Open Sql, Con, 3, 2

Response.Write "<CENTER><TABLE BORDER=1 cellspacing = 0 cellpadding = 0 >"

Response.Write "<TR BGCOLOR=#00FFFF>"

fld = Rs.Fields.Count-1

‘提示欄位抬頭 For i=0 to fld

Response.WRITE "<TD><FONT Size=2 > " &Rs(i).Name & "</FONT>

</TD>"

Next

Response.Write "</TR>"

Do While Not Rs.EOF Response.Write "<TR>"

For i=0 to fld

Response.WRITE "<TD><FONT Size=2 > " & Rs(i) & "</FONT></TD>"

Next

Response.Write "</TR>"

Rs.MoveNext Loop

Response.Write "</TABLE></CENTER>"

修改DLL 檔程式碼

在DLL 之類別開頭宣告五個公用變數

Public m_HOST As String Public m_WKDB As String Public m_USER As String Public m_PSWD As String Public m_SQL As String

在DLL 之類別內加入五個資料存取欄位

Public Property Let HOST(ByVal str AsString) M_HOST=str

End Property

Public Property Let WKDB(ByVal str AsString) M_WKDB=str

End Property

Public Property Let USER(ByVal str AsString) M_USER=str

End Property

Public Property Let PSWD(ByVal str AsString) M_PSWD=str

End Property

Public Property Let SQL(ByVal str AsString) M_SQL=str

End Property

在DLL 之類別內加入一個展示資料程序

Private Sub showData() On Error Resume Next

Dim Con AS New ADODB>Connection Dim Rs New ADODB.Recordset

cn_str = "Driver={MySQL ODBC 3.51 Driver}; Server=” & m_HOST & “;

" "User=” & m_USER & “; Password=” & m_PSWD & “;" Database=” &

m_WKDB & “;Option= 16437"

Con.ConnectionString = cn_str Con.Open

Rs.Open m_SQL, Con, 3, 2

Response.Write "<CENTER><TABLE BORDER=1 cellspacing = 0 cellpadding = 0 >"

Response.Write "<TR BGCOLOR=#00FFFF>"

fld = Rs.Fields.Count-1

‘提示欄位抬頭 For i=0 to fld

Response.WRITE "<TD><FONT Size=2 > " &Rs(i).Name & "</FONT>

</TD>"

Next

Response.Write "</TR>"

Do While Not Rs.EOF Response.Write "<TR>"

For i=0 to fld

Response.WRITE "<TD><FONT Size=2 > " & Rs(i) & "</FONT></TD>"

Next

Response.Write "</TR>"

Rs.MoveNext Loop

Response.Write "</TABLE></CENTER>"

Rs.Close

Set Rs = Nothing Con.Close

Set Con = Nothing End Sub

接著將編輯內容存檔,然後重製DLL 檔(但不需要新註冊)。

將原ASP 程式改為下列程式碼

<%

Set AspObj = Server.CreateObject(“AspDoMysql.Class1”) AspObj.HOST = “LocalHost”

AspObj .WKDB = 資料庫 AspObj .USER = 用戶帳號 AspObj.PSWD = 密碼

AspObj.SQL = “Select * From Customer”

AspObj.showData Set AspObj = Nothing

%>

大功告成!是不是覺得很簡單。請試試你的DLL 執行結果。DLL 之能力

不只如此而已,請好好發揮你的創意吧!

在文檔中 第 19 章 ASP 程式設計 (頁 28-33)

相關文件