Solved

Disconnect a user or application from file

Posted on 1998-12-17
25
225 Views
Last Modified: 2013-12-03
Hi, there!
I´m seeking for API calls to disconnect a user or app from a file (locally as well as remote).
To avoid the 'file sharing violation' message and the 'access denied, file is used by another process' message.
0
Comment
Question by:Geffl
  • 12
  • 6
  • 5
  • +2
25 Comments
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1417386
You need to be an administrator to do that.
0
 
LVL 1

Author Comment

by:Geffl
ID: 1417387
I am an administrator! I am looking for the API calls to use it in Perl-Scripts or C-Code.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1417388
There is an application called "net watch" that does exactly that.  It comes as a part of the NT resource kit.
0
 
LVL 1

Author Comment

by:Geffl
ID: 1417389
Yes, but I can't use it in scripts to automate this procedure for many files.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1417390
NetWatch sources are available as a sample in various MSVC versions or the SDK.  If you like, I can email you a copy (may take a couple of days though).  Drop me an email.
0
 
LVL 1

Author Comment

by:Geffl
ID: 1417391
Yes, but I can't use it in scripts to automate this procedure for many files.
0
 
LVL 1

Author Comment

by:Geffl
ID: 1417392
My email address is: stefan.koell@metro.at
When I get your mail, you´ll get your points!

Thanks a lot!

0
 
LVL 11

Expert Comment

by:alexo
ID: 1417393
>> When I get your mail, you´ll get your points!
Fair enough.  May take some time though.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1417394
File sent...
0
 
LVL 1

Author Comment

by:Geffl
ID: 1417395
Now you have to answer again!

0
 
LVL 1

Author Comment

by:Geffl
ID: 1417396
stop, stop, stop, alexo.
I just reviewed the code you have sent me. And unfortunately there is no code I need. Maybe I asked the wrong question or you misunderstoo the question. The only thing I found is the NetShareDel - call.

Let me describe my problem in detail: I have to synchronize a share in 10 domains. I´m to write a perl script to check for differences between the source share and the destination shares. The problem is at least the file copy, because if files are exclusively opened by any user, I can´t copy them. So you can close all sessions (and these are a lot) manually using the Filemanager.
Finally I found a tool which can do it for a single file in command line. The tool is called 'netfile' and have found it at www.ntsysadmin.com. Although I´m further interested how to do it with Win32-API. So I hope you can help me.
Thanx, Geffl
0
 
LVL 11

Expert Comment

by:alexo
ID: 1417397
>> stop, stop, stop, alexo.
What?  What?  Has the war started?  :-)

Take a look at these functions:
  NetSessionEnum() - to enumerate all sessions
  NetSessionGetInfo() - to get info about a session
  NetSessionDel() - to nuke the session

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:Geffl
ID: 1417398
With these function I can close sessions on user- or computer level. Is it possible to close all session for one file? How does this work?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1417399
You can use NetFileEnum() to enumerate all open files, find the one you need and disconnect the user from the corresponding share.  I'm not sure you can forcefuly close a single file.  There is a NetFileClose() function but it is documented as obsolete.
0
 
LVL 3

Expert Comment

by:altena
ID: 1417400
Geffl,

On the one hand, I understand what you are trying to do,
on the other hand, it seems that you are solving the wrong
problem.....

Your problem is buggy apps locking files over the network,
not your inability to unlock the files.

Fix the buggy applications. If you find a way to "liberate"
your files, you will start receiving calls by users whos applications have crashed. (and then you will have to fix the
bugs anyway)
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1417401
Geffl, I can give you for 400 pts+grade A working code to close net connection for a file - exactly what netfile.exe utility does.
It uses NetCloseFile(). There is no other way to close the file.
Of course, it works on NT only.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1417402
Note: my code will work for network-connected files (ie opened via network connection).
0
 
LVL 1

Author Comment

by:Geffl
ID: 1417403
Ok, Nick here are the points!

It seems, that you know the netfile.exe utility. Netfile.exe works on local files too. I hope so your code do?!?

Anyway, let me have a look.

0
 
LVL 15

Accepted Solution

by:
NickRepin earned 400 total points
ID: 1417404
Geffl, I've heard about netfile from you. I just check what this utility for.
About local files. Eg, on my PC there is dir C:\SHARE with a file FILE.EXT in it. If I open this file just as C:\SHARE\FILE.EXT, nothing can release this lock, include netfile.exe. But, if I open this file as , eg.,\\NICK\SHARE_NAME\FILE.TXT, or through connected network drive (eg Z:\SHARE\FILE.EXT), then we can do it.

Here is the console app written for you by me .
The first part of it does exactly what you want.
The second part enumerates all opened resources.

