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)
You can create and return an object describing your result:
public class ProcessInfo
{
public string ProcessName { get; set; }
public string FileName { get; set; }
}
public ProcessInfo GetProcessInfo(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
return new ProcessInfo
{
FileName = proc.MainModule.FileName.ToString(),
ProcessName = proc.ProcessName
}
}
Or (I personally like this less), a Tuple<string, string>
:
public Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
return Tuple.Create(proc.MainModule.FileName.ToString(),
proc.ProcessName);
}
Since both are string
, how about return a Tuple<string, string>
instead?
public static Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
Tuple<string, string> t = new Tuple<string, string>
(
proc.MainModule.FileName,
proc.ProcessName
);
return t;
}
Last option would be using out-params:
public voidstring GetProcessInfo(IntPtr hwnd, out string fileName, out string processName{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
fileName = proc.MainModule.FileName.ToString();
processName = proc.ProcessName;
}
You could change the return type to Process
:
public static Process GetProcessInfo(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
return Process.GetProcessById((int)pid);
}
and then get the data you need from the returned object:
var proc = GetProcessInfo(hwnd);
var processName = proc.ProcessName;
var moduleFileName = proc.MainModule.FileName.ToString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.