Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if network shared EXE file is used by more than one user?

I have an application which sits on a server in the network shared folder. Many users in network, can use that file simultaneously. I would like to perform an update of that EXE file and to do this, I would like to know who in network (besides me) is currently using that file.

Is it possible to check this programmatically? For example: to list all user names who are using that file now? Or atleast to retrieve number of locks on that file?

Thanks.

like image 750
Wodzu Avatar asked Jul 11 '11 11:07

Wodzu


People also ask

How can I tell who is accessing my network shares?

Go into Computer Management and select System Tools >> Shared Folders >> Sessions to see who is connected.

How do you see who is using a shared file?

Manage access to a file or folder When you select an item in the Shared with list, the Manage Access panel opens: The Links Giving Access section shows the links that have permissions to the file or folder. Click the ellipsis (...) to see the users that the link has been shared with.

How can I tell who has access to a shared drive in Windows?

In File Explorer, navigate to the shared folder. Right-click the shared folder, and select Properties. In the Properties window, click Security. Within the Group or user names field, you should see everyone who has permissions relating to that folder.


2 Answers

To list the open shared files in a machine you can use ADSI (Active Directory Service Interfaces).

In order to use these interfaces from delphi you must import the Active DS type library

enter image description here

Then access the IADsFileServiceOperations interface, which contains a method called Resources this method return a collection with all the shared resources opened.

Check this sample code

{$APPTYPE CONSOLE}

uses
  ActiveDs_TLB,
  Variants,
  ActiveX,
  SysUtils;


function ADsGetObject(lpszPathName:WideString; const riid:TGUID; out ppObject):HRESULT; safecall; external 'activeds.dll';


procedure ListSharedResourcesInUse;
var
  FSO           : IADsFileServiceOperations;
  Resources     : IADsCollection;
  Resource      : OleVariant;
  pceltFetched  : Cardinal;
  oEnum         : IEnumvariant;
begin
  //establish the connection to ADSI
  ADsGetObject('WinNT://./lanmanserver', IADsFileServiceOperations, FSO);
  //get the resources interface 
  Resources := FSO.Resources;
  //get the enumerator
  oEnum:= IUnknown(Resources._NewEnum) as IEnumVariant;
  while oEnum.Next(1, Resource, pceltFetched) = 0 do
  begin
    Writeln(Format('Resource %s User %s',[Resource.Path,Resource.User]));
    Resource:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      ListSharedResourcesInUse;
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.
like image 107
RRUZ Avatar answered Sep 27 '22 20:09

RRUZ


If what you really need is to replace an exe file, that may be in use, you can rename the executable, and then just copy the new file on the same location. New users will run the new executable, where as the old (renamed) one can be deleted as soon as the last user, running it, closes the application.

like image 27
Silver Avatar answered Sep 27 '22 20:09

Silver