Monday, December 27, 2010

Disable Sound / Mute Computer

In this post I want to show how to mute the computer. Again, the solution goes over the WinAPI, which is why using System.Runtime.InteropServices; is needed.
The needed P/Invoke function is SendMessageW and has the signature ntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam).
The function sends a message to a specific program or window. To disable the sound in the computer, we send a command to a system process.
The code looks as follows, first the declaration part:

        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
        private const int WM_APPCOMMAND = 0x319;

And the function:

private void Mute()
{
    SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE);
}

No comments:

Post a Comment