Giter Site home page Giter Site logo

com_policy_config's Introduction

com-policy-config

COM definitions for the undocumented IPolicyConfig interface. This interface can be used to change to the default audio device on windows.

This is a manual port of PolicyConfig.h.

More additional resources:

Link 1

Link 2

Important: It is possible I made a mistake during the port or messed up some type. Take everything with a grain of salt.

Example

use windows::core::{PCWSTR, Result};
use windows::Win32::Devices::FunctionDiscovery::PKEY_Device_FriendlyName;
use windows::Win32::Media::Audio::{DEVICE_STATE_ACTIVE, eConsole, eRender, IMMDeviceEnumerator, MMDeviceEnumerator};
use windows::Win32::System::Com::{CLSCTX_ALL, CoCreateInstance, COINIT_MULTITHREADED, CoInitializeEx, CoUninitialize, STGM_READ};
use com_policy_config::{IPolicyConfig, PolicyConfigClient};

fn main() -> Result<()>{

    unsafe {
        CoInitializeEx(None, COINIT_MULTITHREADED)?;

        let enumerator: IMMDeviceEnumerator = CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL)?;

        println!("All Devices:");
        let device_collection = enumerator.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?;
        for i in 0..device_collection.GetCount()? {
            let device = device_collection.Item(i)?;
            let property_store = device.OpenPropertyStore(STGM_READ)?;
            let name = property_store.GetValue(&PKEY_Device_FriendlyName)?;
            println!("  {}", name.Anonymous.Anonymous.Anonymous.pwszVal.display());
        }

        let selected_device = device_collection.Item(0)?;
        let property_store = selected_device.OpenPropertyStore(STGM_READ)?;
        let name = property_store.GetValue(&PKEY_Device_FriendlyName)?;
        println!("Selected Device: {}", name.Anonymous.Anonymous.Anonymous.pwszVal.display());

        let device_id = PCWSTR(selected_device.GetId()?.0);
        let policy_config: IPolicyConfig = CoCreateInstance(&PolicyConfigClient, None, CLSCTX_ALL)?;
        policy_config.SetDefaultEndpoint(&device_id, eConsole)?;

        CoUninitialize();
    }

    Ok(())
}

License

MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.