Fix an issue where it can't get the process name or executable path because of a permission issue

This commit is contained in:
dlprows 2023-09-24 23:42:58 -06:00
parent bbb0e55ed6
commit ca634f8d3c
2 changed files with 66 additions and 16 deletions

View File

@ -1,9 +1,11 @@
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;
@ -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,6 +71,60 @@ 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)
{ {

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);
} }