Archive: Determine if a file is in use


Determine if a file is in use
What would be the best way to determine if a file (.dll) is in use (by the shell) or not?

Will FileOpen (W) always fail if the file is in use? Is that the quickest way?


thanks

o_cee


As far as I know every shell DLL (COM as a matter of fact) has an exported function called DllCanUnloadNow. Use an external DLL to call it and see if it can be unloaded, which means it can be deleted (I think).

http://msdn.microsoft.com/library/de...f_d2l_531z.asp


Seems very useful. I'll have to ask a friend to make a suitable dll for me since i don't code myself :)

Thanks


o_cee


Could the new system.dll plugin be used for this? If yes, how?


thanks

o_cee


;)
Yes, but I surely should develop a new documentation :)

Ok. At first, you should use the latest CVS snapshot or snapshoted release (from sept.22), because the order of arguments changed in this release.

There are two pathways: first does the direct check, while the second allows you to check if the dll available and contains the DllCanUnloadNow.

1) System::FullCall "shell32.dll?DllCanUnloadNow?i" "?0"
; Now you can do the check, for example with StrCmp

shell32.dll should be replaced with your dll (it can include path).
"?0" should be replaced with desired output register ('?s' for stack, '?n' for $n, '?N', where N='a'..o for $Rn).
After that you can check it with StrCmp, for example
StrCmp $0 0 Can Cant
There is no error, S_OK==0 and S_CANCEL==1, so 0 return value means YES, you can :)

2) System::FullAddr "shell32.dll?DllCanUnloadNow?i" "?1"
Pop $0
StrCmp $0 0 0 DllFound
MessageBox MB_OK "Dll not found or it contains no DllCanUnloadNow"
Quit ; Or whatever you want
DllFound:
System::Call $0
; Now you can do the check, for example with StrCmp
; And at last, if you are not going to confuse Windows
System::Free $0

All comments for previous section are the same... And be carefull, this code uses "?1" so you should check $1, not $0.