Skip to content

Commit

Permalink
Merge pull request #47 from horsenit/master
Browse files Browse the repository at this point in the history
Different method for retrieving process path.
  • Loading branch information
juvlarN authored Jul 30, 2017
2 parents 2c25f7e + 269061a commit a527f0d
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions vibrance.GUI/common/ProcessExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using vibrance.GUI.AMD;
Expand Down Expand Up @@ -61,6 +62,8 @@ private void GetAllProcesses()
}
}

[DllImport("psapi.dll")]
static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize);
/// <summary>
/// Safely gets the executable path from the specified process.
/// Process.MainModule.FileName crashes when called on a x64 process because vibranceGUI is running as x86 process.
Expand All @@ -69,19 +72,11 @@ private void GetAllProcesses()
/// <returns>the fully qualified executable path</returns>
private string GetPathFromProcessId(Process process)
{
var query = "SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id;

using (var searcher = new ManagementObjectSearcher(query))
foreach (ManagementObject item in searcher.Get())
{
object id = item["ProcessID"];
object path = item["ExecutablePath"];

if (path != null && id.ToString() == process.Id.ToString())
{
return path.ToString();
}
}
var sb = new StringBuilder(1024);
if (GetModuleFileNameEx(process.Handle, IntPtr.Zero, sb, sb.Capacity) > 0)
{
return sb.ToString();
}
return string.Empty;
}

Expand Down

0 comments on commit a527f0d

Please sign in to comment.