>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.
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
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).
--
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>...