熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP學習:遠程注冊自己的組件

2022-06-13   來源: .NET編程 

  把asp程序作成dll很多好處但是有一點該dll必須注冊才能在asp中調用如果是自己的服務器那還好但如果是租用的虛擬服務器就沒辦法使用了
  
  怎樣在遠程主機上注冊我們的dll呢?在服務器端使用Shell!!!
  
  讓我們先將自己的dll文件通過ftp或http上傳到服務器上然後作一個asp程序調用WScriptShell來 執行regsvr命令
  
  Set oShell = CreateObject (WScriptShell)
  
  oShellRun c:\WINNT\system\regsvrexe /s d:\xxxdll False
  
  當然如果對方的服務器安全搞的很好的話這個代碼也許就不能用了但不管怎麼樣學習一下 也是好的
  
  在這裡也要提醒那些出租空間的朋友你的服務器是否限制了使用WScriptShell的權限?還是小心為妙
  
  完整代碼如下保存為asp即可使用
  <% ResponseBuffer = True %>
  <% ServerScriptTimeout =
  Dim frmFolderPath frmFilePath
  
  frmFolderPath = RequestForm(frmFolderPath)
  frmFilePath = RequestForm(frmDllPath)
  frmMethod = RequestForm(frmMethod)
  btnREG = RequestForm(btnREG)
  %>
  
  <HTML>
  <HEAD>
  <TITLE>Regsvrasp</TITLE>
  <STYLE TYPE=TEXT/CSS>
  Legend {FONTFAMILY: veranda; FONTSIZE: px; FONTWEIGHT: bold; COLOR: blue}
  FS {FONTFAMILY: veranda; FONTSIZE: px; BORDERWIDTH: px; BORDERCOLOR: green;
  MARGINLEFT:px; MARGINRIGHT:px}
  TD {MARGINLEFT:px; MARGINRIGHT:px; PADDINGLEFT:px; PADDINGRIGHT:px}
  </STYLE>
  </HEAD>
  
  <BODY>
  <FORM NAME=regForm METHOD=POST>
  <TABLE BORDER= CELLSPACING= CELLPADDING= MARGINWIDTH=>
  <TR>
  <TD VALIGN=TOP>
  <FIELDSET ID=FS NAME=FS 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 RequestForm(btnFileList) <> OR btnREG <> Then
  Set RegisterFiles = New clsRegister
  RegisterFilesEchoB(<B>Select File</B>)
  Call RegisterFilesinit(frmFolderPath)
  RegisterFilesEchoB(<BR><INPUT TYPE=SUBMIT NAME=btnREG VALUE= & Chr() _
  & REG/UNREG & Chr() & >)
  IF RequestForm(btnREG) <> Then
  Call RegisterFilesRegister(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 = ServerCreateObject(ScriptingFileSystemObject)
  End Property
  
  Sub init(strRoot) Root to Search (c: d: e:)
  Dim oDrive oRootDir
  IF oFSFolderExists(strRoot) Then
  IF Len(strRoot) < Then Must Be a Drive
  Set oDrive = oFSGetDrive(strRoot)
  Set oRootDir = oDriveRootFolder
  Else
  Set oRootDir = oFSGetFolder(strRoot)
  End IF
  Else
  EchoB(<B>Folder ( & strRoot & ) Not Found)
  Exit Sub
  End IF
  setRoot = oRootDir
  
  Echo(<SELECT NAME= & Chr() & frmDllPath & Chr() & >)
  Call getAllDlls(oRootDir)
  EchoB(</SELECT>)
  BuildOptions
  End Sub
  
  Sub getAllDlls(oParentFolder)  通過fso列舉所有的dll和ocx文件
  Dim oSubFolders oFile oFiles
  Set oSubFolders = oParentFolderSubFolders
  Set opFiles = oParentFolderFiles
  
  For Each oFile in opFiles
  IF Right(lCase(oFileName) ) = dll OR Right(lCase(oFileName) ) = ocx Then
  Echo(<OPTION VALUE= & Chr() & oFilePath & Chr() & > _
  & oFileName & </Option>)
  End IF
  Next
  
  On Error Resume Next
  For Each oFolder In oSubFolders Iterate All Folders in Drive
  Set oFiles = oFolderFiles
  For Each oFile in oFiles
  IF Right(lCase(oFileName) ) = dll OR Right(lCase(oFileName) ) = ocx Then
  Echo(<OPTION VALUE= & Chr() & oFilePath & Chr() & > _
  & oFileName & </Option>)
  End IF
  Next
  Call getAllDlls(oFolder)
  Next
  On Error GoTo
  End Sub
  
  Sub Register(strFilePath regMethod)
  Dim theFile strFile oShell exitcode
  Set theFile = oFSGetFile(strFilePath)
  strFile = theFilePath
  
  Set oShell = CreateObject (WScriptShell)
  
  IF regMethod = REG Then Register
  oShellRun c:\WINNT\system\regsvrexe /s & strFile False
  exitcode = oShellRun(c:\WINNT\system\regsvrexe /s & strFile False)
  EchoB(regsvrexe exitcode = & exitcode)
  Else unRegister
  oShellRun c:\WINNT\system\regsvrexe /u/s & strFile False
  exitcode = oShellRun(c:\WINNT\system\regsvrexe /u/s & strFile False)
  EchoB(regsvrexe 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 = ResponseWrite(str & vbCrLf)
  End Function
  
  Function EchoB(str)
  EchoB = ResponseWrite(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
  %>
From:http://tw.wingwit.com/Article/program/net/201311/13897.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.