From 33dc72ab4508f0a6a6e729b12fe9328eefaf1aba Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Tue, 22 Sep 2015 16:05:40 +0200 Subject: [PATCH] Add device name on WASAPI --- src/wasapi/mod.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/wasapi/mod.rs b/src/wasapi/mod.rs index 1212a67..5d6267c 100644 --- a/src/wasapi/mod.rs +++ b/src/wasapi/mod.rs @@ -2,9 +2,12 @@ extern crate winapi; extern crate ole32; use std::io::Error as IoError; +use std::os::windows::ffi::OsStringExt; +use std::ffi::OsString; use std::sync::{Arc, Mutex, MutexGuard}; use std::ptr; use std::mem; +use std::slice; use Format; use FormatsEnumerationError; @@ -51,9 +54,28 @@ unsafe impl Send for Endpoint {} unsafe impl Sync for Endpoint {} impl Endpoint { - #[inline] + // TODO: this function returns a GUID of the endpoin + // instead it should use the property store and return the friendly name pub fn get_name(&self) -> String { - "unknown".to_owned() // TODO: + unsafe { + let mut name_ptr = mem::uninitialized(); + // can only fail if wrong params or out of memory + check_result((*self.device).GetId(&mut name_ptr)).unwrap(); + + // finding the length of the name + let mut len = 0; + while *name_ptr.offset(len) != 0 { + len += 1; + } + + // building a slice containing the name + let name_slice = slice::from_raw_parts(name_ptr, len as usize); + + // and turning it into a string + let name_string: OsString = OsStringExt::from_wide(name_slice); + ole32::CoTaskMemFree(name_ptr as *mut _); + name_string.into_string().unwrap() + } } #[inline]