Subscribe to Windows IT Pro
December 11, 2001 12:00 AM

Scripting Solutions with WSH and COM: Short, Sharp Scripts

Windows IT Pro
InstantDoc ID #23366
Rating: (0)
Downloads
23366.zip

The Rundll32 Program
A nifty little application on your Windows machine lets your scripts call methods and properties inside DLLs to perform a variety of system tasks without intervention. All you need to do is call rundll32.exe from your WSH script and pass the necessary parameters.

For example, would you like your script to format a floppy disk? Create a standard WScript::Shell object, then call the Shell::Run method to run the Format command from the shell. Listing 4 shows FormatFloppy.vbs, which creates a Wscript::Shell object called objWSH, then calls rundll32 .exe. The script passes the parameters shell32.dll (the name of the DLL that will run the Format command) and SHFormatDrive (the method in the DLL that formats a drive). Because someone could modify this script to format hard disks, the usual caveats about limiting access to your crucial machines apply here.

If you need to copy a disk, use DiskCopier.vbs. Web exclusive Listing 3 shows this script. DiskCopier .vbs is similar to FormatFloppy.vbs, but DiskCopier.vbs opens diskcopy.dll and uses the DiskCopyRunDll method.

OpenAs.vbs displays the Open With What Application dialog box to let you choose the application that should open a file. Web exclusive Listing 4 shows OpenAs.vbs, which uses the OpenAs_RunDLL method call from the shell32.dll file. The script also needs to pass to the OpenAs_Run DLL method the name of the file to be opened—my code specifies the file C:\my scripts\test.txt, which you will replace with the name of your file.

Control Panel Applets
The final group of scripts I discuss lets you direct users to a specific Control Panel applet. Control Panel applets reside in Control Panel (.cpl) files, which are usually in C:\winnt\system32 on Windows XP, Win2K, and NT and in C:\windows\system on Windows Me and Win9x. A Control Panel file can contain more than one applet. For example, main .cpl holds both the Control Panel Keyboard applet and the Control Panel Mouse applet in XP and Win2K. In addition, a Control Panel applet can have several tabs—Web exclusive Figure 1 shows the Mouse applet and its five tabs. You can not only tell Rundll32 to open Control Panel applets but also specify which tab the window will display.

Using Rundll32, you call the Control_Run DLL method of shell32.dll and pass from one to three parameters. The syntax of the Rundll32 call is

rundll32.exe shell32.dll,Control_RunDLL file.cpl @X,Y

where the first parameter, file.cpl, is the filename (without the path) of the Control Panel applet you want to open. The remaining parameters are optional. The first optional parameter, @X, is the at (@) symbol followed by the number of the applet you want to open within the Control Panel file (the number of the first .cpl file is 0). The second optional parameter, Y, is the number of the tab that you want to select (the number of the first tab is always 0).

If you omit the first optional parameter, the method will open the first applet in the Control Panel file; if you omit the second optional parameter, the method will open the first tab. Listing 5 shows the script MouseAndKeyb .vbs, which opens the first tab of both the Mouse and Keyboard applets (applet 0 and applet 1, respectively) from within main.cpl. If you want to open the third tab of the Mouse applet in XP, you can use one of the following commands:

rundll32.exe shell32.dll,Control_RunDLL main.cpl,@0,2
rundll32.exe shell32.dll,Control_RunDLL main.cpl,,2

Web exclusive Listing 5 shows IE-CPLProps.vbs. This script opens IE's Control Panel applet (which is named Internet Properties in XP and Internet Options in Win2K) at the default tab, then again at the fifth tab. Opening specific tabs is a good way to focus users on the appropriate part of an open window. Similarly, Install.vbs, which Web exclusive Listing 6 shows, opens the Control Panel Add/Remove Programs, Windows Setup, and Startup Disk applets. All three of these applets reside in appwiz.cpl.

InstallScreenSaverAndLock.vbs is my final example. This script, which Listing 6 shows, illustrates how to combine Rundll32 functionality with opening a Control Panel applet. First, the script calls the Install-ScreenSaver method from the desktop Control Panel file (desk.cpl), which activates an existing installed screen saver—SSStars.scr in the sample code—in the C:\windows\system or C:\windows\system32 directory. Next, the script calls the LockWorkStation method from the user32.dll file to lock the workstation. I like to run a script such as InstallScreenSaverAndLock.vbs on all machines overnight to set the company screen saver and lock the workstations. However, be aware that this script can have unusual results with certain screen savers, and I haven't managed to determine why. In some cases, instead of activating the screen saver, the first Install-ScreenSaver line will only open the Display Properties applet with the Screen Saver tab selected. If that happens, the user will need to either click OK or select the screen saver, then click OK. This behavior is a bit nonsensical, so I recommend that before deploying this script, you make sure it works correctly with your screen saver.

I hope that you find these scripts useful. If you create some short, sharp scripts that you think other readers would like to see, please send them to me.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.