Page 1 of 1

Odd behavior of the GetWindowModuleFileName API function.

Posted: Thu Sep 04, 2008 3:26 pm
by Tipperton
According to the MSDN library:
MSDN wrote:GetWindowModuleFileName
The GetWindowModuleFileName function retrieves the full path and file name of the module associated with the specified window handle.

Code: Select all

UINT WINAPI GetWindowModuleFileName(
  HWND hwnd,            // handle to window
  LPTSTR lpszFileName,  // file name buffer
  UINT cchFileNameMax   // max size of file name buffer
);
Parameters
hwnd
[in] Handle to the window whose module file name will be retrieved.
lpszFileName
[out] Pointer to a buffer that receives the path and file name.
cchFileNameMax
[in] Specifies the maximum number of TCHARs that can be copied into the lpszFileName buffer.

Return Values
The return value is the total number of TCHARs copied into the buffer.
But if you run this code:

Code: Select all

Procedure.l EnumWindowsProc_(lhWnd.l, lJunk.l)
  If GetWindowModuleFileName_(lhWnd, @sFileName.s{#MAX_PATH}, #MAX_PATH)
    If GetWindowText_(lhWnd, @sWindowTitle.s{#MAX_PATH}, #MAX_PATH)
      WriteString(0, Trim(StrU(lhWnd, #Long))+#TAB$)
      WriteString(0, Trim(sFileName)+#TAB$)
      WriteStringN(0, Trim(sWindowTitle))
    EndIf
  EndIf
  ProcedureReturn #True
EndProcedure

If CreateFile(0, GetPathPart(ProgramFilename())+"WindowList.csv")
  WriteString(0, "WindowHandle"+#TAB$)
  WriteString(0, "FileName"+#TAB$)
  WriteStringN(0, "WindowTitle")
  EnumWindows_(@EnumWindowsProc_(), #Null)
  CloseFile(0)
EndIf
End
The function always returns the file name of the program that called it! :shock:

Any ideas or am I doing something wrong?

Posted: Thu Sep 04, 2008 3:41 pm
by Rings

Posted: Thu Sep 04, 2008 3:52 pm
by Tipperton
Nice! ;)

Thanks! :)

Posted: Fri Sep 05, 2008 9:35 pm
by SFSxOI
yeah, but that only applies to:

• Microsoft Platform Software Development Kit-January 2000 Edition, when used with:
Microsoft Windows 98 Standard Edition
Microsoft Windows 95
Microsoft Windows NT Server 4.0 Standard Edition
Microsoft Windows NT Workstation 4.0 Developer Edition

and affects Windows NT 4.0 and Windows 2000.

Posted: Fri Sep 05, 2008 11:26 pm
by Tipperton
Yeah, Microsoft recommends using the functions in psapi.dll if you need interprocess functionality as it was in the 9x series.

Unfortunately there appears to be no equivalent for the GetWindowModuleFileName function so I'll have to figure out another way to go from a window handle to the file the window came from.