Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Disconnect a user or application from file

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
Geffl
Asked:
Geffl
  • 12
  • 6
  • 5
  • +2
1 Solution
 
MirkwoodCommented:
You need to be an administrator to do that.
0
 
GefflAuthor Commented:
I am an administrator! I am looking for the API calls to use it in Perl-Scripts or C-Code.

0
 
alexoCommented:
There is an application called "net watch" that does exactly that.  It comes as a part of the NT resource kit.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
GefflAuthor Commented:
Yes, but I can't use it in scripts to automate this procedure for many files.
0
 
alexoCommented:
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
 
GefflAuthor Commented:
Yes, but I can't use it in scripts to automate this procedure for many files.
0
 
GefflAuthor Commented:
My email address is: stefan.koell@metro.at
When I get your mail, you´ll get your points!

Thanks a lot!

0
 
alexoCommented:
>> When I get your mail, you´ll get your points!
Fair enough.  May take some time though.
0
 
alexoCommented:
File sent...
0
 
GefflAuthor Commented:
Now you have to answer again!

0
 
GefflAuthor Commented:
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
 
alexoCommented:
>> 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
 
GefflAuthor Commented:
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
 
alexoCommented:
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
 
altenaCommented:
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
 
NickRepinCommented:
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
 
NickRepinCommented:
Note: my code will work for network-connected files (ie opened via network connection).
0
 
GefflAuthor Commented:
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
 
NickRepinCommented:
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
 
GefflAuthor Commented:
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
 
GefflAuthor Commented:
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
 
GefflAuthor Commented:
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
 
NickRepinCommented:
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
 
GefflAuthor Commented:
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
 
NickRepinCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 12
  • 6
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now