把asp程式作成dll很多好處,但是有一點,該dll必須註冊才能在asp中調用。如果是自己的伺服器那還好,但如果是租用的虛擬伺服器,就沒辦法使用了。
怎樣在遠程主機上註冊我們的dll呢?在伺服器端使用Shell!!!
讓我們先將自己的dll檔案通過ftp或http上傳到伺服器上,然後作一個asp程式,調用WScript.Shell來 執行regsvr32命令:
Set oShell = CreateObject ("WScript.Shell")
oShell.Run "c:\WINNT\system32\regsvr32.exe /s d:\xxx.dll", 0, False
當然如果對方的伺服器安全搞的很好的話,這個代碼也許就不能用了,但不管怎麼樣,學習一下 也是好的,:)
在這裡也要提醒那些出租空間的朋友,你的伺服器是否限制了使用WScript.Shell的許可權?還是小心為妙
完整代碼如下,儲存為.asp即可使用:
<% Response.Buffer = True %>
<% Server.ScriptTimeout = 500
Dim frmFolderPath, frmFilePath
frmFolderPath = Request.Form("frmFolderPath")
frmFilePath = Request.Form("frmDllPath")
frmMethod = Request.Form("frmMethod")
btnREG = Request.Form("btnREG")
%>
<HTML>
<HEAD>
<TITLE>Regsvr32.asp</TITLE>
<STYLE TYPE="TEXT/CSS">
.Legend {FONT-FAMILY: veranda; FONT-SIZE: 14px; FONT-WEIGHT: bold; COLOR: blue}
.FS {FONT-FAMILY: veranda; FONT-SIZE: 12px; BORDER-WIDTH: 4px; BORDER-COLOR: green;
MARGIN-LEFT:2px; MARGIN-RIGHT:2px}
TD {MARGIN-LEFT:6px; MARGIN-RIGHT:6px; PADDING-LEFT:12px; PADDING-RIGHT:12px}
</STYLE>
</HEAD>
<BODY>
<FORM NAME="regForm" METHOD="POST">
<TABLE BORDER=0 CELLSPACING=6 CELLPADDING=6 MARGINWIDTH=6>
<TR>
<TD VALIGN=TOP>
<FIELDSET ID=FS1 NAME=FS1 CLASS=FS>
<LEGEND CLASS=Legend>Regsvr Functions</LEGEND>
Insert Path to DLL Directory<BR>
<INPUT TYPE=TEXT NAME="frmFolderPath" VALUE="<%=frmFolderPath%>"><BR>
<INPUT TYPE=SUBMIT NAME=btnFileList VALUE="Build File List"><BR>
<%
IF Request.Form("btnFileList") <> "" OR btnREG <> "" Then
Set RegisterFiles = New clsRegister
RegisterFiles.EchoB("<B>Select File</B>")
Call RegisterFiles.init(frmFolderPath)
RegisterFiles.EchoB("<BR><INPUT TYPE=SUBMIT NAME=btnREG VALUE=" & Chr(34) _
& "REG/UNREG" & Chr(34) & ">")
IF Request.Form("btnREG") <> "" Then
Call RegisterFiles.Register(frmFilePath, frmMethod)
End IF
Set RegisterFiles = Nothing
End IF
%>
</FIELDSET>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
<%
Class clsRegister
Private m_oFS
Public Property Let oFS(objOFS)
m_oFS = objOFS
End Property
Public Property Get oFS()
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
End Property
Sub init(strRoot) 'Root to Search (c:, d:, e:)
Dim oDrive, oRootDir
IF oFS.FolderExists(strRoot) Then
IF Len(strRoot) < 3 Then 'Must Be a Drive
Set oDrive = oFS.GetDrive(strRoot)
Set oRootDir = oDrive.RootFolder
Else
Set oRootDir = oFS.GetFolder(strRoot)
End IF
Else
EchoB("<B>Folder ( " & strRoot & " ) Not Found.")
Exit Sub
End IF
setRoot = oRootDir
Echo("<SELECT NAME=" & Chr(34) & "frmDllPath" & Chr(34) & ">")
Call getAllDlls(oRootDir)
EchoB("</SELECT>")
BuildOptions
End Sub
Sub getAllDlls(oParentFolder) '通過fso列舉所有的dll和ocx檔案
Dim oSubFolders, oFile, oFiles
Set oSubFolders = oParentFolder.SubFolders
Set opFiles = oParentFolder.Files
For Each oFile in opFiles
IF Right(lCase(oFile.Name), 4) = ".dll" OR Right(lCase(oFile.Name), 4) = ".ocx" Then
Echo("<OPTION VALUE=" & Chr(34) & oFile.Path & Chr(34) & ">" _
& oFile.Name & "</Option>")
End IF
Next
On Error Resume Next
For Each oFolder In oSubFolders 'Iterate All Folders in Drive
Set oFiles = oFolder.Files
For Each oFile in oFiles
IF Right(lCase(oFile.Name), 4) = ".dll" OR Right(lCase(oFile.Name), 4) = ".ocx" Then
Echo("<OPTION VALUE=" & Chr(34) & oFile.Path & Chr(34) & ">" _
& oFile.Name & "</Option>&q