How to register/unregister an ActiveX DLL remotely using plain ASP?by Ken Chia.
Overview
If you are developing an ActiveX DLL for a customer and his website is hosted on a third party server, how are you going to register and unregister the component without having to request the server administrator to do it for you. The latter may even have reservations about this for security or other business reasons.
The alternative would be to give up the idea of using an ActiveX DLL and go back to using scripted ASP pages. However, sometimes you or your customer might need to protect the program logic from prying eyes.
So what is the solution? Simply register or unregister a component yourself using the following ASP scripts:
Use this register.asp to register :
<%
' register.asp
Response.Write ("Current Path: " & _
Server.MapPath(Request.ServerVariables("SCRIPT_NAME")))
''Work only if Path string includes the regsvr32 /s
Path = Request.Form("Path")
If Path <> "" Then
filepath = Replace(Path, "regsvr32 /s", "")
filepath = Trim(filepath)
Response.Write("<br><br>File Path: " & filepath)
Dim WshShell, fso
Set WshShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filepath) Then
WshShell.run Path , 1, True
Response.Write "<br><br><br><div align=""center""><b>"
Response.Write "Register <font color=""#C50D6F"">"
Response.Write Path & "</font> succeeded !</b></div>"
Else
Response.Write "<br><br><br><div align=""center""><b>"
Response.Write "Target DLL not found at filepath!</b>"
Response.Write "</div>"
End If
Set fso = Nothing
Set WshShell = Nothing
Else
%>
<br><br>
<div align="center" style="background-color: #E3E4EB;
margin:100px;border:1px ridge #000000;">
<form method="POST">
<b><font color="#2A00A2">Register DLL</font></b>
<br> <input name="path" type="text" size="40" value="regsvr32 /s ">
<br>e.g. <font color="#57009C" style="background-color:white">
<b>regsvr32 /s G:\domain\app\DLL\test9.dll</b></font><br><br>
<input type="submit" value="Submit" style="background-color:#BDC99B;">
</form>
</div>
<%
End If
%>
The above code produces this:

Registering ActiveX DLL
How to unregister this COM Component? move over to the next page.
How to unregister?
Use this unregister.asp to unregister:
<%
' unregister.asp
Response.Write("Current Path: " & _
Server.MapPath(Request.ServerVariables("SCRIPT_NAME")))
''Work only if Path string includes the regsvr32 /u /s
Path = Request.Form("Path")
If Path <> "" Then
filepath = Replace(Path, "regsvr32 /u /s", "")
filepath = Trim(filepath)
Response.Write("<br><br>File Path: " & filepath)
Dim WshShell, fso
Set WshShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filepath) Then
WshShell.run Path , 1, True
Response.Write "<br><br><br><div align=""center""><b>"
Response.Write "UnRegister <font color=""#C50D6F"">"
Response.Write Path & "</font> succeeded !</b></div>"
else
Response.Write "<br><br><br><div align=""center""><b>"
Response.Write "Target DLL not found at filepath!</b>"
Response.Write "</div>"
end if
Set fso = Nothing
Set WshShell = Nothing
Else
%>
<br><br>
<div align="center" style="background-color: #E3E4EB;
margin:100px;border:1px ridge #000000;">
<form method="POST">
<b><font color="#2A00A2">UnRegister DLL</font></b>
<br> <input name="path" type="text" size="40" value="regsvr32 /u /s ">
<br>e.g. <font color="#57009C" style="background-color:white">
<b>regsvr32 /u /s G:\domain\app\DLL\test9.dll</b></font><br><br>
<input type="submit" value="Submit" style="background-color:#BDC99B;">
</form>
</div>
<%
End If
%>
The above code produces this:

