Running Windows XP Control Panel Applets from Visual Basic.NET 2005

I've always been intrigued by the Control Panel; what are those items in the Control Panel? Are they programs? What makes them appear? I'm not talking about what the various objects inside the Control Panel do or mean; I'm speaking about what make those objects tick, how I can make things easier for myself by running those objects directly from my programs. With this article, I will show you how to run all the various Control Panel applets from your own program.

Control Panel Applets Explained

Control Panel Applets are special purpose Dynamic Link Libraries (DLLs) that let users configure the environment of Microsoft® Windows®.

The primary responsibility of any Control Panel Applet is to display a window (typically a dialog box or property sheet) and to carry out any tasks the user specifies. Despite this responsibility, Control Panel Applets do not provide menus or other direct means for users to access their dialog boxes. Instead, these applications operate under the control of another application and launch their subprograms only when requested by the controlling application. Control Panel Applets, such as the Mouse applet, are usually controlled by a Windows system utility specifically designed to give users access to these applications.

Most Control Panel Applets display and manage a single subprogram, giving the user control of the settings and operational modes of a single system component. However, any given Control Panel Applet can provide any number of subprograms to control any number of system components. To distinguish among subprograms, a Control Panel Applet typically supplies the controlling application with a unique icon for each subprogram. The controlling application displays these icons, and the user can choose a subprogram by choosing the corresponding icon.

Behind the Scenes in Executing These Applets

To open or run these applets, you could use a Windows utility named RunDll32. RunDll32 enables you to call functions exported from a 32-bit DLL. These functions must have the following syntax:

void CALLBACK EntryPoint(
   HWND hwnd,           // handle to owner window
   HINSTANCE hinst,     // instance handle for the DLL
   LPTSTR lpCmdLine,    // string the DLL will parse
   int nCmdShow         // show state
);

Now, I'm not going to go into the details of the physical contents these files need to have, but I'm just quickly going to explain how the RunDll32 utility works. Rundll32 loads the specified DLL using LoadLibrary, obtains the address of the function using the GetProcAddress function, and calls the function with the specified arguments, if any. When the function returns, Rundll32 unloads the DLL and exits.

It is possible to create a Unicode version of the function. Rundll32 first tries to find a function named EntryPointW. If it cannot find this function, it tries EntryPointA, and then EntryPoint.

Executing the Control Panel Applets

To use the RunDll32 utility in your program, you need to have a look at the Process class. This class provides access to local and remote processes and enables you to start and stop local system processes.

A Process object provides access to a process that is running on a computer. A process, in the simplest terms, is a running application. A thread is the basic unit to which the operating system allocates processor time.

To start a Process, you can use the Start method from the Process class; you also can include the StartInfo parameter that members can use to duplicate the functionality of the Run dialog box of the Windows Start menu. Anything that can be typed into a command line can be started by setting the appropriate values in the StartInfo property. The only StartInfo property that must be set is the FileName property. The FileName property does not have to be an executable file. It can be of any file type for which the extension has been associated with an application that is installed on the system. For example, the FileName property can have a .txt extension if you have associated text files with an editor, such as Notepad; or it can have a .doc extension if you have associated .doc files with a word processing tool, such as Microsoft Word.

On the command line, you can specify actions to take for certain types of files. For example, you can print documents or edit text files. Specify these actions by using the Verb member of the StartInfo property. For other types of files, you can specify command-line arguments when you start the file from the Run dialog box. For example, you can pass a URL as an argument if you specify your browser as the FileName. These arguments can be specified in the StartInfo property's Arguments member.

Putting Everything Together

Running Accessibility Options

'Open first tabpage of the Accessibility Options applet
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL access.cpl,,1")
'Open second tabpage of the Accessibility Options applet
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL access.cpl,,2")
'Open third tabpage of the Accessibility Options applet
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL access.cpl,,3")

