No, while there are other way's to handle this, none can make use of
documented features.
But, when talking about "solutions", you first need to define the problem.
What exactly are you trying to solve here?.
Why do people want know about "who" has an open handle to a file, for other
reasons than debugging?
Say a program owns the handle to a file called a.txt, while a 2nd program
tries to open the same a.txt file. When the open fails due to a sharing
violation, you know that the file has been opened by another process, right?
Now there are three reasons for the sharing violation:
1. App1 isn't designed for sharing, whatever the sharing mode.
2. App1 is designed to share the file, but app2 isn't designed to share.
3. None of the applications are designed to share the file.
Which one of the above can be solved by knowing who has the file open, and
how are you going to solve it?
The only solution is to "correctly handle" the sharing, if this is not
possible for whatever reason, then you can't run the applications
simultaneously.
Willy.
"Michael Nemtsev" <ne*****@msn.comwrote in message
news:17***************************@msnews.microsof t.com...
| Hello Willy Denoyette [MVP],
|
| i wonder if there are any better solutions?
|
| WBeware that this code uses undocumented exports from ntdll.dll, and
| Wrequires
| Wthat the caller is a member of the "debug users" group!
| WWhich means that this is not something you would ever use in
| Wproduction
| Wcode.
| WWilly.
| W>
| W"Michael Nemtsev" <ne*****@msn.comwrote in message
| Wnews:17***************************@msnews.microso ft.com...
| W| Hello Demetri,
| W|
| W| See info there
| W|
| W
http://www.codeguru.com/Cpp/W-P/syst.../article.php/c
| W2827
| W| and there
| W
http://vbnet.mvps.org/index.html?cod...etfileenum.htm
| W|
| W| The WinAPI code for this is:
| W|
| W| void WhoUsesFile( LPCTSTR lpFileName, BOOL bFullPathCheck )
| W| {
| W| BOOL bShow = FALSE;
| W| CString name;
| W| CString processName;
| W| CString deviceFileName;
| W| CString fsFilePath;
| W| SystemProcessInformation::SYSTEM_PROCESS_INFORMATI ON* p;
| W| SystemProcessInformation pi;
| W| SystemHandleInformation hi;
| W|
| W| if ( bFullPathCheck )
| W| {
| W| if ( !SystemInfoUtils::GetDeviceFileName( lpFileName,
| WdeviceFileName ) )
| W| {
| W| _tprintf( _T("GetDeviceFileName() failed.\n") );
| W| return;
| W| }
| W| }
| W|
| W| hi.SetFilter( _T("File"), TRUE );
| W|
| W| if ( hi.m_HandleInfos.GetHeadPosition() == NULL )
| W| {
| W| _tprintf( _T("No handle information\n") );
| W| return;
| W| }
| W|
| W| pi.Refresh();
| W|
| W| _tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"),
| W_T("Path") );
| W| _tprintf(
| W_T("------------------------------------------------------\n") );
| W|
| W| for ( POSITION pos = hi.m_HandleInfos.GetHeadPosition(); pos !=
| WNULL; )
| W| {
| W| SystemHandleInformation::SYSTEM_HANDLE& h =
| Whi.m_HandleInfos.GetNext(pos);
| W|
| W| if ( pi.m_ProcessInfos.Lookup( h.ProcessID, p ) )
| W| {
| W| SystemInfoUtils::Unicode2CString( &p->usName, processName );
| W| }
| W| else
| W| processName = "";
| W|
| W| //NT4 Stupid thing if it is the services.exe and I call GetName :((
| W| if ( INtDll::dwNTMajorVersion == 4 && _tcsicmp( processName,
| W_T("services.exe"
| W| ) ) == 0 )
| W| continue;
| W|
| W| hi.GetName( (HANDLE)h.HandleNumber, name, h.ProcessID );
| W|
| W| if ( bFullPathCheck )
| W| bShow = _tcsicmp( name, deviceFileName ) == 0;
| W| else
| W| bShow = _tcsicmp( GetFileNamePosition(name), lpFileName ) == 0;
| W|
| W| if ( bShow )
| W| {
| W| if ( !bFullPathCheck )
| W| {
| W| fsFilePath = "";
| W| SystemInfoUtils::GetFsFileName( name, fsFilePath );
| W| }
| W|
| W| _tprintf( _T("0x%04X %-20s %s\n"),
| W| h.ProcessID,
| W| processName,
| W| !bFullPathCheck ? fsFilePath : lpFileName );
| W| }
| W| }
| W| }
| W|
| W|
| W| DNo, I don't want an already made utility. I would like to know
| Whow to
| W| Ddo this in c#.
| W| D>
| W| DThanks
| W| D>
| W| D"Petar Repac" wrote:
| W| D>
| W| >Demetri wrote:
| W| >>
| W| >>Anyone know how I can find out if any process has a handle on a
| W| >>specific file?
| W| >>>
| W| >Take a look at
| W
http://www.sysinternals.com/FileAndDiskUtilities.html
| W| >maybe you will find what you need there.
| W| >>
| W| >Regards,
| W| >Petar
| W| ---
| W| WBR,
| W| Michael Nemtsev :: blog:
http://spaces.live.com/laflour
| W|
| W| "At times one remains faithful to a cause only because its
| Wopponents do
| Wnot
| W| cease to be insipid." (c) Friedrich Nietzsche
| W|
| W|
| ---
| WBR,
| Michael Nemtsev :: blog:
http://spaces.live.com/laflour
|
| "At times one remains faithful to a cause only because its opponents do
not
| cease to be insipid." (c) Friedrich Nietzsche
|
|