// // BwgBurn - CD-R/CD-RW/DVD-R/DVD-RW burning program for Windows XP // // Copyright (C) 2006 by Jack W. Griffin (butchg@comcast.net) // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License // for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the // // Free Software Foundation, Inc., // 59 Temple Place, Suite 330, // Boston, MA 02111-1307 USA // using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Bwg.Scsi { /// /// /// public unsafe class WinDev : IDisposable { // // External functions required to interface to th // [DllImport("Kernel32.dll", SetLastError = true)] private static extern IntPtr CreateFile(string h, uint acc, uint share, IntPtr sec, uint disp, uint flash, uint temp); [DllImport("Kernel32.dll", SetLastError = true)] private static extern bool DeviceIoControl(IntPtr h, uint code, IntPtr inbuf, uint insize, IntPtr outbuf, uint outsize, ref uint returned, IntPtr Overlapped); [DllImport("Kernel32.dll", SetLastError = true)] private static extern bool CloseHandle(IntPtr h); [DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode)] private static extern uint FormatMessage(uint flags, IntPtr src, uint errcode, uint langid, IntPtr str, uint size, IntPtr vargs) ; /// /// The name of the device /// public string Name { get { CheckOpen() ; return m_name; } } /// /// Returns TRUE if the device is open, FALSE otherwise /// public bool IsOpen { get { return m_handle.ToInt32() != -1; } } private string m_name; private IntPtr m_handle; private uint m_last_error; /// /// /// public WinDev() { m_handle = new IntPtr(-1); } /// /// /// public uint LastError { get { return m_last_error; } } /// /// /// public void Dispose() { if (IsOpen) Close(); } /// /// /// public void Close() { CloseHandle(m_handle); } /// /// /// /// /// virtual public bool Open(string name) { string dname = name; // // CreateFile // name // accessmode = GenericRead | GenericWrite UInt32 acc = 0x80000000 | 0x40000000; m_handle = CreateFile(dname, acc, 0x01, (IntPtr)0, 3, 0x00000080, (uint)0); if (m_handle.ToInt32() == -1) { m_last_error = (uint)Marshal.GetLastWin32Error(); return false; } m_name = name; return true; } /// /// /// /// /// virtual public bool Open(char letter) { string dname = "\\\\.\\" + letter + ":"; return Open(dname); } /// /// /// protected void CheckOpen() { if (m_handle.ToInt32() == -1) throw new Exception("device is not open") ; } /// /// /// /// /// /// /// /// /// /// /// protected bool Control(uint code, IntPtr inbuf, uint insize, IntPtr outbuf, uint outsize, ref uint ret, IntPtr overlapped) { bool b; CheckOpen() ; b = DeviceIoControl(m_handle, code, inbuf, insize, outbuf, outsize, ref ret, overlapped); if (!b) m_last_error = (uint)Marshal.GetLastWin32Error(); return b; } /// /// Convert a WIN32 error code to an error string. /// /// the error code to convert /// the string for the error code static public string Win32ErrorToString(uint error) { IntPtr buffer = Marshal.AllocHGlobal(1024) ; uint ret = FormatMessage(0x1000, IntPtr.Zero, error, 0, buffer, 1024, IntPtr.Zero); if (ret == 0) return "cannot find win32 error string for this code (" + error.ToString() + ")"; string str = Marshal.PtrToStringUni(buffer); str = str.Trim(); Marshal.FreeHGlobal(buffer); return str; } } }