As you can see, it is very easy running this applet with the use of the Process class. Here, you use Control_RunDLL as your Entrypoint for the shell32.dll. If you read closely, you would notice the last part of each statement. The numbers I've used are the numbers of the tabpages I wanted to show within the dialog box. Easy? Yes! I thought so too.

Running Add or Remove Programs

Static AddRemoveCount As Integer

'add & remove programs
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL appwiz.cpl,," & AddRemoveCount)

AddRemoveCount += 1 'go to next option
If AddRemoveCount > 3 Then AddRemoveCount = 0

The same rule of thumb applies here, except that with the Add or Remove Programs dialog, you are not dealing with tabs, but with four options, namely:

  1. Change or Remove Programs
  2. Add New Programs
  3. Add/Remove Windows Components
  4. Set Program Access and Defaults

This time, however, I made use of a Static variable counter named AddRemoveCount that keeps track of how many times the button is clicked; then, based on that, concatenates its value to the command string you are running. If the static variable equals the value of the last option, you just reset it to 0 to step through the options again.

Running Date & Time Settings

Static DateTimeCount As Integer

'date & time
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL timedate.cpl,," & DateTimeCount)
DateTimeCount += 1 'go to next tab

If DateTimeCount > 2 Then DateTimeCount = 0

Running Display

Static DisplayCount As Integer
DisplayCount += 1

If DisplayCount > 5 Then DisplayCount = 0
'display
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL desk.cpl,," & DisplayCount)

Running Game Controls

'game controllers
Process.Start("rundll32.exe", "shell32.dll,Control_RunDLL joy.cpl")

With this applet, there aren't any additional tabs.

Running Internet Options

Static InternetCount As Integer
InternetCount += 1

If InternetCount > 7 Then InternetCount = 0
'Internet options
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL inetcpl.cpl,," & InternetCount)

Running Keyboard

Static KeyboardCount As Integer

KeyboardCount += 1

If KeyboardCount > 2 Then KeyboardCount = 0
'keyboard
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL main.cpl,@1," & KeyboardCount)

Running Mouse

Static MouseCount As Integer

MouseCount += 1

If MouseCount > 5 Then MouseCount = 0
'mouse
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL main.cpl,@0," & MouseCount)

Running Windows XP Control Panel Applets from Visual Basic.NET 2005

Running System

Static SystemCount As Integer

SystemCount += 1

If SystemCount > 7 Then SystemCount = 0
'system
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL sysdm.cpl,@1," & SystemCount)

Running Sounds and Audio

Static SoundCount As Integer

SoundCount += 1

If SoundCount > 5 Then SoundCount = 0
'sounds & audio
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL mmsys.cpl,," & SoundCount)

Running Add Printer Wizard

'open add printer wizard
Process.Start("rundll32.exe", _
              "shell32.dll,SHHelpShortcuts_RunDLL AddPrinter")

Here, you used SHHelpShortcuts_RunDLL as the Entrypoint in the shell32.dll.

Running Regional and Language Options

Static RegionalCount As Integer

RegionalCount += 1