Unregistering ActiveX DLL
You should then place these 2 files in the same folder as the DLL, so that you can read the local file path as produced by the following string in the aforesaid pages : Response.Write ("Current Path: " & Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))) . This will help you to input the correct filepath for your DLL.
[Actually I had tried using a site filepath -- like /folder/my.dll -- for input and then converted it into the server local path using Server.MapPath(). Unfortunately it did not work. ]
Next, enter the full string example regsvr32 /s G:\domain\app\DLL\test9.dll and the component should be properly registered. Do not remove the /s which means silent mode so as not to alarm the server administrator unnecessarily with a pop-up window on his computer screen that says your DLL has been successfully registered or unregistered.
How to Replace a previous DLL with a new one?
If you have a DLL already registered but wish to replace it there are 2 options:
- Break compatibility of the new DLL with the old one. This means:
- You need to set the Project Properties/Component to "No Compatibility".
- You need to change the project name in VB IDE and then save the project with this new project name. Recompile the DLL and you need to use a new name DLL say abc2.dll if the old one was abc.dll.
[The reason is because you are probably going to place this new DLL in the same folder as the old one. You cannot just replace the old one because it is in use -- until you unregister it and after the next server reboot.]
- Next place the new DLL in the same folder at the the website and use register.asp to register it.
- Change the project name in your calling asp pages at the website so that they will invoke the new DLL and not the old one. Now your new program is ready to run. Simply use your browser to open your asp pages and you will see the changes.
- Remember to unregister the old DLL using unregister.asp.
- Maintain compatibility of the new DLL with the old one. This means :
- You need to keep the default setting in the Project Properties/Component which is "Project Compatibility". You will probably see that the original DLL name appears in the textbox right below this selection.
- You do not need to change the project name and do not need to change project name in your calling asp pages.
- You just recompile your project with a new DLL name.
- Next place the new DLL in the same folder of your old DLL at the the website. Use unregister.asp to unregister the old DLL. Then use register.asp to register this new DLL.
- The new program code will NOT take immediate effect. The old DLL is still working UNTIL the server reboots. So you need to wait for this event to happen first. Then the new DLL will take effect.
Overview
If you are developing an ActiveX DLL for a customer and his website is hosted on a third party server, how are you going to register and unregister the component without having to request the server administrator to do it for you. The latter may even have reservations about this for security or other business reasons.
The alternative would be to give up the idea of using an ActiveX DLL and go back to using scripted ASP pages. However, sometimes you or your customer might need to protect the program logic from prying eyes.
So what is the solution? Simply register or unregister a component yourself using the following ASP scripts:
Use this register.asp to register :
<%
' register.asp
Response.Write ("Current Path: " & _
Server.MapPath(Request.ServerVariables("SCRIPT_NAME")))
''Work only if Path string includes the regsvr32 /s
Path = Request.Form("Path")
If Path <> "" Then
filepath = Replace(Path, "regsvr32 /s", "")
filepath = Trim(filepath)
Response.Write("<br><br>File Path: " & filepath)
Dim WshShell, fso
Set WshShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filepath) Then
WshShell.run Path , 1, True
Response.Write "<br><br><br><div align=""center""><b>"
Response.Write "Register <font color=""#C50D6F"">"
Response.Write Path & "</font> succeeded !</b></div>"
Else
Response.Write "<br><br><br><div align=""center""><b>"
Response.Write "Target DLL not found at filepath!</b>"
Response.Write "</div>"
End If
Set fso = Nothing
Set WshShell = Nothing
Else
%>
<br><br>
<div align="center" style="background-color: #E3E4EB;
margin:100px;border:1px ridge #000000;">
<form method="POST">
<b><font color="#2A00A2">Register DLL</font></b>
<br> <input name="path" type="text" size="40" value="regsvr32 /s ">
<br>e.g. <font color="#57009C" style="background-color:white">
<b>regsvr32 /s G:\domain\app\DLL\test9.dll</b></font><br><br>
<input type="submit" value="Submit" style="background-color:#BDC99B;">
</form>
</div>
<%
End If
%>
The above code produces this:

