Disable playback/recording device programmatically (cmd, c#) Windows

1

I am trying to disable the microphone from cmd or c#. enter image description here

I tried to use devcon.exe, but it disables the device in Device Manager but not in the sound properties.

I tried to write to the registry:

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{6f512f63-a7a0-4f4f-b24c-73dd352b6709}" /v DeviceState /d 0x10000001 /t Reg_DWord /f

Even in the elevated mode in cmd I get the error:

ERROR: Access is denied.

I read it is because the registry is protected and I would have to take the ownership of the registry before applying that change.

When I use .reg file:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{6f512f63-a7a0-4f4f-b24c-73dd352b6709}]
"DeviceState"=dword:0x10000001

Everything seems to be fine, no error is displayed, but the registry is not changed.

I also tried to run the above commands from the Windows Service (Local System), but with no success.

What are the other ways I can try to disable playback/recording devices programmatically?

EDIT:
I modified the reg file a bit. I used Procmon.exe to check what is changed when I click 'Disable' and now reg file looks like this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{6f512f63-a7a0-4f4f-b24c-73dd352b6709}]
"DeviceState"=dword:10000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{6f512f63-a7a0-4f4f-b24c-73dd352b6709}\FxProperties]
"{00123682-6AD4-4737-98CC-6092E12453CC},0"=dword:00000000
"{10786EAC-45AF-49ea-9182-4B994BD075E8},1"=dword:00000003
"{10786EAC-45AF-49ea-9182-4B994BD075E8},0"=hex:41,00,00,00,01,00,00,00,00,00,\
  00,00,3c,4f,01,00,00,00,00,00,08,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
\\....

When I applied this .reg file as a user (by clicking on the file), the recording device gets disabled correctly. I have two questions now:
1. It seems to be a very messy solution, is it safe to use this reg file?
2. I cannot apply this reg file using Windows Service, I am guessing, these registries are blocked by the User account or something, right?

Chris

Posted 2019-12-17T10:28:55.787

Reputation: 31

Does this answer your question? Shortcut to enable/disable a device in Windows

– CaldeiraG – 2019-12-18T13:41:43.057

Unfortunately not. As I wrote in the question, devcon.exe allows to disable/enable devices in Device Manager - this is something different than disable/enable in sound properties. – Chris – 2019-12-18T14:05:06.953

Right, I missed that part, sorry about that. – CaldeiraG – 2019-12-18T14:16:18.683

No answers