If RegionalCount > 3 Then RegionalCount = 0
'regional settings
Process.Start("rundll32.exe", "shell32.dll,_
              Control_RunDLL intl.cpl,," & RegionalCount)

Running Taskbar and Start Menu

'taskbar & start menu
Process.Start("rundll32.exe", "shell32.dll,Options_RunDLL 1")

Here, you used Options_RunDLL as the Entrypoint.

Opening the Folder Options dialog

'folder options
Process.Start("rundll32.exe", "shell32.dll,Options_RunDLL 0")

You also used Options_RunDLL here as your Entrypoint, but this time, you passed 0 to it.

Opening Control Panel

'open the control panel
Process.Start("rundll32.exe", "shell32.dll,Control_RunDLL")

Just using the Control_RunDLL Entrypoint, without any arguments, opens the Control Panel.

Running Phone & Modem Settings

'phone & modem settings
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL telephon.cpl")

Running Power Settings

'power settings
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL powercfg.cpl")

Running Add Hardware Wizard

'add hardware wizard
Process.Start("rundll32.exe", "shell32.dll, _
              Control_RunDLL hdwwiz.cpl")

Opening the Fonts folder

'open the fonts folder
Process.Start("RUNDLL32.EXE", "shell32.dll, _
              SHHelpShortcuts_RunDLL FontsFolder")

You used SHHelpShortcuts_RunDLL as the Entrypoint into shell32.dll again (just as you did when opening the Add Printer Wizard earlier). This time, however, you passed FontsFolder to the function.

Opening the Administrative Tools folder

'administration tools
Process.Start("control.exe", "admintools")

This time, you didn't run RunDll32.exe at all, but Control.exe instead. Control.exe is another utility included with Windows; it also can run Control Panel applets. The nice thing about Control.exe is that it recognises special names for commonly used Control Panel items, meaning you can execute an item such as AdminTools just by using its special name instead of the corresponding .cpl file.

Running Windows® Firewall

'firewall settings
Process.Start("RunDll32.exe", "shell32.dll, _
              Control_RunDLL firewall.cpl")

Running Automatic Updates

'Autopmatic updates
Process.Start("RunDll32.exe", "shell32.dll, _
              Control_RunDLL wuaucpl.cpl")

Running Network Setup Wizard

'network setup
Process.Start("RunDll32.exe", "netshell.dll,StartNCW")

Opening the Network Connections folder

'network connections
Process.Start("RunDll32.exe", "shell32.dll,Control_RunDLL ncpa.cpl")

Opening User Accounts

'users
Process.Start("RunDll32.exe", "shell32.dll, _
              Control_RunDLL nusrmgr.cpl")

Opening the Scheduled Tasks folder

'scheduled tasks
Process.Start("control.exe", "schedtasks")

Running Security Center

'security settings
Process.Start("RunDll32.exe", "shell32.dll, _
              Control_RunDLL wscui.cpl")

Update: 29/10/07

I came across three more dialogs you can show via the use of RunDll32.exe

  1. The ODBC Data Source Administrator dialog. This is the dialog box used to set up Open DataBase Connectivity sources, here's more information.
  2. The Opens With dialog box. This is the dialog box that opens up after you have right-clicked a file, and selected Open With.
  3. The Safely Remove Hardware dialog box. This is the box you see when you need to remove your memory stick from the computer.

Opening the ODBC Data Source Administrator

'Open ODBC dialog box
Process.Start("RunDll32.exe", "shell32.dll,Control_RunDLL odbccp32.cpl")

Opening Opens With

Process.Start("RunDll32.exe", "shell32.dll,OpenAs_RunDLL test.abd")
'Opens the Open With dialog box
'(the dialog box when you right click a file, and select Open With)

This is actually quite handy when you have to set File Associations for unknown file types on the system. In the above example, I've use a filename of test with an .abd extension, with the use of executing this box from within our program, you can set up the necessary file association. I also made use of the OpenAs Entrypoint into Shell32.dll.

Opening Safely Remove Hardware

'Opens the Safely remove Hardware dialog to remove, for example,
'your USB memory stick
Process.Start("RunDll32.exe", "shell32.DLL,Control_RunDLL HotPlug.dll")

With the code above, I used Control_RunDLL Entrypoint to run the DLL file named HotPlug.dll. This produces the Safely Remove Hardware dialog box.

Conclusion

I hope you have enjoyed reading this article and I hope that I have succeeded in teaching you something new or something unique.



About the Author

Hannes du Preez

Hannes du Preez is a Microsoft MVP for Visual Basic. He is a trainer at a South African-based company. He is the co-founder of hmsmp.co.za, a community for South African developers.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • The exponential growth of data, along with virtualization, is bringing a disruptive level of complexity to your IT infrastructure. Having multiple point solutions for data protection is not the answer, as it adds to the chaos and impedes on your ability to deliver consistent SLAs. Read this white paper to learn how a more holistic view of the infrastructure can help you to unify the data protection schemas by properly evaluating your business needs in order to gain a thorough understanding of the applications …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds