(Ebook - Pdf) Kick Ass Delphi Programming
.pdfdecided I wanted to create an application that would display the names of all active processes in the system. In addition, I wanted to show the names of every module (that is, collection of code, data, bitmaps, device drivers, etc. that make up a process) in the system. In addition, I wanted the option of listing only the modules that are associated with a specified process. Finally, I wanted to see the count of how many system-wide instances there were of every module displayed.
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
function GetSystemModuleList(FullPath : Boolean; Unique : Boolean;
IncludeData : Boolean) : TStringList;
function GetProcessModules(ProcName : String; FullPath : Boolean;
IncludeData : Boolean) : TStringList;
function GetLocalModuleList : TStringList;
function ModuleSysInstCount(ModuleName : String) : Integer;
implementation
{
Return a string with the path information removed.
}
function ChopPath(PathName : String) : String; var
s : String; begin
s := PathName; if Length(s) > 0 then begin
while Pos(':', s) > 0 do Delete(s, 1, Pos(':', s)); while Pos('\', s) > 0 do Delete(s, 1, Pos('\', s)); Result := s;
end
else Result := '';
end;
{
Return a String List containing the names of all active processes in the system.
}
function GetSystemProcessList(FullPath : Boolean;
Unique : Boolean) : TStringList;
var
AList : TStringList;
ProcHandle : THandle; AProcEntry : TProcessEntry32; begin
AList := TStringList.Create; Result := AList; AList.Sorted := True;
if Unique
then AList.Duplicates := dupIgnore else Alist.Duplicates := dupAccept;
ProcHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0); if ProcHandle = -1 then Exit;
AProcEntry.dwSize := sizeof(TProcessEntry32);
if Process32First(ProcHandle, AProcEntry) then begin
{ Add the first process } if FullPath
then AList.Add(AProcEntry.szExeFile)
while Module32Next(ModHandle, AModEntry) do begin
if IncludeData
then s := '<' + IntToStr(AModEntry.GlblcntUsage) else s := '';
if FullPath
then s := AModEntry.szExePath + s else s := AModEntry.szModule + s; AList.Add(s);
end;
CloseHandle(ModHandle);
{ Work on any remaining processes }
while Process32Next(ProcHandle, AProcEntry) do begin
ModHandle := CreateToolHelp32Snapshot(TH32CS_SNAPMODULE, AProcEntry.th32ProcessID);
if Module32First(ModHandle, AModEntry) then begin
{ Work on first module in this process } if IncludeData
then s := '<' + IntToStr(AModEntry.GlblcntUsage) else s := '';
if FullPath
then s := AModEntry.szExePath + s else s := AModEntry.szModule + s; AList.Add(s);
{ Work on remaining modules in this process } while Module32Next(ModHandle, AModEntry) do begin
if IncludeData then
s := '<' + IntToStr(AModEntry.GlblcntUsage) else s := '';
if FullPath
then s := AModEntry.szExePath + s else s := AModEntry.szModule + s; AList.Add(s);
end;
end;
CloseHandle(ModHandle); end; { while }
end;
end;
CloseHandle(ProcHandle);
end;
{
Return a String List containing the names of all active modules in the current process.
}
function GetLocalModuleList : TStringList; var
AList : TStringList;
ModHandle : THandle; AModEntry : TModuleEntry32; begin
AList := TStringList.Create; AList.Sorted := True; Result := AList;
ModHandle := CreateToolHelp32Snapshot(TH32CS_SNAPMODULE, 0); if ModHandle = -1 then Exit;
AModEntry.dwSize := sizeof(TModuleEntry32);
if Module32First(ModHandle, AModEntry) then begin
{Add the first module } AList.Add(AModEntry.szModule);
{Add any remaining modules }
while Module32Next(ModHandle, AModEntry) do AList.Add(AModEntry.szModule);
end;
CloseHandle(ModHandle);
end;
{
Return a String List containing the names of all active modules in the process specified by name.
}
function GetProcessModules(ProcName : String; FullPath : Boolean;
IncludeData : Boolean) : TStringList;
var
s : String; Found : Boolean; Done : Boolean;
AList : TStringList;
ProcHandle : THandle;
ModHandle : THandle; AProcEntry : TProcessEntry32; AModEntry : TModuleEntry32;
begin
AList := TStringList.Create; Result := AList; AList.Sorted := True;
ProcHandle := CreateToolHelp32Snapshot(TH32CS_SNAPALL, 0); if ProcHandle = -1 then Exit;
AProcEntry.dwSize := sizeof(TProcessEntry32);
AModEntry.dwSize := sizeof(TModuleEntry32);
Done := False;
if Process32First(ProcHandle, AProcEntry) then begin
{ Examine processes until a match is found }
Found := UpperCase(AProcEntry.szExeFile) = UpperCase(ProcName);
if not Found then repeat
Done := not Process32Next(ProcHandle, AProcEntry); if not Done
then Found :=
UpperCase(AProcEntry.szExeFile) = UpperCase(ProcName); until Done or Found;
if Found then begin
ModHandle := CreateToolHelp32Snapshot(TH32CS_SNAPMODULE, AProcEntry.th32ProcessID);
if Module32First(ModHandle, AModEntry) then begin
{ Work on the first module in the first process } if IncludeData
then s := '<' + IntToStr(AModEntry.GlblcntUsage) else s := '';
if FullPath
then s := AModEntry.szExePath + s else s := AModEntry.szModule + s; AList.Add(s);
{ Work on any remaining module in first process } while Module32Next(ModHandle, AModEntry) do
begin
if IncludeData
then s := '<' + IntToStr(AModEntry.GlblcntUsage) else s := '';
if FullPath
then s := AModEntry.szExePath + s else s := AModEntry.szModule + s; AList.Add(s);
end;
end;
CloseHandle(ModHandle);
end;
end;
CloseHandle(ProcHandle);
end;
{
Return a count of the total number of times the specified module appears in all processes in the system.
}
function ModuleSysInstCount(ModuleName : String) : Integer; var
Idx : Integer;
p : Integer;
s : String;
ModList : TStringList;
MatchFound : Boolean; begin
Result := -1;
ModList := GetSystemModuleList
(ws_NoDirectory, ws_DupesOK, ws_InstanceCount);
if ModList = nil then Exit;
Idx := 0; MatchFound := False;
while (Idx < ModList.Count) and not MatchFound do begin
s := ModList.Strings[Idx]; p := pos('<', s);
MatchFound := Uppercase(copy(s, 1, p - 1)) = Uppercase(ModuleName); if not MatchFound then Inc(Idx);
end; { while } if MatchFound
then Result := StrToInt(copy(s, p + 1, Length(s) - p)) else Result := 0;
end;
end.
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
After the szExeFile is added to the string list, a while loop is used to repeatedly call Process32Next. This routine takes the same parameters, and if it returns True, another process’s data will be placed in AProcEntry. (If you’ve ever used FindFirst and FindNext under DOS, you already know this drill.) When all is finished, there is one last housekeeping chore to perform. The call to CreateToolHelp32Snapshot created a Win95 object that must be disposed. This is done with a call to CloseHandle.
GetSystemModuleList is a more extensive example of walking. For each process, the complete list of modules for that process is examined with Module32First and Module32Next. For each process, a handle is obtained using CreateToolHelp32Snapshot. This time, the unique process ID of the process currently being examined (AProcEntry.th32ProcessID) is used in the call, resulting in a handle referring to module information for that process only. Note the use of the mask TH32CS_SNAPMODULE, which limits the data retreived to that for modules.
TModuleEntry32 records contain several fields. For these purposes, the three most interesting are szExePath, a string containing the complete path to the module; szModule, a string containing the base name of the module; and GlblcntUsage, a double word field containing the number of system-wide instances of this module.
Note once again that the size of the TModuleEntry32 record must be entered in AModEntry’s dwSize field, and that every call to
CreateToolHelp32Snapshot must have a matching CloseHandle to dispose of the object created.
The remaining routines are, for the most part, variations on a theme. GetLocalModuleList walks the list of only the modules belonging to the current process, obtained by using a zero as the process ID number. GetProcessModules walks through the module list, seeking a match for the specified process. If found, it walks through the modules for that process. And
ModuleSysInstCount uses a customized call to GetSystemModuleList to get a system-wide list of modules, from which it searches for a match for the specified module. From the string entry for the matching module, it converts the number of instances and returns that number as an integer.
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.