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.
ClassPublishingAsked:
Who is Participating?
 
ziolkoConnect With a Mentor Commented:
>>Do I need to use your code if I always close the handle that is returned when CreateMutex is called?

create mutex when DLL is loaded, in my code thats OnDLLLoad
close mutex handle when DLL is unloaded, so in my code that will be in OnDLLUnload

so that would looks like this:

var mutex: THandle;

procedure OnDLLLoad;
begin
  mutex := CreateMutex(NIL,FALSE,'PLOCaseDataAlreadyRunning');
end;

procedure OnDLLUnLoad;
begin
  CloseHandle(mutex);
end;

and on EXE side you can check if mutex exists with your PLOCaseDataRunning function

>>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?

nope,
DllProc := MyDLLProc;
this changes original DLL entry proc with my coustom one

ziolko.
0
 
ClassPublishingAuthor Commented:
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ziolkoCommented:
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.
0
 
JohnjcesCommented:
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;
0
 
JohnjcesCommented:
Did any of this help you?

John
0
 
ClassPublishingAuthor Commented:
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?
0
 
ziolkoCommented:
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.
0
 
ClassPublishingAuthor Commented:
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?
0
 
ClassPublishingAuthor Commented:
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;
0
 
ClassPublishingAuthor Commented:
Thans Ziolko
0
All Courses

From novice to tech pro — start learning today.