I got the following code to get path/filename by handle:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out uint processId);
public static string GetProcessPath(IntPtr hwnd)
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
return proc.MainModule.FileName.ToString();
}
it works perfect in 32 bit but I get error in 64bit > “Only part of the ReadProcessMemory or WriteProcessMemory request was completed.”
the project is compiled as x86 (platform target x86).
How can I fix it?
~Thanks Ron
It appears from your question that you’ve currently compiled your program as a 32-bit application. However, the process you’re trying to query (assuming you’re running on a 64-bit version of Windows) is undoubtedly a 64-bit one. That kind of thing isn’t allowed. Although you can run 32-bit applications on 64-bit versions of Windows, they run under the dedicated Windows on Windows (WOW64) subsystem. That’s why you’re getting a
Win32Exceptionclaiming that “Only part of a ReadProcessMemory or WriteProcessMemory request was completed”. I agree it’s not the most descriptive of error messages if you don’t already know how Windows manages 32-bit and 64-bit processes, but armed with this knowledge it makes at least a little more sense.The solution is to compile your application as a 64-bit application (x64), or for “Any CPU”. Everything should work as expected after that. If possible, I suggest using “Any CPU”, which will allow the application to run in 32-bit mode on 32-bit OSes and 64-bit on 64-bit OSes. Which is really the ideal set of circumstances, assuming that:
IntPtrwhere appropriate, instead ofInteger).