#define STRICT
#define UNICODE
#include <windows.h>
#include <iostream.h>
#include <stdlib.h>
#include <lmcons.h>
#include <lmshare.h>


// Usage: a.exe <server>  - enumerate opened resources
//        a.exe <server> <file> - close file by name

void main(int argc,char* argv[])
{
   if(argc>1) {
      wchar_t serverName[MAX_COMPUTERNAME_LENGTH+1];
      mbstowcs(serverName,argv[1],strlen(argv[1])+1);
      if(argc>2) {  // Close file
         wchar_t basePath[MAX_PATH+1];
         mbstowcs(basePath,argv[2],strlen(argv[2])+1);
         wchar_t* userName=0; // All users
         FILE_INFO_2* fi;
         DWORD read,total,resume=0;
         NET_API_STATUS r=NetFileEnum(LPTSTR(serverName),LPTSTR(basePath),
            LPTSTR(userName),2,(LPBYTE*)(&fi),MAX_PREFERRED_LENGTH,
            &read,&total,&resume);
         if(r) {
            cout<<"Enumeration error! "<<r<<endl;
            return;
         }
         if(read==0) {
            cout<<"File not in network use!"<<endl;
            return;
         }
         r=NetFileClose(LPTSTR(serverName),fi->fi2_id);
         cout<<(r ? "Cannot close file!" : "Ok")<<endl;
      }
      else { // Just enumerate resources
         wchar_t* basePath=0; // All open resources
         wchar_t* userName=0; // All users
         FILE_INFO_3* fi;
         DWORD read,total,resume=0;
         NET_API_STATUS r=NetFileEnum(LPTSTR(serverName),LPTSTR(basePath),
            LPTSTR(userName),3,(LPBYTE*)(&fi),MAX_PREFERRED_LENGTH,
            &read,&total,&resume);
         if(r) {
            cout<<"Enumeration error! "<<r<<endl;
            return;
         }
         if(read==0) {
            cout<<"No resources in use! "<<endl;
            return;
         }
         for(DWORD i=0;i<read;i++) {
            cout<<endl<<"Id="<<(fi[i].fi3_id)<<' '<<
               "Locks="<<(fi[i].fi3_num_locks)<<' ';
            char buf[MAX_PATH*2+2];
            wcstombs(buf,fi[i].fi3_pathname,sizeof(buf));
            cout<<buf<<' ';
            wcstombs(buf,fi[i].fi3_username,sizeof(buf));
            cout<<buf<<' '<<endl;
         }
      }
   }
}

0
 
LVL 1

Author Comment

by:Geffl
ID: 1417405
Ok, Nick here are the points!

It seems, that you know the netfile.exe utility. Netfile.exe works on local files too. I hope so your code do?!?

Anyway, let me have a look.

0
 
LVL 1

Author Comment

by:Geffl
ID: 1417406
Rock'n'Roll!
Works fine, the only thing I wonder is, that I found the NetFileClose call before in my MSDN and they say that the call is obsolete and only for WIN16 compatibility. Which function instead should be used is not described as less than a sample.
Maybe you can tell me the reason or an explanation why?

Thanx, Geffl
0
 
LVL 1

Author Comment

by:Geffl
ID: 1417407
Rock'n'Roll!
Works fine, the only thing I wonder is, that I found the NetFileClose call before in my MSDN and they say that the call is obsolete and only for WIN16 compatibility. Which function instead should be used is not described as less than a sample.
Maybe you can tell me the reason or an explanation why?

Thanx, Geffl
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1417408
1) Netfile.exe uses NetFileClose()
2) About 'obsolete' NetFileClose(). I think MS made an mistake here. This func is in 32-bit dll, and I don't know how the WIN16 will call it. The functions which are really obsolete just removed from the system dlls. Next, although NetFileClose() is 'obsolete', nevertheless it works, and MS says that it must work.
Moreover, this function is very important to the system to close files, eg, when share is stopped. This function is used by the system dlls xactsrv.dll, net1ui.dll, srvsvc.dll and others. So I think it's absolutely safe to use it.

By the way, I think the 400 pts* grade A=1600 pts, but I receive only 1200. Are there any problems with my answer, or it seems too simple for grade A? I've warned you about NetFileClose in my answer, and you can found the answer yourself if you think it's easy.
0
 
LVL 1

Author Comment

by:Geffl
ID: 1417409
I´m sorry, the wrong grade was my mistake. In my hurry, I missed the A, I think. But you helped me out of some deep shi.. and there is no reason that you get not your points:
I´ll post a question with 100 pts and you answer it - OK?


0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1417410
Thank you, apologies accepted.
It's not necessary to post a new Q. 400 pts more or less - it's  not important for me. But for  you it will cost 500 pts instead of 400. Just delete your new Q.


0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now