Win32FileStream Exception

May 25, 2010 at 2:17 PM
Edited May 25, 2010 at 2:29 PM

I tried my first steps with this library, but with no success. I followed the example on codeproject.

Win32FileStream file = new Win32FileStream(@"\\.\CdRom0", FileAccess.ReadWrite);

I got a:
Unhandled Exception: System.ArgumentException: Value does not fall within the expected range.
at Helper.IO.UnsafeNativeMethods.ThrowExceptionForHR(Int32 hr)
at Helper.IO.UnsafeNativeMethods.CreateFile(String fileName, Win32FileAccess access, FileShare shareMode, FileMode mode, Win32FileOptions options)
at Helper.IO.Win32FileStream..ctor(String path, FileAccess access)
at SCSITest.Program.testBurnCD(String filePath, Boolean close) in ...
at SCSITest.Program.Main(String[] args) in ...

Do you have a hint whats going wrong?

Thanks for help,
best regards.
May 25, 2010 at 6:17 PM


That's funny... it works on my computer. Try this, and replace X with your drive letter: (Don't actually call any other methods here, since if your drive letter ever changes or is wrong, you can overwrite data on your hard disk really easily. It's just for testing.)

//Win32FileStream(string path, Win32FileAccess access, FileShare share, FileMode mode, Win32FileOptions options)

Win32FileStream drive = new Win32FileStream(@\\.\X:, Win32FileAccess.ReadAttributes | Win32FileAccess.Synchronize, FileShare.ReadWrite, FileMode.Open, Win32FileOptions.None);

Let me know if this works or not, and if not, please write what exception you get again.

Thanks a lot for your feedback!

May 26, 2010 at 6:14 AM
Edited May 26, 2010 at 7:02 AM

ok, now I think I got it. The problem is not the path-parameter. The exception comes if I use the 2 parameter Win32FileAccess method. With the method above it seems to work.

Win32FileStream file = new Win32FileStream(@"\\.\CdRom0", Win32FileAccess.ReadAttributes | Win32FileAccess.Synchronize, FileShare.ReadWrite, FileMode.Open, Win32FileOptions.None);

But now I get a new exception at:


Unhandled Exception: System.Runtime.InteropServices.COMException: Unzulässige Funktion.
at Scsi.Win32Spti.ThrowExceptionForHR(Int32 hr)
at Scsi.Win32Spti.LockVolume()
at SCSITest.Program.testBurnCD(String filePath, Boolean close) in ...
at SCSITest.Program.Main(String[] args) in ...

"Unzulässige Funktion" means: forbidden function/method

Thanks for help again. ;-)

PS: Now I used your src-code and got:
System.Runtime.InteropServices.COMException (0x800703F0): Es wurde versucht, auf ein Token zuzugreifen, das nicht vorhanden ist.
at Scsi.Win32Spti.ThrowExceptionForHR(Int32 hr) in ...\Scsi\Scsi\Win32Spti.cs:line 180 at Scsi.Win32Spti.LockVolume() in ...\Scsi\Scsi\Win32Spti.cs:line 189
Means: The methode called a token which is not available.

The call is: ThrowExceptionForHR(int hr) with hr = -2147024895

Win32FileStream(@"\\.\CdRom0", FileAccess.ReadWrite, FileShare.ReadWrite, FileMode.Open, FileOptions.None)
May 26, 2010 at 2:17 PM
I did think the problem might be with the access and not the path, but I thought changing both should work. I forgot to tell you that the access mode that I used doesn't work for most functions -- it's a special read access mode that doesn't access the medium, but that has limited use. So, yes, you're right: you need ReadWrite access as you pointed out in order to solve this. I'm not sure now -- did using "\\.\CdRom0" and FileAccess.ReadWrite solve both of these problems?