473,467 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Who has it?

Anyone know how I can find out if any process has a handle on a specific file?

--
-Demetri
Oct 14 '06 #1
9 2290
Demetri wrote:
Anyone know how I can find out if any process has a handle on a specific file?
Take a look at
http://www.sysinternals.com/FileAndDiskUtilities.html
maybe you will find what you need there.

Regards,
Petar
Oct 14 '06 #2
No, I don't want an already made utility. I would like to know how to do this
in c#.

Thanks
--
-Demetri
"Petar Repac" wrote:
Demetri wrote:
Anyone know how I can find out if any process has a handle on a specific file?

Take a look at
http://www.sysinternals.com/FileAndDiskUtilities.html
maybe you will find what you need there.

Regards,
Petar
Oct 14 '06 #3
Hello Demetri,

See info there
http://www.codeguru.com/Cpp/W-P/syst...icle.php/c2827
and there http://vbnet.mvps.org/index.html?cod...etfileenum.htm

The WinAPI code for this is:

void WhoUsesFile( LPCTSTR lpFileName, BOOL bFullPathCheck )
{
BOOL bShow = FALSE;
CString name;
CString processName;
CString deviceFileName;
CString fsFilePath;
SystemProcessInformation::SYSTEM_PROCESS_INFORMATI ON* p;
SystemProcessInformation pi;
SystemHandleInformation hi;

if ( bFullPathCheck )
{
if ( !SystemInfoUtils::GetDeviceFileName( lpFileName, deviceFileName ) )
{
_tprintf( _T("GetDeviceFileName() failed.\n") );
return;
}
}

hi.SetFilter( _T("File"), TRUE );

if ( hi.m_HandleInfos.GetHeadPosition() == NULL )
{
_tprintf( _T("No handle information\n") );
return;
}

pi.Refresh();

_tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"), _T("Path") );
_tprintf( _T("------------------------------------------------------\n") );

for ( POSITION pos = hi.m_HandleInfos.GetHeadPosition(); pos != NULL; )
{
SystemHandleInformation::SYSTEM_HANDLE& h = hi.m_HandleInfos.GetNext(pos);

if ( pi.m_ProcessInfos.Lookup( h.ProcessID, p ) )
{
SystemInfoUtils::Unicode2CString( &p->usName, processName );
}
else
processName = "";

//NT4 Stupid thing if it is the services.exe and I call GetName :((
if ( INtDll::dwNTMajorVersion == 4 && _tcsicmp( processName, _T("services.exe"
) ) == 0 )
continue;

hi.GetName( (HANDLE)h.HandleNumber, name, h.ProcessID );

if ( bFullPathCheck )
bShow = _tcsicmp( name, deviceFileName ) == 0;
else
bShow = _tcsicmp( GetFileNamePosition(name), lpFileName ) == 0;

if ( bShow )
{
if ( !bFullPathCheck )
{
fsFilePath = "";
SystemInfoUtils::GetFsFileName( name, fsFilePath );
}

_tprintf( _T("0x%04X %-20s %s\n"),
h.ProcessID,
processName,
!bFullPathCheck ? fsFilePath : lpFileName );
}
}
}
DNo, I don't want an already made utility. I would like to know how to
Ddo this in c#.
D>
DThanks
D>
D"Petar Repac" wrote:
D>
>Demetri wrote:
>>Anyone know how I can find out if any process has a handle on a
specific file?
Take a look at http://www.sysinternals.com/FileAndDiskUtilities.html
maybe you will find what you need there.

Regards,
Petar
---
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
Oct 15 '06 #4
Beware that this code uses undocumented exports from ntdll.dll, and requires
that the caller is a member of the "debug users" group!
Which means that this is not something you would ever use in production
code.

