通过ASP远程注册自己的组件

把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即可使用:

1 Response.Buffer = True 
1 Server.ScriptTimeout = 500   
2Dim frmFolderPath, frmFilePath 
3
4frmFolderPath = Request.Form("frmFolderPath")   
5frmFilePath = Request.Form("frmDllPath")   
6frmMethod = Request.Form("frmMethod")   
7btnREG = Request.Form("btnREG")   
 1<html>
 2<head>
 3<title>Regsvr32.asp</title>
 4<style type="TEXT/CSS">   
 5.Legend {FONT-FAMILY: veranda; FONT-SIZE: 14px; FONT-WEIGHT: bold; COLOR: blue}   
 6.FS {FONT-FAMILY: veranda; FONT-SIZE: 12px; BORDER-WIDTH: 4px; BORDER-COLOR: green;   
 7MARGIN-LEFT:2px; MARGIN-RIGHT:2px}   
 8TD {MARGIN-LEFT:6px; MARGIN-RIGHT:6px; PADDING-LEFT:12px; PADDING-RIGHT:12px}   
 9</style>
10</head>
11<body>
12<form method="POST" name="regForm">
13<table border="0" cellpadding="6" cellspacing="6" marginwidth="6">
14<tr>
15<td valign="TOP">
16<fieldset class="FS" id="FS1" name="FS1">
17<legend class="Legend">Regsvr Functions</legend>   
18Insert Path to DLL Directory<br/>
19<input name="frmFolderPath" type="TEXT" value="```
20=frmFolderPath
21```"/><br/>
22<input name="btnFileList" type="SUBMIT" value="Build File List"/><br/>   

IF Request.Form("btnFileList") &lt;&gt; "" OR btnREG &lt;&gt; "" Then
Set RegisterFiles = New clsRegister
RegisterFiles.EchoB("<b>Select File</b>")
Call RegisterFiles.init(frmFolderPath)
RegisterFiles.EchoB("<br/><input "="" &="" chr(34)="" name="btnREG" reg="" type="SUBMIT" unreg"="" value=" &amp; Chr(34) _
&amp; "/>")
IF Request.Form("btnREG") &lt;&gt; "" Then
Call RegisterFiles.Register(frmFilePath, frmMethod)
End IF
Set RegisterFiles = Nothing
End IF

1</fieldset>
2</td>
3</tr>
4</table>
5</form>
6</body>
7</html>
 1   
 2Class clsRegister 
 3
 4Private m_oFS 
 5
 6Public Property Let oFS(objOFS)   
 7m_oFS = objOFS   
 8End Property 
 9
10Public Property Get oFS()   
11Set oFS = Server.CreateObject("Scripting.FileSystemObject")   
12End Property 
13
14  
15Sub init(strRoot) 'Root to Search (c:, d:, e:)   
16Dim oDrive, oRootDir   
17IF oFS.FolderExists(strRoot) Then   
18IF Len(strRoot) < 3 Then 'Must Be a Drive   
19Set oDrive = oFS.GetDrive(strRoot)   
20Set oRootDir = oDrive.RootFolder   
21Else   
22Set oRootDir = oFS.GetFolder(strRoot)   
23End IF   
24Else   
25EchoB("

<b>Folder ( " &amp; strRoot &amp; " ) Not Found.")
Exit Sub
End IF
setRoot = oRootDir

Echo("<select "="" &="" chr(34)="" frmdllpath"="" name=" &amp; Chr(34) &amp; ">")
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=" &amp; Chr(34) &amp; oFile.Path &amp; Chr(34) &amp; ">" _
&amp; oFile.Name &amp; "</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=" &amp; Chr(34) &amp; oFile.Path &amp; Chr(34) &amp; ">" _
&amp; oFile.Name &amp; "</option>")
End IF
Next
Call getAllDlls(oFolder)
Next
On Error GoTo 0
End Sub

Sub Register(strFilePath, regMethod)
Dim theFile, strFile, oShell, exitcode
Set theFile = oFS.GetFile(strFilePath)
strFile = theFile.Path

Set oShell = CreateObject ("WScript.Shell")

IF regMethod = "REG" Then 'Register
oShell.Run "c:\WINNT\system32\regsvr32.exe /s " &amp; strFile, 0, False
exitcode = oShell.Run("c:\WINNT\system32\regsvr32.exe /s " &amp; strFile, 0, False)
EchoB("regsvr32.exe exitcode = " &amp; exitcode)
Else 'unRegister
oShell.Run "c:\WINNT\system32\regsvr32.exe /u/s " &amp; strFile, 0, False
exitcode = oShell.Run("c:\WINNT\system32\regsvr32.exe /u/s " &amp; strFile, 0, False)
EchoB("regsvr32.exe exitcode = " &amp; exitcode)
End IF

Cleanup oShell
End Sub

Sub BuildOptions
EchoB("Register: <input checked="" name="frmMethod" type="RADIO" value="REG"/>")
EchoB("unRegister: <input name="frmMethod" type="RADIO" value="UNREG"/>")
End Sub

Function Echo(str)
Echo = Response.Write(str &amp; vbCrLf)
End Function

Function EchoB(str)
EchoB = Response.Write(str &amp; "<br/>" &amp; vbCrLf)
End Function

Sub Cleanup(obj)
If isObject(obj) Then
Set obj = Nothing
End IF
End Sub

Sub Class_Terminate()
Cleanup oFS
End Sub
End Class

Published At
Categories with Web编程
Tagged with
comments powered by Disqus