Compare commits
3 Commits
609a7bdb65
...
ca634f8d3c
Author | SHA1 | Date | |
---|---|---|---|
ca634f8d3c | |||
bbb0e55ed6 | |||
520659ac52 |
@ -1,16 +1,18 @@
|
||||
using FocusVolumeControl.AudioSessions;
|
||||
using BarRaider.SdTools;
|
||||
using FocusVolumeControl.AudioSessions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace FocusVolumeControl;
|
||||
|
||||
public class AudioHelper
|
||||
{
|
||||
static object _lock = new object();
|
||||
List<Process> _currentProcesses;
|
||||
int[] _currentProcesses;
|
||||
|
||||
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
|
||||
if (index < currentIndex)
|
||||
{
|
||||
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;
|
||||
(results.DisplayName, results.ExecutablePath) = GetInfo(audioProcess);
|
||||
|
||||
currentIndex = index;
|
||||
}
|
||||
@ -83,14 +71,69 @@ public class AudioHelper
|
||||
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)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
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);
|
||||
}
|
||||
@ -107,7 +150,7 @@ public class AudioHelper
|
||||
}
|
||||
}
|
||||
|
||||
_currentProcesses = processes;
|
||||
_currentProcesses = processIds;
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public sealed class ActiveAudioSessionWrapper : IAudioSession
|
||||
}
|
||||
catch
|
||||
{
|
||||
_icon = "Image/encoderIcon";
|
||||
_icon = "Images/encoderIcon";
|
||||
}
|
||||
}
|
||||
return _icon;
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace FocusVolumeControl;
|
||||
|
||||
@ -59,4 +60,11 @@ public class Native
|
||||
[DllImport("ntdll.dll")]
|
||||
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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user