Willy.
"Michael Nemtsev" <ne*****@msn.comwrote in message
news:17***************************@msnews.microsof t.com...
| Hello Demetri,
|
| See info there
| http://www.codeguru.com/Cpp/W-P/syst...icle.php/c2827
| and there http://vbnet.mvps.org/index.html?cod...etfileenum.htm
|
| The WinAPI code for this is:
|
| void WhoUsesFile( LPCTSTR lpFileName, BOOL bFullPathCheck )
| {
| BOOL bShow = FALSE;
| CString name;
| CString processName;
| CString deviceFileName;
| CString fsFilePath;
| SystemProcessInformation::SYSTEM_PROCESS_INFORMATI ON* p;
| SystemProcessInformation pi;
| SystemHandleInformation hi;
|
| if ( bFullPathCheck )
| {
| if ( !SystemInfoUtils::GetDeviceFileName( lpFileName, deviceFileName ) )
| {
| _tprintf( _T("GetDeviceFileName() failed.\n") );
| return;
| }
| }
|
| hi.SetFilter( _T("File"), TRUE );
|
| if ( hi.m_HandleInfos.GetHeadPosition() == NULL )
| {
| _tprintf( _T("No handle information\n") );
| return;
| }
|
| pi.Refresh();
|
| _tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"), _T("Path") );
| _tprintf(
_T("------------------------------------------------------\n") );
|
| for ( POSITION pos = hi.m_HandleInfos.GetHeadPosition(); pos != NULL; )
| {
| SystemHandleInformation::SYSTEM_HANDLE& h = hi.m_HandleInfos.GetNext(pos);
|
| if ( pi.m_ProcessInfos.Lookup( h.ProcessID, p ) )
| {
| SystemInfoUtils::Unicode2CString( &p->usName, processName );
| }
| else
| processName = "";
|
| //NT4 Stupid thing if it is the services.exe and I call GetName :((
| if ( INtDll::dwNTMajorVersion == 4 && _tcsicmp( processName,
_T("services.exe"
| ) ) == 0 )
| continue;
|
| hi.GetName( (HANDLE)h.HandleNumber, name, h.ProcessID );
|
| if ( bFullPathCheck )
| bShow = _tcsicmp( name, deviceFileName ) == 0;
| else
| bShow = _tcsicmp( GetFileNamePosition(name), lpFileName ) == 0;
|
| if ( bShow )
| {
| if ( !bFullPathCheck )
| {
| fsFilePath = "";
| SystemInfoUtils::GetFsFileName( name, fsFilePath );
| }
|
| _tprintf( _T("0x%04X %-20s %s\n"),
| h.ProcessID,
| processName,
| !bFullPathCheck ? fsFilePath : lpFileName );
| }
| }
| }
|
|
| DNo, I don't want an already made utility. I would like to know how to
| Ddo this in c#.
| D>
| DThanks
| D>
| D"Petar Repac" wrote:
| D>
| >Demetri wrote:
| >>
| >>Anyone know how I can find out if any process has a handle on a
| >>specific file?
| >>>
| >Take a look at http://www.sysinternals.com/FileAndDiskUtilities.html
| >maybe you will find what you need there.
| >>
| >Regards,
| >Petar
| ---
| 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
|
|
Oct 15 '06 #5
You know, it amazes me how something that seems like a fundamental thing for
an OS is so hard to come by (code wise). Of course I only want production
worthy code. Why is the code to do such a thing so remote and almost
un-findable (if that is a word).

--
-Demetri
"Willy Denoyette [MVP]" wrote:
Beware that this code uses undocumented exports from ntdll.dll, and requires
that the caller is a member of the "debug users" group!
Which means that this is not something you would ever use in production
code.

