Register COM Components Remotely via an
ASP Page or from the Context Menu

By Peter A. Bromberg, Ph.D.

Peter Bromberg

If you work with COM dlls a lot as I do, you often have a need to register / unregister these components conveniently. Often with only FTP or HTTP access to your remote production webserver, it would also be convenient to be able to call an ASP page that asks you for the full path and name of the DLL, and allows you to either register or unregister the specified component. Fear not! Here I'll present easy ways to do both. First, lets whip up an ASP page that lets us remotely register/unregister our favorite DLL: The key to this operation is to use the RUN method of the WSH WshShell class. First, let's take a brief overview abstracted from the help file so you'll understand where we are going with this useful method:



Method: object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

object
WshShell object.
Arguments: strCommand
String value indicating the command line you want to run. You must include any parameters you want to pass to the executable file, .e.g. "Regsvr32.exe c:\apps\mydll.dll".
intWindowStyle
Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this information.
bWaitOnReturn
Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program (0 for success). If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).

Remarks

The Run method returns an integer. The Run method starts a program running in a new Windows process. You can have your script wait for the program to finish execution before continuing. This allows you to run scripts and programs synchronously. Environment variables within the argument strCommand are automatically expanded. If a file type has been properly registered to a particular program, calling run on a file of that type executes the program. For example, if Word is installed on your computer system, calling Run on a *.doc file starts Word, and loads the document. The following table lists the available settings for intWindowStyle.

intWindowStyle Description
0 Hides the window and activates another window.
1 Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
2 Activates the window and displays it as a minimized window.
3 Activates the window and displays it as a maximized window.
4 Displays a window in its most recent size and position. The active window remains active.
5 Activates the window and displays it in its current size and position.
6 Minimizes the specified window and activates the next top-level window in the Z order.
7 Displays the window as a minimized window. The active window remains active.
8 Displays the window in its current state. The active window remains active.
9 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
10 Sets the show-state based on the state of the program that started the application.

Example

The following VBScript code opens the calling VBS script with Notepad:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run ("%windir%\notepad" & WScript.ScriptFullName)

The following VBScript code does the same thing, except it specifies the window type, waits for Notepad to be shut down by the user, and saves the error code returned from Notepad when it is shut down.

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("notepad " & WScript.ScriptFullName, 1, true)


So now let's construct our most simple implementation of an ASP page that uses this technique:

<%
Dim Scriptname
ScriptName = "RegUnReg.asp"
if Request.Form("SUBMIT") = "" Then
Response.Status = "401 - Unauthorized"
With Response
.write "<CENTER><FORM ACTION=""" & ScriptName & """ METHOD=POST>"
.write "<INPUT TYPE=TEXT NAME=FILEPATH SIZE=120>Full Path/Filename to Reg/UnReg<BR>"
.write "<INPUT TYPE=CHECKBOX NAME=ACTIONTYPE CHECKED>Checked=Register; Unchecked=Unregister<BR>"
.write "<INPUT TYPE=SUBMIT NAME=SUBMIT VALUE=SUBMIT></CENTER>"
.write "</FORM>"
end with

else

Set fso = Server.CreateObject("Scripting.FileSystemObject")
RegSrv = fso.GetSpecialFolder(1) & "\REGSVR32.EXE /s "
Set WshShell =CreateObject("WScript.Shell")
runstring = "CMD /C " & RegSrv & " /u "
if Request.Form("ACTIONTYPE")="on" then runstring = "CMD /C " & RegSrv
Return = WshShell.Run(runstring & Request.Form("FILEPATH"), 1, True)

Response.Write "Result: " & Return
Set fso=Nothing
Set WshShell = Nothing
end if
%>

Make sure you get all the syntax and spaces in the right places above, or your script may fail!. If you copy the entire script above including the <% %> script delimiters, and paste it into Notepad and save it as "RegUnReg.asp" on your server, it should work right out of the box. Note the "401 Unauthorized" Response Header near the top is a good security idea on a production box to help you remember your own username and password, right? Now the only thing you have to know is the drive letter, path and name of the DLL. If your development machine's setup parallels your production box, you could put a dummy "<INPUT TYPE=FILE" control on there as well, and use that to actually search your own filesytem to easily get the full path and filename to "Paste" right into the real formfield.

In addition, and in response to some helpful user comments, I've included a zip at the link below containing the VB 6.0 project and compiled DLL for "LoginAdmin.ImpersonateUser" which use the LogonUSer and ImpersonateUser API's to enable your script to impersonate, for example, a member of the Power Users group which may be required to gain access to REGSVR32.EXE through IIS.

Setting Shell Context Menu items to Register / Unregister COM Dll's via Registry Entries

Another handy tip is to enter the shell registry command values to enable you to right click on any COM DLL and either register or unregister it. Copy the entire colored text below to a file with a ".REG" extension and double click on it. You'll thank me later.

REGEDIT4

[HKEY_CLASSES_ROOT\dllfile\shell\Unregister\command]
@="regsvr32.exe /u %1"

[HKEY_CLASSES_ROOT\dllfile\shell\Register\command]
@="regsvr32.exe %1"

 

Download the code that accompanies this article

Peter Bromberg is an independent consultant specializing in distributed .NET solutionsa Senior Programmer / Analyst at in Orlando and a co-developer of the NullSkull.com developer website. He can be reached at info@eggheadcafe.com