0

I'm trying to unmute the volume from a console app using the standard SendMessageW() method. However, we have our own application running as the shell, instead of explorer.exe. When I try to unmute, nothing happens; but as soon as I start Explorer it works fine. Does explorer have to be running for messages to be sent?
Code:

private const int APPCOMMAND_VOLUME_MUTE = 0x80000; 
private const int WM_APPCOMMAND = 0x319;
SendMessageW(Process.GetCurrentProcess().MainWindowHandle, WM_APPCOMMAND, Process.GetCurrentProcess().MainWindowHandle, (IntPtr)APPCOMMAND_VOLUME_MUTE);

Return value from SendMessageW() is 0.
If Explorer is needed, does anybody know of an alternate method of unmuting?

Side Note
I just tried it using vbs:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))

And got the same results. It only unmutes if explorer.exe is running.

Community
  • 1
  • 1
Marcus
  • 5,407
  • 3
  • 31
  • 54

2 Answers2

5

SendMessageW is a function found in User32.DLL and what it does is it sends a message to an application that tells it to do something. In this case you are sending a message to Explorer and telling it to mute the sound. This message gets sent by SendMessageW regardless of whether explorer is running or not, however, if you are running your own shell, your shell isn't programmed to know what to do with this message and its just ignoring it.

You can do one of two things. The first one is to implement APPCOMMAND_VOLUME_MUTE in your shell so that when the message gets sent, it can mute the sound. The second one is to mute the sound using the WinMM.dll file and use the code posted here to mute the sound instead (which is what explorer.exe does after it receives the mute command).

Community
  • 1
  • 1
Icemanind
  • 47,519
  • 50
  • 171
  • 296
2

A window can process a WM_APPCOMMAND itself but almost never does, it just passes it on to DefWindowProc(). Which then delivers the message to the shell through the WH_SHELL hook. A shell must listen to them by calling SetWindowsHookEx() to initialize the hook. And gets the notification through the callback with the HSHELL_APPCOMMAND notification.

That's you.

Being the shell isn't easy.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536