I began by creating two module level variables, statMsg
, to store my status messages, and theShellObject
for the WSH Shell object. The form controls were all pretty straightforward. The only real time spent coding was on registering the components. When the user clicks the Register/Unregister Objects button, I check to see if any items exist in the listbox, get the total number items, create the WSH Shell object by setting my variable to the createTheShell()
function (shown in Listing 2
), and loop over each item passing the physical path to the regTheObj()
is the sub-procedure regTheObj()
that registers or unregisters the component. This sub-procedure is declared as private and accepts the object path by reference as a string data type. I declare a variable of the string data type named returnVar
to capture the Run
method's return value, and another variable named regType
, to store whether you are registering or unregistering the component. The latter is used for our status message.
Next, I check whether the user has decided to register or unregister the component. Depending on which they have chosen, I call the Run method of the Shell object passing in the syntax for the regsvr32 command line, an empty argument for the window style, and True for the final argument.
After the Run method has been executed, I check to make sure the return variable is not zeroa zero would indicate that there was an error. Then I update the status message accordingly.
The Run method of the shell object creates a new process that executes any environment variables and syntax, an optional integer argument for the window style of the new process, which is not utilized in a VB UI, and a Boolean that dictates whether to immediately return execution to the calling proceduresomewhat like multi-threadingor wait for the Shell's Run method to complete and return an any error codes.
|Figure 2. Command Line Method: The standard way to run Regsvr32.exe is from the DOS prompt. |
In listing 2
, the sub-procedure to instantiate the WSH shell object, I first make sure the object has not been instantiated by passing it to the typeName()
function. If an object variable has not yet been instantiated, the typeName
function will return the string "Nothing
." Alternatively, you could check to see if the typeName()
function were equal to "IWSHShell3
", the typeName()
return string for the WSH Shell object. I then declare a variable of type object and set that variable to the createObject
method, passing in the object identifier, wscript.shell
. I set the function name to that variable to return my object back to the regTheObj
See the sidebar for an explanation of the syntax for regsvr32.exe. Figure 2 shows an example of running regsvr32 via the command line.
A utility is only worthwhile if it's available and easily accessible on all your machines, so I ran the Package and Deployment Wizard to make this a distributable application. I would recommend always including some sort of documentation or readme file in your packageno matter how small your application. In this case, the article serves as documentation.