Link to home
Start Free TrialLog in
Avatar of ClassPublishing
ClassPublishing

asked on

Determining whether a DLL is in use or not

I have a Delphi application which calls a Delphi DLL. However, I have some VBA (in a Word document) which also calls the same Delphi DLL and I want to determine whether the Delphi DLL is in use or not, thereby alleviating file locking problems.
Avatar of cbmm
cbmm
Flag of United States of America image

Avatar of ClassPublishing
ClassPublishing

ASKER

Thanks for quick reply. Unfortunately, I can't install third party software on every user's computers. I was hoping for a Windows API call which will let me know if a DLL is in use or not.
Avatar of Lukasz Zielinski
try this:

procedure TForm1.Button6Click(Sender: TObject);
var s: TFileStream;
begin
  try
    s := TFileStream.Create('C:\Windows\system32\advapi32.dll', fmOpenWrite);
    ShowMessage('its NOT locked');
    if Assigned(s) then
      s.Free;
  except
    ShowMessage('its locked');
  end;
end;

ziolko.
Simliar to zilko's but not using streams.... his may work better for dlls however!!

But this could help.

John

function IsFileInUse(FileName: TFileName): Boolean;
var
  HFileRes: HFILE;
begin
  Result := False;
  if not FileExists(FileName) then Exit;
  HFileRes := CreateFile(PChar(FileName),
                         GENERIC_READ or GENERIC_WRITE,
                         0,
                         nil,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         0);
  Result := (HFileRes = INVALID_HANDLE_VALUE);
  if not Result then
    CloseHandle(HFileRes);
end;
 
 
{Example:}
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsFileInUse('c:\windows\notepad.exe') then
    ShowMessage('Notepad is in use.');
  else
    ShowMessage('Notepad not in use.');
end;
Did any of this help you?

John
Thank you John and Ziolko for your help. I ended up use a createmutex command to check whether the dll is running. i.e.

Function PLOCaseDataRunning: Boolean;
Begin
  Result := False;
  CaseDataHandle := CreateMutex(NIL,FALSE,'PLOCaseDataAlreadyRunning');
  If GetLastError = ERROR_ALREADY_EXISTS then
    Begin
      Result := True;
      ShowMessage('PLO case data is already running!');
    End;
End;

I then free the casedatahandle before the DLL closes.

What do you think? It seems to work OK. I put it in the beginning of the export routine in the DLL, as well as the calling program's project source. Can you see any problems with using this? I am a little concerned with threads and whether this will always work?
yup Mutex should work, use this code to ensure that mutex is created and released with library this (wil lwork with standard library not COM)

procedure OnDLLLoad;
begin

end;

procedure OnDLLUnLoad;
begin

end;

procedure MyDLLProc(dwReason: Integer);
begin
  case dwReason of
    DLL_PROCESS_ATTACH:   OnDLLLoad;
    DLL_PROCESS_DETACH:   OnDLLUnLoad;
  end;
end;

exports
  your exports here

begin
  DllProc := MyDLLProc;
  DllProc(DLL_PROCESS_ATTACH);
end.


ziolko.
Thanks Ziolko. Do I need to use your code if I always close the handle that is returned when CreateMutex is called? Surely this will also release the mutex when the library closes?
Also, Ziolko, I see that in your code you have made MyDLLProc a procedure and yet you are expecting a return value when you call it? This is what I do to ensure I don't try to run library twice:

Procedure CaseDataProgram(hAppHandle: THandle; Var TheFileName: PChar); StdCall;

Function PLOCaseDataRunning: Boolean;
Begin
  Result := False;
  CaseDataHandle := CreateMutex(NIL,FALSE,'PLOCaseDataAlreadyRunning');
  If GetLastError = ERROR_ALREADY_EXISTS then
    Begin
      Result := True;
      ShowMessage('PLO case data is already running!');
    End;
End;

Procedure CaseDataProgram(hAppHandle: THandle; Var TheFileName: PChar);
Var
  {declared here}
Begin
  If PLOCaseDataRunning then   {Have provided code for that in conversation above}
    Begin
      Exit;
    End;
  Try
    {Do necessary stuff for this library routine}
  Finally
      If CaseDataHandle <> 0 then  {CaseDataHandle is public var}
      CloseHandle(CaseDataHandle);
  end;
End;
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thans Ziolko