Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Component Services / April 2004

Tip: Looking for answers? Try searching our database.

GetDirectories file handle interference

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul - 21 Apr 2004 20:44 GMT
I have a web service that recursively traverses a folder, collecting
information about the subdirectories and files in it.  One of the things I
do is store information in the hidden streams in the files.  To get at the
hidden streams I have to define CreateFile from kernel32.dll, and open the
stream up.  But when 2 processes access this web service at the same time,
one instance's calls to CreateFile are disrupting the other instance's calls
to DirectoryInfo.GetDirectories() and GetFiles(), which randomly throw an
exception of "System.IO.IOException" with an message of "The handle is
invalid".

I am hoping that I just have some of the parameters wrong in my call to
CreateFile.  But my fear is that the static CreateFile function is returning
the same file handle number to two different processes, and one is closing
the handle of the other.

Any help would be greatly appreciated.

Here is my CreateFile() definition and calling function:

public class VersionInformation
{
[DllImport("kernel32.dll")]
static extern IntPtr CreateFile(
   string lpFileName,
   uint dwDesiredAccess,
   uint dwShareMode,
   IntPtr lpSecurityAttributes,
   uint dwCreationDisposition,
   uint dwFlagsAndAttributes,
   IntPtr hTemplateFile);

private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint CREATE_ALWAYS = 2;
private const uint OPEN_EXISTING = 3;
private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
private static IntPtr INVALID_HANDLE = new IntPtr(-1);
private const uint ERROR_SHARING_VIOLATION = 32;
private const uint ERROR_ACCESS_DENIED = 5;
[DllImport("kernel32.dll")]
static extern IntPtr CloseHandle(IntPtr hHandle);
[DllImport("kernel32.dll")]
static extern uint GetLastError();

public SPVersion GetVersionInformation(string i_fileName)
{
   SPVersion a_returnVersion = null;
   IntPtr a_fileHandle = CreateFile(i_fileName + ":XEDMS.VERSION",
       GENERIC_READ,
       FILE_SHARE_READ,
       IntPtr.Zero,
       OPEN_EXISTING,
       0,
       IntPtr.Zero);
   uint a_error = GetLastError();
   if (a_fileHandle != INVALID_HANDLE)
   {
       FileStream a_reader = new FileStream(a_fileHandle, FileAccess.Read);
       try
       {
           XmlSerializer a_serializer = new
XmlSerializer(typeof(SPVersion));
           a_returnVersion = (SPVersion)a_serializer.Deserialize(a_reader);
       }
       catch {}
       a_reader.Close();
       CloseHandle(a_fileHandle);
   }
   return a_returnVersion;
}
Paul - 22 Apr 2004 05:09 GMT
Well, it's getting more interesting.  I rewrote the DirectoryInfo class
myself, by calling the FindFirstFileEx/FindNextFile routines directly, and
guess what?  Every now and then, when two calls to the web service run
concurrently, I get an ERROR_INVALID_HANDLE back from GetLastError() after a
call to FindFirstFile.  But only rarely.

It looks like there is a problem marshalling data between C# and Win32,
where the CreateFile function manages to screw up FindFirstFileEx when they
are called at the same time in the same service.

Please, anyone have any ideas?

Thanks,

-Paul

> I have a web service that recursively traverses a folder, collecting
> information about the subdirectories and files in it.  One of the things I
[quoted text clipped - 67 lines]
>     return a_returnVersion;
> }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.