views:

234

answers:

4

Hi,

I try to get the name of executable name of all of my launched windows and my problem is that:

I use the method

UINT GetWindowModuleFileName(      
HWND hwnd,
LPTSTR lpszFileName,
UINT cchFileNameMax);

And I don't understand why it doesn't work.

Data which I have about a window are:
-HWND AND PROCESSID

The error is: e.g:

HWND: 00170628 
ProcessId: 2336        
WindowTitle: C:\test.cpp - Notepad++
GetWindowModuleFileName():  C:\test.exe

HWND: 00172138 
ProcessId: 2543        
WindowTitle: Firefox
GetWindowModuleFileName():  C:\test.exe

HWND: 00120358 
ProcessId: 2436        
WindowTitle: Mozilla Thunderbird
GetWindowModuleFileName():  C:\test.exe

Note: test.exe is the name of my executable file, but it is not the fullpath of Notepad++... and it make this for Mozilla Thunderbird too... I don't understand why

I use the function like this:

char filenameBuffer[4000];
if (GetWindowModuleFileName(hWnd, filenameBuffer, 4000) > 0)
{
    std::cout << "GetWindowModuleFileName(): " << filenameBuffer << std::endl;
}

Thank you for your response.

+1  A: 

Well according to the MSDN page for GetWindowModuleFileName you appear to be calling it correctly, and if your executable is located in the the root of C: it is returning the correct value:

The GetWindowModuleFileName function retrieves the full path and file name of the module associated with the specified window handle.

What are you expecting to get back?

ChrisF
@ChrisF: Look at my e.g I add three program which are Notepad, firefox and thunderbird... and my executable name is text.exe for those programs ???Is it the dark side's power ?
Jaguar
@Jaguar - I'm not sure what's going on. The MSDN page would imply that you can call this function for any window handle, but it does appear that it's only returning the module name for the program being run.
ChrisF
@Ninefingers: That's right, I try to get firefox.exe and as you can see on my e.g my HWND print the good WindowTitle but the GetWindowModuleFileName function doesn't do it !!!
Jaguar
@ChrisF: Thank you for your help !
Jaguar
+1  A: 

Aaah. I read the MSDN page at the bottom.

From http://support.microsoft.com/?id=228469

GetWindowModuleFileName and GetModuleFileName correctly retrieve information ab... GetWindowModuleFileName and GetModuleFileName correctly retrieve information about windows and modules in the calling process. In Windows 95 and 98, they return information about windows and modules in other processes. However, in Windows NT 4.0 and Windows 2000, since module handles are no longer shared by all processes as they were on Windows 95 and 98, these APIs do not return information about windows and modules in other processes.

To get more information on Windows 2000, use the Process Status Helper set of APIs (known as PSAPI, see Psapi.h include file), available since Windows NT 4.0. APIs such as GetModuleFileNameEx and GetModuleBaseName offer equivalent functionality.

Try using GetModuleFileNameEx instead.

Ninefingers
@Ninefingers: Thank you for your help!
Jaguar
+4  A: 

The function works for windows in the current process only.1 You have to do the following.

  1. Retrieve the window's process with GetWindowThreadProcessId.
  2. Open the process with PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights using OpenProcess.
  3. Use GetModuleFileNameEx on the process handle.

If you really want to obtain the name of the module with which the window is registered (as opposed to the process executable), you can obtain the module handle with GetWindowLongPtr with GWLP_HINSTANCE. The module handle can then be passed to the aforementioned GetModuleFileNameEx.

avakar
@avakar: Thank you it's work
Jaguar
A: 

http://support.microsoft.com/?id=228469

The executive summary is, GetWindowModuleFileName doesn't work for windows in other processes in NT based Windows.

Instead, you can use QueryFullProcessImageName once you have a handle to the process. You can get a handle to the process with OpenProcess which you can use once you have a process id. You can get the process id from the HWND by using GetWindowThreadProcessId

Logan Capaldo