Tutorial - Retrieving loaded modules with the Toolhelp functions

'

This article was written by Peter Johnson. Peter has written a number of articles, components and applications, unfortunately the web address we have is no longer valid, if you know where it is now please let us know.

The following is an example of using the Toolhelp functions to retrieve the modules (DLL's) that the calling process is using. The example loads a listbox called 'ModuleList' with the name of each module and its version (if version information is present).

When using in your own applications don't forget to include 'Tlhelp32' in the application's using clause.

var
  ModuleSnap: THandle;
  ModuleEntry32: TModuleEntry32;
  More: Boolean;
  VerData, Ver: Pointer;
  VerStr: String;
  VerSize, VerLength: Integer;

begin
  ModuleList.Lines.Clear;
  ModuleList.Lines.Add('Module, Version');
  try
    ModuleSnap := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,
    GetCurrentProcessID);
    if ModuleSnap = -1 then Exit;
    ModuleEntry32.dwSize := SizeOf(ModuleEntry32);
    More := Module32First(ModuleSnap, ModuleEntry32);
    while More do
    begin
      VerSize := GetFileVersionInfoSize(ModuleEntry32.szExePath + 1,
                                        VerLength) 
      try
        GetMem(VerData, VerSize);
        if GetFileVersionInfo(ModuleEntry32.szExePath + 1,
                                     0, VerSize,
                                     VerData) then
        begin
          VerQueryValue(VerData,
                        Char('\StringFileInfo\040904E4\FileVersion'
                        + #0),
                        Ver, VerLength);
          SetString(VerStr, PChar(Ver), VerLength);
          ModuleList.Lines.Add(StrPas(ModuleEntry32.szExePath + 1) 
		                       + ', ' + VerStr);
      end;
    finally
      FreeMem(VerData);
    end;
    More := Module32Next(ModuleSnap, ModuleEntry32);
  end;
  finally
    CloseHandle(ModuleSnap);
  end;
end;

Google
Web www.Delphi-Central.com
Delphi Central - Delphi Programming Tutorials, Hints and Tips