Compare commits

..

3 Commits

3 changed files with 71 additions and 20 deletions

View File

@ -1,16 +1,18 @@
using FocusVolumeControl.AudioSessions; using BarRaider.SdTools;
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();
List<Process> _currentProcesses; int[] _currentProcesses;
public IAudioSession Current { get; private set; } public IAudioSession Current { get; private set; }
@ -55,21 +57,7 @@ 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)
{ {
try (results.DisplayName, results.ExecutablePath) = GetInfo(audioProcess);
{
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;
} }
@ -83,14 +71,69 @@ 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(processes)) if (_currentProcesses == null || !_currentProcesses.SequenceEqual(processIds))
{ {
Current = FindSession(processes); Current = FindSession(processes);
} }
@ -107,7 +150,7 @@ public class AudioHelper
} }
} }
_currentProcesses = processes; _currentProcesses = processIds;
return Current; return Current;
} }
} }

View File

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

View File

@ -1,6 +1,7 @@
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;
@ -59,4 +60,11 @@ 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);
} }