Wednesday, July 6, 2011

Fixing unresponsive volume controls

On my Vista laptop (a Toshiba Tecra S10), the volume controls occasionally are unresponsive when coming out of hibernation. When this is the case, turning the volume dial causes the whole taskbar to freeze. To fix this, I have to
  • kill explorer.exe,
  • disable the Realtek audio device,
  • stop the Windows Audio service,
  • re-enable the Realtek audio device,
  • restart the Windows Audio service,
  • and, finally, restart explorer.exe.
Until now, I have performed these steps manually each time my volume controls stopped working. Today, I got tired of it and decided to automate this. ;)

My first attempt was just to write a simple DOS batch script. For this I needed a command line utility to enable and disable devices. Fortunately, Microsoft provides just the utility I'm looking for. :)

Using that utility is quite simple. After putting the executable in one of my PATH directories, I looked for the Realtek device (in an elevated command window):

C:\>devcon find * | find "Realtek"

HDAUDIO\FUNC_01&VEN_10EC&DEV_0268&SUBSYS_11790602&REV_1000\4&24AF6467&0&0001: Realtek High Definition Audio (With EC)

C:\>devcon find *VEN_10EC* && REM Let's see if this substring uniquely finds the Realtek device

HDAUDIO\FUNC_01&VEN_10EC&DEV_0268&SUBSYS_11790602&REV_1000\4&24AF6467&0&0001: Realtek High Definition Audio (With EC)
1 matching device(s) found.

C:\>REM Great! It does :)

After testing the devcon utility, I wrote the following batch script:

However, this script has a flaw. The script requires admin rights in order to make changes to devices and services. This is in itself is not a problem, but if the script is running with admin rights, then the final line of the script causes the explorer.exe process to be restarted with elevated rights. As a result, any process started by the new explorer.exe process, e.g., through the Run dialog (Win+R), will also have administrative powers!

So I decided to write a small C# program instead. Luckily, Microsoft provides some sample code that shows us how to launch an elevated process. Based on this example, I wrote the following program that launches itself to execute a few commands with elevated rights, after which it restarts explorer.exe using the access rights it was originally started with:

Perfect! Now I don't have to go manually through all these tedious steps ever again. :)

No comments:

Post a Comment