IMMDevice Activation (windows-rs)

eora 0 Reputation points
2025-02-10T07:02:05.72+00:00

Hello, I've been trying to activate any IMMDevice, for the moment just trying to access a default audio endpoint to collect data on current audio sessions. I've attempted a few times, and no matter what IMMDevice apparently doesn't have the Activate function, which sounds absolutely ridiculous. I looked at a few other projects that implement this, I consulted the features references for windows-rs and C++ documentation too. I should be using the correct feature flags:

use windows::{
    core::*,
    Win32::{Devices::FunctionDiscovery::*, Media::Audio::*, System::Com::*},
};
use crate::hook::Hook;
use arrayvec::ArrayVec;
use anyhow::Result;


unsafe fn default_devices() -> Result<(IMMDevice,IMMDevice)> {
    let device_enumerator: IMMDeviceEnumerator =
        CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL)?;
    let device1 = device_enumerator.GetDefaultAudioEndpoint(eRender, eConsole)?;
    let device2 = device_enumerator.GetDefaultAudioEndpoint(eCapture, eConsole)?;
    Ok((device1, device2))
}

fn audio_ch(hook: &mut Hook) -> Result<()> {

        unsafe {
            let (input_device, output_device) = default_devices()?;
            let mut audio_sessions = Vec::new();
            let device_id: PWSTR = input_device.GetId()?;
            let device_state: DEVICE_STATE = input_device.GetState()?;
            let audio_client: IAudioClient = input_device.Activate(CLSCTX_ALL, None)?;
            //let mut audio_session_manager: IAudioSessionManager2 = output_device.Activate(CLSCTX_ALL, None)?;
    }
    Ok(())
}

My cargo dependencies and feature imports look like this:

[dependencies.windows]
version = "0.59"
features = [   
    "Win32_UI_WindowsAndMessaging",  
    "Win32_UI_Input_KeyboardAndMouse",
    "Win32_System_Threading",
    "Win32_System_LibraryLoader",
    "Win32_System_Console",
    "Win32_Foundation",
    "Win32_System_SystemInformation",
    "Win32_System_Registry",
    "Win32_System_Time",
    "Win32_Security",
    "Win32_Foundation",
    "Win32_Media_Audio",
    "Win32_Media_Audio_Endpoints",
    "Win32_Media_Multimedia",
    "Win32_System_Com",
    "Win32_Devices_FunctionDiscovery",
]

I imported way more features then I need, some are being used in other parts of my project. I cleaned the project as first with cargo and built it a few times, but I get the same error:

error[E0599]: no method named `Activate` found for struct `Audio::IMMDevice` in the current scope
  --> winlib/src/audioinf.rs:41:59
   |
41 |             let audio_client: IAudioClient = input_device.Activate(CLSCTX_ALL, None)?;
   |                                                          

I probably made an obvious mistake but I can't really find any resources that dispute it or point it obviously yet so I wanted to post this here, appreciate any suggestions.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,737 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.