把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>")
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 " & strfile, 0, false
exitcode = oshell.run("c:\winnt\system32\regsvr32.exe /s " & strfile, 0, false)
echob("regsvr32.exe exitcode = " & exitcode)
else unregister
oshell.run "c:\winnt\system32\regsvr32.exe /u/s " & strfile, 0, false
exitcode = oshell.run("c:\winnt\system32\regsvr32.exe /u/s " & strfile, 0, false)
echob("regsvr32.exe exitcode = " & exitcode)
end if
cleanup oshell
end sub
sub buildoptions
echob("register: <input type=radio name=frmmethod value=reg checked>")
echob("unregister: <input type=radio name=frmmethod value=unreg>")
end sub
function echo(str)
echo = response.write(str & vbcrlf)
end function
function echob(str)
echob = response.write(str & "<br>" & 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
%>
