Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

(Ebook - Pdf) Kick Ass Delphi Programming

.pdf
Скачиваний:
286
Добавлен:
17.08.2013
Размер:
5.02 Mб
Скачать

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

Go!

Keyword

To access the contents, click the chapter and section titles.

Kick Ass Delphi Programming

(Publisher: The Coriolis Group)

Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin

ISBN: 1576100448

Publication Date: 09/01/96

Search this book:

Go!

-----------

The WalkStuf Unit

After several hours of thrashing and many false starts, I created the unit shown in Listing 13.7. This bit of code provides several general-purpose routines that make it pretty straightforward to get lists of modules and procedures from Win95.

Listing 13.7 Code for the WalkStuf unit

{———————————————————————————————————————————————————}

{

The Walking Demo

}

{

WALKSTUF.PAS : Utilities Unit

}

{

By Ace Breakpoint, N.T.P.

}

{

Assisted by Don Taylor

}

{

 

}

{ This unit contains some routines for returning

}

{ specific results from the TlHelp32 unit.

}

{

 

}

{ Written for *Kick-Ass Delphi Programming*

}

{ Copyright (c) 1996 The Coriolis Group, Inc.

}

{

Last Updated 3/28/96

}

{———————————————————————————————————————————————————}

unit WalkStuf;

interface

uses

Windows, Classes, Dialogs, SysUtils, TLHelp32;

const

ws_FullPath = True; ws_NoDirectory = False; ws_Unique = True; ws_DupesOK = False; ws_InstanceCount = True; ws_NoInstanceCount = False;

function GetSystemProcessList(FullPath : Boolean;

Unique : Boolean) : TStringList;

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)

else AList.Add(ChopPath(AProcEntry.szExeFile));

{ Add any other processes }

while Process32Next(ProcHandle, AProcEntry) do if FullPath

then AList.Add(AProcEntry.szExeFile)

else AList.Add(ChopPath(AProcEntry.szExeFile)); end;

CloseHandle(ProcHandle);

end;

{

Return a String List containing the names of all active modules in all processes in the system.

}

function GetSystemModuleList(FullPath : Boolean; Unique : Boolean;

IncludeData : Boolean) : TStringList;

var

s : String;

AList : TStringList;

ProcHandle : THandle;

ModHandle : THandle; AProcEntry : TProcessEntry32; AModEntry : TModuleEntry32; 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);

AModEntry.dwSize := sizeof(TModuleEntry32);

if Process32First(ProcHandle, AProcEntry) then begin

{ Work on the first process } 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;

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.

To access the contents, click the chapter and section titles.

Kick Ass Delphi Programming

Go!

Keyword

(Publisher: The Coriolis Group)

Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin

ISBN: 1576100448

Publication Date: 09/01/96

Search this book:

Go!

-----------

WalkStuf provides five useful functions that make exploring Win95’s operations a lot easier. GetSystemProcessList returns a string list containing the names of all active processes in the system. An option is provided to list only the process name (without its full path) and to suppress multiple instances of the same process. GetSystemModuleList returns a string list of all modules from all processes in the system. The same options are provided for suppressing path information and multiple instances; in addition, there is a provision to append the number of system-wide instances of the module to each entry’s string. GetProcessModules returns a string list of all modules for a specified process. GetLocalModuleList brings back a string list of only the modules in the current process. Finally, ModuleSysInstCount returns an integer representing the number of system-wide instances of a specified module.

There are a few items of interest in the WalkStuf routines that bear some explanation. GetSystemProcessList illustrates how to “walk” the list of processes. ProcHandle is assigned a handle to an area within KERNEL32 that is set up to contain a list of all the processes. Next, the dwSize field of the TProcessEntry32 (a record designed to handle process information) is set to the size of that data type. (Although this seems almost silly, it is critical to the operation!) Process32First is then called, using the ProcHandle to the KERNEL32 information and the variable (AProcEntry) where the data is to be dumped.

If Process32First returns True, several pieces of information about the first process in the list have been copied to the fields of AProcEntry. Perhaps the two most interesting are szExeFile and th32ProcessID. The szExeFile field contains a string which identifies the complete path to the .EXE file the process was created from. The th32ProcessID field is a unique ID value for the process being examined; it can be passed on to other functions within ToolHelp. More on that in a moment.

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.