Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

How to mute the system volume after system lock

Rate me:
Please Sign up or sign in to vote.
4.23/5 (7 votes)
13 Oct 2009CPOL1 min read 36.7K   480   31   9
A utility to mute the system volume after a system lock and unmute after logging in.

Introduction

This small utility mutes the system volume each time you lock down your system. When you unlock your system, the volume is un-muted back. This utility uses the SessionSwitch event from the Win32 namespace. With this event, we can catch the session switch for the lock down and unlock events. Using user32.dll with the SendMessageW method, we can send the mute message to the system.

Background

In my company, we need to lock our system each time we move away from the computer. The volume of my speakers is a bit high, and my room partner complaints that he wants his quiet time whenever I leave the room. With this small utility, my system volume is mute every time I lock my computer.

Using the Code

Here is the declaration of the consts and the DllImport of user32.dll:

C#
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;

private const int WM_APPCOMMAND = 0x319;

[DllImport("user32.dll")]

public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, 
                     IntPtr wParam, IntPtr lParam);

We need to register to the SessionSwitch event, so first we need to add a reference to the Win32 namespace. Just add a using statement for Microsoft.Win32. Now add the following to register to the event:

C#
//Register to the SessionSwitch event
SystemEvents.SessionSwitch += 
new SessionSwitchEventHandler(this.SystemEvents_SessionSwitch);

Now, for the main event of this sample:

C#
private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
   //If the reason for the session switch is lock or unlock 
   //send the message to mute or unmute the system volume
   if (e.Reason == SessionSwitchReason.SessionLock)
   {
       SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                   (IntPtr)APPCOMMAND_VOLUME_MUTE); 
   }
   else if (e.Reason == SessionSwitchReason.SessionUnlock)
   {
       SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                   (IntPtr)APPCOMMAND_VOLUME_MUTE); 
   }
}

You must detach the SessionSwitch event when your application closes to avoid memory leaks.

C#
//Detach the SessionSwitch event

SystemEvents.SessionSwitch -= new SessionSwitchEventHandler
(this.SystemEvents_SessionSwitch);

Points of Interest

It is always nice to learn about the events of the Operating System.

History

  • 13th October, 2009: Initial post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPause Windows Media Player Pin
arnischwarz21-Oct-09 10:11
arnischwarz21-Oct-09 10:11 
GeneralSimple and good Pin
Sharath C V19-Oct-09 19:16
professionalSharath C V19-Oct-09 19:16 
Very simple and good article. Thank you.
GeneralRe: Simple and good Pin
Roey C26-Oct-09 2:15
Roey C26-Oct-09 2:15 
GeneralNot really (un)mute Pin
tlhIn`toq19-Oct-09 15:38
tlhIn`toq19-Oct-09 15:38 
GeneralRe: Not really (un)mute Pin
xlife18-Jan-13 10:09
xlife18-Jan-13 10:09 
QuestionPause after lock? Pin
RK KL15-Oct-09 8:31
RK KL15-Oct-09 8:31 
AnswerRe: Pause after lock? Pin
Ron Levy18-Oct-09 23:41
Ron Levy18-Oct-09 23:41 
AnswerRe: Pause after lock? Pin
Member 175775219-Oct-09 20:11
Member 175775219-Oct-09 20:11 
AnswerRe: Pause after lock? [modified] Pin
Per-Axel11-Nov-09 10:33
Per-Axel11-Nov-09 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.