 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Frank Soriano Guest
|
Posted: Thu Apr 20, 2006 2:04 pm Post subject: Get running executable |
|
|
Hello,
I want to know what is the current process executable. I mean the one that
has focus, which EXE file is that one. My big problem is that I want to know
that information every 10 seconds. My idea is to create a system that will
keep track of statistics in a machine of what applications are running. I
already know how to read the executable:
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;
So my question is will this cause performance problems?
Thanks in advance
Frank |
|
| Back to top |
|
 |
Jim Rowell Guest
|
Posted: Fri Apr 21, 2006 2:03 pm Post subject: Re: Get running executable |
|
|
"Frank Soriano" <franksoriano (AT) programmer (DOT) net> wrote in message
news:44478a41 (AT) newsgroups (DOT) borland.com...
| Quote: | Hello,
I want to know what is the current process executable. I mean the one that
has focus, which EXE file is that one. My big problem is that I want to
know that information every 10 seconds. My idea is to create a system that
will keep track of statistics in a machine of what applications are
running. I already know how to read the executable:
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;
So my question is will this cause performance problems?
|
I think a better group to ask this in would be nativeApi.win32.
According to MS, GetModuleFileName only works if the calling process owns
the module. To query a different process you need to use GetModuleFileNameEx
and it's only available for NT4, W2K, XP, etc.
I use the following routine to do similar to what you are trying:
function GetProcessPath(ProcessId: DWORD; var Name: string; var Path:
string): boolean;
// Gets the exec file Name and Path for the process specified in ProcessId.
// Returns true if successful.
var
ph: THandle;
FileName: array[0..MAX_PATH-1] of Char;
begin
Result := false;
ph := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
False,ProcessId);
if ph <> 0 then
try
if GetModuleFileNameEx(ph,0,FileName,SizeOf(FileName)) > 0 then
begin
Name := ExtractFileName(FileName);
Path := ExtractFilePath(FileName);
result := true;
end;
finally
CloseHandle(ph);
end;
end;
You can get the ProcessId (a cardinal) using:
GetWindowThreadProcessId(GetForeGroundWindow,@ProcessId);
One of my apps calls all of this 4 times a second along with a bunch of
other stuff and I don't notice any performance hit at all. If you need Win98
perhaps you could attach to the process first. I'm not sure if that would
work or not.
--
Jim Rowell |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|