Esempio n. 1
0
        public static string GetIconpath(string name)
        {
            IAudioSessionControl2 ctl2 = GetVolumeObject(name) as IAudioSessionControl2;
            string path;

            ctl2.GetIconPath(out path);
            return(path);
        }
        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);
        }
Esempio n. 3
0
        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);
        }