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.
http://technet.microsoft.com/en-us/sysinternals/bb896656.aspx?wt.svl=related
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.
try this:
procedure TForm1.Button6Click(Sender : TObject);
var s: TFileStream;
begin
try
s := TFileStream.Create('C:\Win dows\syste m32\advapi 32.dll', fmOpenWrite);
ShowMessage('its NOT locked');
if Assigned(s) then
s.Free;
except
ShowMessage('its locked');
end;
end;
ziolko.
procedure TForm1.Button6Click(Sender
var s: TFileStream;
begin
try
s := TFileStream.Create('C:\Win
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\no tepad.exe' ) then
ShowMessage('Notepad is in use.');
else
ShowMessage('Notepad not in use.');
end;
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
begin
if IsFileInUse('c:\windows\no
ShowMessage('Notepad is in use.');
else
ShowMessage('Notepad not in use.');
end;
Did any of this help you?
John
John
ASKER
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,'PLO CaseDataAl readyRunni ng');
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?
Function PLOCaseDataRunning: Boolean;
Begin
Result := False;
CaseDataHandle := CreateMutex(NIL,FALSE,'PLO
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.
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.
ASKER
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?
ASKER
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,'PLO CaseDataAl readyRunni ng');
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;
Procedure CaseDataProgram(hAppHandle
Function PLOCaseDataRunning: Boolean;
Begin
Result := False;
CaseDataHandle := CreateMutex(NIL,FALSE,'PLO
If GetLastError = ERROR_ALREADY_EXISTS then
Begin
Result := True;
ShowMessage('PLO case data is already running!');
End;
End;
Procedure CaseDataProgram(hAppHandle
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thans Ziolko