private static ISimpleAudioVolume GetVolumeObject(string name) { // get the speakers (1st render + multimedia) device IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDevice speakers; deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers); // activate the session manager. we need the enumerator Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; object o; speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o); IAudioSessionManager2 mgr = (IAudioSessionManager2)o; // enumerate sessions for on this device IAudioSessionEnumerator sessionEnumerator; mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); // search for an audio session with the required name // NOTE: we could also use the process id instead of the app name (with IAudioSessionControl2) ISimpleAudioVolume volumeControl = null; for (int i = 0; i < count; i++) { IAudioSessionControl ctl; sessionEnumerator.GetSession(i, out ctl); IAudioSessionControl2 ctl2 = ctl as IAudioSessionControl2; //uint pid; //ctl2.GetProcessId(out pid); //Process p = Process.GetProcessById((int)pid); // ctl2.SetDisplayName(p.ProcessName, Guid.Empty); string dn; ctl2.GetDisplayName(out dn); if (string.Compare(name, dn, StringComparison.OrdinalIgnoreCase) == 0) { volumeControl = ctl2 as ISimpleAudioVolume; break; } Marshal.ReleaseComObject(ctl); Marshal.ReleaseComObject(ctl2); } Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(deviceEnumerator); return(volumeControl); }
public static IEnumerable <ApplicationVolumeInformation> EnumerateApplications() { // get the speakers (1st render + multimedia) device IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDevice speakers; deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers); // activate the session manager. we need the enumerator Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; object o; speakers.Activate(IID_IAudioSessionManager2, 0, IntPtr.Zero, out o); IAudioSessionManager2 mgr = (IAudioSessionManager2)o; // enumerate sessions for on this device IAudioSessionEnumerator sessionEnumerator; mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); for (int i = 0; i < count; i++) { IAudioSessionControl ctl; sessionEnumerator.GetSession(i, out ctl); uint GetProcessID = 0; String GetName = ""; float GetVolume = 0; String GetIconPath = ""; IAudioSessionControl getsession = null; getsession = ctl; if (ctl is IAudioSessionControl2) { IAudioSessionControl2 ctl2 = ((IAudioSessionControl2)ctl); ctl2.GetProcessId(out GetProcessID); ctl2.GetDisplayName(out GetName); String sIconPath; ctl2.GetIconPath(out sIconPath); ISimpleAudioVolume volcast = (ctl2 as ISimpleAudioVolume); float grabvolume; volcast.GetMasterVolume(out grabvolume); GetVolume = grabvolume; try { Process grabProcess = Process.GetProcessById((int)GetProcessID); if (String.IsNullOrEmpty(GetName)) { GetName = grabProcess.ProcessName; } } catch (Exception exx) { GetName = "Name Not Available"; } } ApplicationVolumeInformation avi = new ApplicationVolumeInformation(getsession, GetProcessID, GetVolume, GetName, GetIconPath); yield return(avi); Marshal.ReleaseComObject(ctl); } Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(deviceEnumerator); }
public static List <Application> EnumerateApplications() { // get the speakers (1st render + multimedia) device List <Application> applications = new List <Application>(); IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDevice speakers = null; deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers); // activate the session manager. we need the enumerator Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; object o; speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o); IAudioSessionManager2 mgr = (IAudioSessionManager2)o; // enumerate sessions for on this device IAudioSessionEnumerator sessionEnumerator; mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); for (int i = 0; i < count; ++i) { IAudioSessionControl2 ctl = null; sessionEnumerator.GetSession(i, out ctl); string dn; string iconPath; int pid; ctl.GetProcessId(out pid); ctl.GetDisplayName(out dn); float volumeLevel = GetApplicationVolume(dn); int roundedVolume = (int)Math.Round(volumeLevel); Console.WriteLine("Application Name: " + dn); Console.WriteLine(pid); Application application = new Application(); application.name = dn; application.volume = roundedVolume; application.pid = pid; //var proc = Process.GetProcessById(pid); //try //{ // Console.WriteLine(proc.MainModule.FileName); //} //catch (Exception e) //{ // Console.WriteLine(e.Message); //} var query = "SELECT ProcessId, Name, ExecutablePath FROM Win32_Process WHERE processid = " + pid.ToString(); using (var searcher = new ManagementObjectSearcher(query)) using (var results = searcher.Get()) { var processes = results.Cast <ManagementObject>().Select(x => new { ProcessId = (UInt32)x["ProcessId"], Name = (string)x["Name"], ExecutablePath = (string)x["ExecutablePath"] }); Console.WriteLine("Applicaiton name from search: " + processes.ElementAt(0).Name); Console.WriteLine("Applicaiton name from search: " + Process.GetProcessById((int)processes.ElementAt(0).ProcessId).MainWindowTitle); if (application.pid == 0) { application.name = "System Sounds"; } else { application.name = Process.GetProcessById((int)processes.ElementAt(0).ProcessId).MainWindowTitle; } foreach (var p in processes) { if (System.IO.File.Exists(p.ExecutablePath)) { var icon = Icon.ExtractAssociatedIcon(p.ExecutablePath); icon.ToBitmap().Save("icon.bmp"); Byte[] bytes = File.ReadAllBytes("icon.bmp"); String encodedIcon = Convert.ToBase64String(bytes); application.icon = encodedIcon; var key = p.ProcessId.ToString(); Console.WriteLine(p.ExecutablePath); } } } applications.Add(application); ctl.GetIconPath(out iconPath); Console.WriteLine("IconPath: " + iconPath); Marshal.ReleaseComObject(ctl); } return(applications); Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(deviceEnumerator); }