Programmatically Increase, Decrease and Mute the Volume

Abstract: This article demonstrates how to programmatically increase, decrease and mute the volume

Programmatically Increase, Decrease and Mute the Volume

I was recently working on the Windows API and thought of creating a small application that can increase, decrease and mute the volume in .NET. Since we will be consuming unmanaged DLL functions, hence I will be using P/Invoke that enables managed code to call unmanaged functions implemented in DLL’s, like the ones in the Win32 API. For this purpose, we will be using the System.Runtime.InteropServices namespace

In this article, I assume you are familiar with interoperating with Unmanaged Code. If you have never worked with unmanaged code, please read the MSDN article Interoperating with Unmanaged Code.

I have created a Windows Application and added three Button Controls to the form as shown below:

As seen in the code above, we are first using the DllImportAttribute attribute that provides the information needed to call a function exported from an unmanaged DLL. On each button click, we are using the SendMessageW() function to send the specified message to a window to increase, decrease and mute the volume.

I hope you liked the article and I thank you for viewing it.

Was this article worth reading? Share it with fellow developers too. Thanks!

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

this.Handle???? It doesn't seams to find it. I have just copy pasted the code so do I miss anything?

Comment posted by
Glenn
on Saturday, January 7, 2012 11:12 AM

Hi this has helped me out on something I am working on. One question though, Is it possible to make the volume increase continuosly without needing to repeatidly click on the button?

Comment posted by
patrick
on Friday, February 10, 2012 3:54 PM

Is he(it) possible to get back the information of the icon of the dumb volume
merci

Comment posted by
Kishor Patil
on Sunday, March 4, 2012 11:41 PM

It nice it’s worked properly thank you.............very much

Comment posted by
vijay
on Tuesday, March 6, 2012 11:19 PM

i want to decrease volume 505 directly please send code to my mail

Comment posted by
vijaya
on Tuesday, March 6, 2012 11:36 PM

i want to decrease volume 50% directly please send code to my mail

Comment posted by
Asanka
on Sunday, June 3, 2012 2:15 AM

Thank u very much agrawal. This is really helpful one. I am developing a web application. I need to decrease the volume by a given percentage. Can you mail me some coding to do that immediately. Really appreciate your work. Thanks in advance

Comment posted by
lokesh
on Thursday, July 12, 2012 6:56 AM

Very good post.. thank you

Comment posted by
bartj
on Tuesday, September 4, 2012 6:26 PM

Thank you! Your code works without any problems. You might mention that when you call the mute function it toggles back and forth between mute and un-mute. Just a detail most people would know but some may be confused as to how to un-mute. Thanks again