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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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
ziolkoCommented:
>>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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ClassPublishingAuthor Commented:
Thans Ziolko
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.