Thanks for the reply.
My application has been marked to run explicitly as a 32bit app (I've
compiled it with the x86 flag).
I need to get the exe name of the application that currently has the
focus which may not be my application but another app (that maybe is a
native 64 bit application).
Here's the code I use that I'm having problems with (it returns the
name of the exe which is the owner of the window handle but on Windows
7 64bit I got strange characters instead of the filename).
Here's the code with the two external declarations. Is there anything
wrong here?
public static string GetProcessPathFromWindowHandle(IntPtr hWnd) {
string filename = string.Empty;
uint pid=0;
Unmanaged.GetWindowThreadProcessId(hWnd, out pid);
//error in Win64: returns strange characters for Win64
files
const int nChars = 1024;
StringBuilder filenameBuffer = new StringBuilder(nChars);
IntPtr hProcess = Unmanaged.OpenProcess(1040, 0, pid);
Unmanaged.GetModuleFileNameEx(hProcess, IntPtr.Zero,
filenameBuffer, nChars);
Unmanaged.CloseHandle(hProcess);
filename = filenameBuffer.ToString();
return filename;
}
[DllImport("psapi.dll")]
public static extern uint GetModuleFileNameEx(IntPtr hProcess,
IntPtr hModule, [Out] StringBuilder lpBaseName, [In]
[MarshalAs(UnmanagedType.U4)] int nSize);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr
hWnd, out uint lpdwProcessId);
What's wrong here? How shall I modify those declarations so that I
make it work on Windows 7 64bit?
Thanks a lot.
Andrea