This method returns only the process filename:
public static string GetProcessInfo(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
return proc.MainModule.FileName.ToString();
}
But I want to return also the process name:
proc.ProcessName;
I believe you have four options (in preference order)
proc.MainModule directly and extract necessary information from caller.public static ProcessModule GetProcessInfo(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
return proc.MainModule;
}
public class ProcessInformation
{
public string FileName;
public string ProcessName;
}
public static ProcessInformation GetProcessInfo(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
var pi = new ProcessInformation
{
proc.MainModule.FileName,
proc.MainModule.ProcessName
}
return pi;
}
Tuple<string, string>
public static Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
return return Tuple.Create(proc.MainModule.FileName,proc.MainModule.ProcessName);
}
out parameters on your method (I never seen two out parameters implemented and I discourage this since definitively smells, but it's an option C# provides) string GetProcessInfo(IntPtr hwnd, out fileName, out processName)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With