1

In order to get the application name of the foreground Window (or the name of application file) I want to use GetActiveWindow with GetWindowModuleFileName.

I found a similar question relating to GetWindowText here

That implementation of GetWindowText works fine, but GetWindowModuleFileName only returns a value for visual studio (when I click inside the devenv) for all other applications it stays blank.

Any hint how I can find out what goes wrong? Might this have to do with permission/security of my application querying the applicationfilename of another process?

EDIT: http://support.microsoft.com/?id=228469 looks like this doesn't work under Win >=XP

Any alternatives how to get the application file name?

Community
  • 1
  • 1
Cilvic
  • 3,417
  • 2
  • 33
  • 57
  • 1
    http://support.microsoft.com/?id=228469 also recommends using GetModuleFileNameEx instead; use GetActiveWindow + GetWindowThreadProcessId + OpenProcess + GetModuleFileNameEx. (Note that OpenProcess will fail if you try to open a process that has a higher privilege level or is running as a different user; eg. this code running as normal user can't OpenProcess a process that's running as Admin.) – BrendanMcK Apr 01 '11 at 00:15
  • @Brendan Can't you open a higher privileged process if you specify a small requested permission set? I'd guess that GetModuleFilenameEx fails with such a permission set though. – CodesInChaos Apr 01 '11 at 10:42

3 Answers3

1

In order to get the application name of the foreground Window (or the name of application file) I want to use GetActiveWindow with GetWindowModuleFileName.

... querying the applicationfilename of another process ...

In my opinion your problem with use of GetActiveWindow() function. It is used for gathering information from the calling thread/process only. If calling thread is inactive GetActiveWindow return 0;

From MSDN:

GetActiveWindow Retrieves the window handle to the active window attached to the calling thread's message queue.

Try to use GetForegroundWindow() function instead of GetActiveWindow()

Community
  • 1
  • 1
Andrew D.
  • 8,130
  • 3
  • 21
  • 23
0

By chance do you have UAC turned off?

Starting with Vista, if your code touches an HWND in another process, your process needs to be run at the same privilege level.

In other words, if the window is hosted in a process running as administrator, your app must also run as administrator.

selbie
  • 100,020
  • 15
  • 103
  • 173
0

I found a workaround using this:

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

IntPtr handle = IntPtr.Zero;
handle = GetForegroundWindow();

uint processId;
if (GetWindowThreadProcessId(handle, out processId) > 0)
{
    Console.WriteLine(Process.GetProcessById((int)processId).MainModule.FileName);
}
Cilvic
  • 3,417
  • 2
  • 33
  • 57
  • This approach doesn't work when trying to get information from a 64 bits process from a 32 bits process (or vice versa) – Geoffrey Nov 27 '13 at 08:26