Willy.
"Michael Nemtsev" <ne*****@msn.comwrote in message
news:17***************************@msnews.microsof t.com...
| Hello Demetri,
|
| See info there
| http://www.codeguru.com/Cpp/W-P/syst...icle.php/c2827
| and there http://vbnet.mvps.org/index.html?cod...etfileenum.htm
|
| The WinAPI code for this is:
|
| void WhoUsesFile( LPCTSTR lpFileName, BOOL bFullPathCheck )
| {
| BOOL bShow = FALSE;
| CString name;
| CString processName;
| CString deviceFileName;
| CString fsFilePath;
| SystemProcessInformation::SYSTEM_PROCESS_INFORMATI ON* p;
| SystemProcessInformation pi;
| SystemHandleInformation hi;
|
| if ( bFullPathCheck )
| {
| if ( !SystemInfoUtils::GetDeviceFileName( lpFileName, deviceFileName ) )
| {
| _tprintf( _T("GetDeviceFileName() failed.\n") );
| return;
| }
| }
|
| hi.SetFilter( _T("File"), TRUE );
|
| if ( hi.m_HandleInfos.GetHeadPosition() == NULL )
| {
| _tprintf( _T("No handle information\n") );
| return;
| }
|
| pi.Refresh();
|
| _tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"), _T("Path") );
| _tprintf(
_T("------------------------------------------------------\n") );
|
| for ( POSITION pos = hi.m_HandleInfos.GetHeadPosition(); pos != NULL; )
| {
| SystemHandleInformation::SYSTEM_HANDLE& h = hi.m_HandleInfos.GetNext(pos);
|
| if ( pi.m_ProcessInfos.Lookup( h.ProcessID, p ) )
| {
| SystemInfoUtils::Unicode2CString( &p->usName, processName );
| }
| else
| processName = "";
|
| //NT4 Stupid thing if it is the services.exe and I call GetName :((
| if ( INtDll::dwNTMajorVersion == 4 && _tcsicmp( processName,
_T("services.exe"
| ) ) == 0 )
| continue;
|
| hi.GetName( (HANDLE)h.HandleNumber, name, h.ProcessID );
|
| if ( bFullPathCheck )
| bShow = _tcsicmp( name, deviceFileName ) == 0;
| else
| bShow = _tcsicmp( GetFileNamePosition(name), lpFileName ) == 0;
|
| if ( bShow )
| {
| if ( !bFullPathCheck )
| {
| fsFilePath = "";
| SystemInfoUtils::GetFsFileName( name, fsFilePath );
| }
|
| _tprintf( _T("0x%04X %-20s %s\n"),
| h.ProcessID,
| processName,
| !bFullPathCheck ? fsFilePath : lpFileName );
| }
| }
| }
|
|
| DNo, I don't want an already made utility. I would like to know how to
| Ddo this in c#.
| D>
| DThanks
| D>
| D"Petar Repac" wrote:
| D>
| >Demetri wrote:
| >>
| >>Anyone know how I can find out if any process has a handle on a
| >>specific file?
| >>>
| >Take a look at http://www.sysinternals.com/FileAndDiskUtilities.html
| >maybe you will find what you need there.
| >>
| >Regards,
| >Petar
| ---
| 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
|
|
Oct 16 '06 #6
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|
Whttp://www.codeguru.com/Cpp/W-P/syst.../article.php/c
W2827
W| and there
Whttp://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
Whttp://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
Oct 16 '06 #7
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|
| Whttp://www.codeguru.com/Cpp/W-P/syst.../article.php/c
| W2827
| W| and there
| Whttp://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
| Whttp://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
|
|
Oct 16 '06 #8
See my reply to Michael.

Willy.