Registering ActiveX DLL
How to unregister this COM Component? move over to the next page.
How to unregister?
Use this unregister.asp to unregister:
<%
' unregister.asp
Response.Write("Current Path: " & _
Server.MapPath(Request.ServerVariables("SCRIPT_NAME")))
''Work only if Path string includes the regsvr32 /u /s
Path = Request.Form("Path")
If Path <> "" Then
filepath = Replace(Path, "regsvr32 /u /s", "")
filepath = Trim(filepath)
Response.Write("<br><br>File Path: " & filepath)
Dim WshShell, fso
Set WshShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filepath) Then
WshShell.run Path , 1, True
Response.Write "<br><br><br><div align=""center""><b>"
Response.Write "UnRegister <font color=""#C50D6F"">"
Response.Write Path & "</font> succeeded !</b></div>"
else
Response.Write "<br><br><br><div align=""center""><b>"
Response.Write "Target DLL not found at filepath!</b>"
Response.Write "</div>"
end if
Set fso = Nothing
Set WshShell = Nothing
Else
%>
<br><br>
<div align="center" style="background-color: #E3E4EB;
margin:100px;border:1px ridge #000000;">
<form method="POST">
<b><font color="#2A00A2">UnRegister DLL</font></b>
<br> <input name="path" type="text" size="40" value="regsvr32 /u /s ">
<br>e.g. <font color="#57009C" style="background-color:white">
<b>regsvr32 /u /s G:\domain\app\DLL\test9.dll</b></font><br><br>
<input type="submit" value="Submit" style="background-color:#BDC99B;">
</form>
</div>
<%
End If
%>
The above code produces this:

Unregistering ActiveX DLL
You should then place these 2 files in the same folder as the DLL, so that you can read the local file path as produced by the following string in the aforesaid pages : Response.Write ("Current Path: " & Server.MapPath(Request.ServerVariables("SCRIPT_NAME"))) . This will help you to input the correct filepath for your DLL.
[Actually I had tried using a site filepath -- like /folder/my.dll -- for input and then converted it into the server local path using Server.MapPath(). Unfortunately it did not work. ]
Next, enter the full string example regsvr32 /s G:\domain\app\DLL\test9.dll and the component should be properly registered. Do not remove the /s which means silent mode so as not to alarm the server administrator unnecessarily with a pop-up window on his computer screen that says your DLL has been successfully registered or unregistered.
How to Replace a previous DLL with a new one?
If you have a DLL already registered but wish to replace it there are 2 options:
- Break compatibility of the new DLL with the old one. This means:
- You need to set the Project Properties/Component to "No Compatibility".
- You need to change the project name in VB IDE and then save the project with this new project name. Recompile the DLL and you need to use a new name DLL say abc2.dll if the old one was abc.dll.
[The reason is because you are probably going to place this new DLL in the same folder as the old one. You cannot just replace the old one because it is in use -- until you unregister it and after the next server reboot.]
- Next place the new DLL in the same folder at the the website and use register.asp to register it.
- Change the project name in your calling asp pages at the website so that they will invoke the new DLL and not the old one. Now your new program is ready to run. Simply use your browser to open your asp pages and you will see the changes.
- Remember to unregister the old DLL using unregister.asp.
- Maintain compatibility of the new DLL with the old one. This means :
- You need to keep the default setting in the Project Properties/Component which is "Project Compatibility". You will probably see that the original DLL name appears in the textbox right below this selection.
- You do not need to change the project name and do not need to change project name in your calling asp pages.
- You just recompile your project with a new DLL name.
- Next place the new DLL in the same folder of your old DLL at the the website. Use unregister.asp to unregister the old DLL. Then use register.asp to register this new DLL.
- The new program code will NOT take immediate effect. The old DLL is still working UNTIL the server reboots. So you need to wait for this event to happen first. Then the new DLL will take effect.
|