Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I Get File Name of a Foreground Window?

97 views
Skip to first unread message

liangxindong

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
hi, anyone:
I'm using GetModuleFileName, I can't get it working.
Following Coding:

>var WinInstance:Thandle;
> WinModuleName:string;
>begin
> SetLength(WinModuleName,251);
> WinInstance:=GetWindowLong(GetForegroundWindow(),GWL_hInstance);
> GetModuleFileName(WinInstance
,PChar(WinModuleName),Length(WinModuleName));
> WinModuleName:=PChar(WinModuleName);
>end;
Can anyone help me .
Thanks.

Jeremy Collins

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to

liangxindong wrote in message <364BE1E5...@ns.cetin.net.cn>...


This doesn't work (it returns the .exe name of
your application, right?), because the hInstance
variable is only unique in the address space of
your application (probably zero). You're getting
an hInstance from *another* address space, which
is probably also zero.

I don't want to go over this here, you wouldn't
believe how hard it is to get the .exe name
of a program given it's window handle (AFAIK).

Go to www.dejanews.com and search for a
thread involving mostly me and a guy called
Felipe Rocha Machado (I think the subject was
"How does WinSight get the executable name?").
This was in comp.lang.pascal.delphi.misc.

--
Jeremy Collins


Ralph Friedman (TeamB)

unread,
Nov 13, 1998, 3:00:00 AM11/13/98
to
liangxindong:

Your code works with the following change:

var
lng: longint;


WinInstance: Thandle;
WinModuleName: string;
begin
SetLength(WinModuleName,251);
WinInstance := GetWindowLong(GetForegroundWindow(), GWL_hInstance);

lng := GetModuleFileName(WinInstance,
PChar(WinModuleName),
Length(WinModuleName));
SetLength(WinModuleName, lng);
end;
--
Regards
Ralph (TeamB)
(No private e-mail replies, please, unless explicitly requested).
--

liangxindong

unread,
Nov 14, 1998, 3:00:00 AM11/14/98
to
Thanks Ralph.
But your code doesn't work.
It only retrun current activewindow File Name.
Help me.
Thanx.

Ralph Friedman (TeamB)

unread,
Nov 14, 1998, 3:00:00 AM11/14/98
to
The current active window _is_ the foreground window. What is it that you are
looking for?

--
Regards
Ralph (TeamB)
(No private e-mail replies, please, unless explicitly requested).
--
liangxindong wrote in message <364D088B...@ns.cetin.net.cn>...

liangxindong

unread,
Nov 15, 1998, 3:00:00 AM11/15/98
to
The foreground window isn't the current active window.
(I use D3 in win98)

Ralph Friedman (TeamB)

unread,
Nov 15, 1998, 3:00:00 AM11/15/98
to
liangxindong;

The following is a quote from a earlier post by Peter Below. I have no
experience with the technique and, unfortunately, don't have the time right
now to try it.

"...using GetModuleFilename only works on Win32 if the window
is part of your own process. You need to use Toolhelp32 functions (on Win95
only) or PSAPI.DLL (NT 4) to get from a process ID (which you obtain from the
window handle via GetWindowthreadProcessID) to the filename of the EXE. Delphi
encapsulates Toolhelp32 in the TlHelp32 unit."


--
Regards
Ralph (TeamB)
(No private e-mail replies, please, unless explicitly requested).
--

liangxindong wrote in message <364E62D5...@ns.cetin.net.cn>...

0 new messages