Odd behavior of the GetWindowModuleFileName API function.

Windows specific forum
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Odd behavior of the GetWindowModuleFileName API function.

Post 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?
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

SPAMINATOR NR.1
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post by Tipperton »

Nice! ;)

Thanks! :)
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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.
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post 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.
Post Reply