Compare commits

..

No commits in common. "ca634f8d3cd843d3822ffbba8df6d677a874047d" and "609a7bdb655c7cabb53ad44e8371eb13bde2f630" have entirely different histories.

3 changed files with 20 additions and 71 deletions

View File

@ -1,18 +1,16 @@
using BarRaider.SdTools; using FocusVolumeControl.AudioSessions;
using FocusVolumeControl.AudioSessions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
namespace FocusVolumeControl; namespace FocusVolumeControl;
public class AudioHelper public class AudioHelper
{ {
static object _lock = new object(); static object _lock = new object();
int[] _currentProcesses; List<Process> _currentProcesses;
public IAudioSession Current { get; private set; } public IAudioSession Current { get; private set; }
@ -57,7 +55,21 @@ public class AudioHelper
//but we want all matching sessions so things like discord work right //but we want all matching sessions so things like discord work right
if (index < currentIndex) if (index < currentIndex)
{ {
(results.DisplayName, results.ExecutablePath) = GetInfo(audioProcess); try
{
var displayName = audioProcess.MainModule.FileVersionInfo.FileDescription;
if (string.IsNullOrEmpty(displayName))
{
displayName = audioProcess.ProcessName;
}
results.DisplayName = displayName;
}
catch
{
results.DisplayName = audioProcess.ProcessName;
}
results.ExecutablePath = audioProcess.MainModule.FileName;
currentIndex = index; currentIndex = index;
} }
@ -71,69 +83,14 @@ public class AudioHelper
return results.Any() ? results : null; return results.Any() ? results : null;
} }
(string name, string path) GetInfo(Process process)
{
try
{
var module = process.MainModule;
var displayName = module.FileVersionInfo.FileDescription;
if (string.IsNullOrEmpty(displayName))
{
displayName = process.ProcessName;
}
var executablePath = module.FileName;
return (displayName, executablePath);
}
catch
{
return (process.ProcessName, GetExecutablePathBackup(process));
}
}
string GetExecutablePathBackup(Process process)
{
try
{
string pathToExe = string.Empty;
if (process != null)
{
//use query limited information handle instead of process.handle to prevent permission errors
var handle = Native.OpenProcess(0x00001000, false, process.Id);
var buffer = new StringBuilder(1024);
var bufferSize = (uint)buffer.Capacity + 1;
var success = Native.QueryFullProcessImageName(handle, 0, buffer, ref bufferSize);
if (success)
{
return buffer.ToString();
}
else
{
var error = Marshal.GetLastWin32Error();
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Error = {error} getting process name");
return "";
}
}
}
catch
{
}
return "";
}
public IAudioSession GetActiveSession(FallbackBehavior fallbackBehavior) public IAudioSession GetActiveSession(FallbackBehavior fallbackBehavior)
{ {
lock (_lock) lock (_lock)
{ {
var processes = GetPossibleProcesses(); var processes = GetPossibleProcesses();
var processIds = processes.Select(x => x.Id).ToArray();
if (_currentProcesses == null || !_currentProcesses.SequenceEqual(processIds)) if (_currentProcesses == null || !_currentProcesses.SequenceEqual(processes))
{ {
Current = FindSession(processes); Current = FindSession(processes);
} }
@ -150,7 +107,7 @@ public class AudioHelper
} }
} }
_currentProcesses = processIds; _currentProcesses = processes;
return Current; return Current;
} }
} }

View File

@ -27,7 +27,7 @@ public sealed class ActiveAudioSessionWrapper : IAudioSession
} }
catch catch
{ {
_icon = "Images/encoderIcon"; _icon = "Image/encoderIcon";
} }
} }
return _icon; return _icon;

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
namespace FocusVolumeControl; namespace FocusVolumeControl;
@ -60,11 +59,4 @@ public class Native
[DllImport("ntdll.dll")] [DllImport("ntdll.dll")]
public static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessUtilities processInformation, int processInformationLength, out int returnLength); public static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessUtilities processInformation, int processInformationLength, out int returnLength);
[DllImport("Kernel32.dll")]
public static extern bool QueryFullProcessImageName(IntPtr hProcess, uint flags, StringBuilder buffer, ref uint bufferSize);
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint processAccess, bool inheritHandle, int processId);
} }