"Demetri" <De*****@discussions.microsoft.comwrote in message
news:F1**********************************@microsof t.com...
| You know, it amazes me how something that seems like a fundamental thing
for
| an OS is so hard to come by (code wise). Of course I only want production
| worthy code. Why is the code to do such a thing so remote and almost
| un-findable (if that is a word).
|
| --
| -Demetri
|
|
| "Willy Denoyette [MVP]" wrote:
|
| Beware that this code uses undocumented exports from ntdll.dll, and
requires
| that the caller is a member of the "debug users" group!
| Which means that this is not something you would ever use in production
| code.
| >
| Willy.
| >
| >
| "Michael Nemtsev" <ne*****@msn.comwrote in message
| news:17***************************@msnews.microsof t.com...
| | Hello Demetri,
| |
| | See info there
| |
http://www.codeguru.com/Cpp/W-P/syst...icle.php/c2827
| | and there
http://vbnet.mvps.org/index.html?cod...etfileenum.htm
| |
| | The WinAPI code for this is:
| |
| | void WhoUsesFile( LPCTSTR lpFileName, BOOL bFullPathCheck )
| | {
| | BOOL bShow = FALSE;
| | CString name;
| | CString processName;
| | CString deviceFileName;
| | CString fsFilePath;
| | SystemProcessInformation::SYSTEM_PROCESS_INFORMATI ON* p;
| | SystemProcessInformation pi;
| | SystemHandleInformation hi;
| |
| | if ( bFullPathCheck )
| | {
| | if ( !SystemInfoUtils::GetDeviceFileName( lpFileName,
deviceFileName ) )
| | {
| | _tprintf( _T("GetDeviceFileName() failed.\n") );
| | return;
| | }
| | }
| |
| | hi.SetFilter( _T("File"), TRUE );
| |
| | if ( hi.m_HandleInfos.GetHeadPosition() == NULL )
| | {
| | _tprintf( _T("No handle information\n") );
| | return;
| | }
| |
| | pi.Refresh();
| |
| | _tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"),
_T("Path") );
| | _tprintf(
| _T("------------------------------------------------------\n") );
| |
| | for ( POSITION pos = hi.m_HandleInfos.GetHeadPosition(); pos !=
NULL; )
| | {
| | SystemHandleInformation::SYSTEM_HANDLE& h =
hi.m_HandleInfos.GetNext(pos);
| |
| | if ( pi.m_ProcessInfos.Lookup( h.ProcessID, p ) )
| | {
| | SystemInfoUtils::Unicode2CString( &p->usName, processName );
| | }
| | else
| | processName = "";
| |
| | //NT4 Stupid thing if it is the services.exe and I call GetName :((
| | if ( INtDll::dwNTMajorVersion == 4 && _tcsicmp( processName,
| _T("services.exe"
| | ) ) == 0 )
| | continue;
| |
| | hi.GetName( (HANDLE)h.HandleNumber, name, h.ProcessID );
| |
| | if ( bFullPathCheck )
| | bShow = _tcsicmp( name, deviceFileName ) == 0;
| | else
| | bShow = _tcsicmp( GetFileNamePosition(name), lpFileName ) == 0;
| |
| | if ( bShow )
| | {
| | if ( !bFullPathCheck )
| | {
| | fsFilePath = "";
| | SystemInfoUtils::GetFsFileName( name, fsFilePath );
| | }
| |
| | _tprintf( _T("0x%04X %-20s %s\n"),
| | h.ProcessID,
| | processName,
| | !bFullPathCheck ? fsFilePath : lpFileName );
| | }
| | }
| | }
| |
| |
| | DNo, I don't want an already made utility. I would like to know how
to
| | Ddo this in c#.
| | D>
| | DThanks
| | D>
| | D"Petar Repac" wrote:
| | D>
| | >Demetri wrote:
| | >>
| | >>Anyone know how I can find out if any process has a handle on a
| | >>specific file?
| | >>>
| | >Take a look at
http://www.sysinternals.com/FileAndDiskUtilities.html
| | >maybe you will find what you need there.
| | >>
| | >Regards,
| | >Petar
| | ---
| | 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
| |
| |
| >
| >
| >
Oct 16 '06 #9
Suppose a file is being written to by some other application (not yours) and
it is a large file (up to 6 or 8 gigs). Your code is watching what is being
written to but would not know that the writting is completed. The only way I
can figure out if the file is done being written is to check if something has
an open handle on it.

Got other bright ideas?

--
-Demetri
"Willy Denoyette [MVP]" wrote:
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|
| Whttp://www.codeguru.com/Cpp/W-P/syst.../article.php/c
| W2827
| W| and there
| Whttp://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
| Whttp://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
|
|
Oct 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.