Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if a DLL is in use by any applications?

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
I am building an installer package which also installs some DLL's. I need to make this installer smart to where it recognizes when it has to copy/replace a DLL, it recognizes if any processes are using the DLL, returns details about each process, with the option to stop that process forcefully. So I need a function sort of like this:

Code:
procedure ListDLLProcesses(DLLFilename: String; List: TStrings);
begin
  //Find processes using the specified DLL as specified in 'DLLFilename'
  //Record each found process in 'List'
end;

Using Windows API's has never been a fun subject for me, so please keep it simple.

Thank you!


JD Solutions
 
Not exactly what you are asking for, but should give you a good shove in the right direction. This uses tlhelp32 so be sure you put that in the uses line.



Code:
procedure TForm1.Button1Click(Sender: TObject);
var
    ModLstHandle, ProcLstHandle: THandle;
    ModEntryProc: TModuleEntry32;
    ProcEntryProc: TProcessEntry32;
    CurrNode: TTreeNode;
begin
  TreeView1.Items.Clear;
  ProcLstHandle := CreateToolHelp32Snapshot(Th32CS_SNAPPROCESS, 0);
  if ProcLstHandle <> INVALID_HANDLE_VALUE then
    begin
      ProcEntryProc.dwSize := Sizeof(ProcEntryProc);
      if Process32First(proclstHandle, ProcEntryProc) then
        repeat
          CurrNode := TreeView1.Items.AddFirst(nil, ProcEntryProc.szExeFile);
          ModlstHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,
                         ProcEntryProc.th32ProcessID);
          if ModLstHandle <> INVALID_HANDLE_VALUE then
            begin
              ModEntryProc.dwSize:=SizeOf(ModEntryProc);
              if Module32First(ModlstHandle, ModEntryProc) then
                begin
                  // Module32First always brings back
                  // main process module first, so skipping first call
                  While Module32Next(modLstHandle, ModEntryProc) do
                    TreeView1.Items.AddChild(CurrNode, ModEntryProc.szModule);
                  CloseHandle(ModlstHandle);
                end;
            end;
        until Not Process32Next(proclstHandle, ProcEntryProc);
      CloseHandle(procLstHandle);
    end;
end;

Specifically ModEntryProc.GlblcntUsage seems to return what you are looking for.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top