简体   繁体   中英

Disabling \ enabling an audio device with WinApi

Story first: ( Skip if not interested )

I've encountered a very annoying daily task where every time I play a game, I need to switch between my speakers and my headphones. This involves in getting to the Control Panel ( Because for some reason Windows 10 removed the option to right click the audio icon in the taskbar and get to the "Manage audio devices" quick ), going to Hardware and Sound, then Manage audio devices, then need to disable the speaker, enable the headphones, and vice versa.

ANNOYING task that I could ( hopefully with your help ) automate with just a program running in background and listening for a "Hotkey". ( There is probably an existing program already for that task, but I wish to learn WinApi through a problem I encountered, to motivate myself )


Now, I started to learn WinApi for that matter, and got stuck in how can I implement such a task, where I can disable one audio device and enable another one. My logic says that those devices have some sort of ID, and I can manipulate various properties through this ID.

I used the example from MSDN to enumerate every audio device using EnumAudioEndpoints And tried to get the device's properties so I could change them later. I Hoped I will encounter some sort of "state" variable which I can change later with IPropertyStore::SetValue function.

In the example, they used this piece of code:

hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);

Where PKEY_Device_FriendlyName key comes from functiondiscoverykeys_devpkey header. This is documented very badly ( if not at all ), I tried every single PROPERTYKEY ( Especially PKEY_Device_DevNodeStatus ), but each one returned empty results (Meaning there is no such property I can change to disable or enable a device through those interfaces they used), Excluding 3 properties :

  • PKEY_DeviceInterface_FriendlyName
  • PKEY_Device_DeviceDesc
  • PKEY_Device_FriendlyName

I've played with this a long time now. Asking for your help, how can I achieve the results I want ?

There may be no programming method to achieve this requirement.

As @Larry Osterman said that,

There is no public API which allows you to change the default audio device, that is functionality that is considered to be under the users control. This has always been the case in Windows.

Having said that, if you search the web, there are a number of people who have reverse engineered the APIs that are used in Windows Vista to do this, but I'm not going to point you to them (the reverse engineered APIs are internal unsupported APIs and may change without notice from Microsoft). You use these solutions at your own peril.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM