It Gives "Invalid Handle" - Tanveer
----- Original Message ----- From: "Richard Blewett" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, August 30, 2003 3:57 PM Subject: Re: [ADVANCED-DOTNET] [inbox] [ADVANCED-DOTNET] Interop with DeviceIoControl > What does Marshal.GetLastWin32Error give you on failure? > > Regards > > Richard Blewett > DevelopMentor > > -----Original Message----- > From: Moderated discussion of advanced .NET topics. > [mailto:[EMAIL PROTECTED] On Behalf Of Tanveer > Sent: 30 August 2003 10:24 > To: [EMAIL PROTECTED] > Subject: [inbox] [ADVANCED-DOTNET] Interop with DeviceIoControl > > Hi All, > > This is the code to retrieve Firmware Serial number of a SCSI Disk. I have a > working code in VC++ which I wanted to port to C# or VB.NET. > > I have worked-up the byte array required to retrieve the code. This byte > array now needs to be Marshalled to DeviceIoControl as (ref) so that it also > fetches the required serial number in it. > > This code always returns the failure (!success). The Full Code is: > > >>>>>>>>>>>>>>>>>>>>>>>>>> > namespace TCSharp > { > using System; > using System.Text; > using System.Runtime.InteropServices; > using System.ComponentModel; > class Class1 > { > public const long GENERIC_READ = 0x80000000L; > public const long GENERIC_WRITE = 0x40000000L; > public const long FILE_SHARE_READ = 0x00000001; > public const long FILE_SHARE_WRITE = 0x00000002; > public const long OPEN_EXISTING = 3; > //char *ConvertToString (uint[] diskdata, int firstIndex, int lastIndex); > [DllImport("kernel32.dll")] > public static extern long CreateFile (string lpFileName, long > dwDesiredAccess, long dwShareMode, > long lpSecurityAttributes, long dwCreationDisposition, long > dwFlagsAndAttributes, long hTemplateFile); > [DllImport("kernel32.dll")] > public static extern long CloseHandle (long hObject ); > [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true )] > public static extern bool DeviceIoControl ( > long hDevice, > int dwIoControlCode, > System.IntPtr lpInBuffer, > int nInBufferSize, > [Out] out System.IntPtr lpOutBuffer, > int nOutBufferSize, > [Out] out byte lpBytesReturned, > object lpOverlapped); > public static long SCSI() > { > ASCIIEncoding e = new ASCIIEncoding() ; > long hFile = 0; > bool success = false; > hFile = CreateFile ("\\\\.\\Scsi0:", > GENERIC_READ | GENERIC_WRITE, > FILE_SHARE_READ | FILE_SHARE_WRITE, 0, > OPEN_EXISTING, 0, 0); > Console.WriteLine ("File Pointer ...{0}",hFile); > if (hFile != -1) > { > byte [] buffer =new byte [560]; > int x=0; > for (x=0;x<560;x++) > { > buffer[x]=(byte) 0; > } > x=0; > buffer[x++]=(byte) 28; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 83; //S > buffer[x++]=(byte) 67; //C > buffer[x++]=(byte) 83; //S > buffer[x++]=(byte) 73; //I > buffer[x++]=(byte) 68; //D > buffer[x++]=(byte) 73; //I > buffer[x++]=(byte) 83; //S > buffer[x++]=(byte) 75; //K > //Timeout > buffer[x++]=(byte) 16; > buffer[x++]=(byte) 39; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > //Control Code > buffer[x++]=(byte) 1; > buffer[x++]=(byte) 5; > buffer[x++]=(byte) 27; > buffer[x++]=(byte) 0; > // > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > // > buffer[x++]=(byte) 20; > buffer[x++]=(byte) 2; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > // > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > // > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > // > buffer[x++]=(byte) 0; > buffer[x++]=(byte) 0; > unchecked { buffer[x++]=(byte) 0xEC; } > buffer[x++]=(byte) 0; > byte dummy = (byte) 0; > System.IntPtr sb = Marshal.AllocHGlobal (560); > Marshal.Copy(buffer,0,sb,560); > // Console.WriteLine ("Input: {0}", Marshal.PtrToStringAnsi (sb,560)); > // Console.ReadLine(); > > object ovl = null; > success = DeviceIoControl (hFile, (int) 315400, > sb, (int) 63, > out sb, (int) 560, > out dummy, ovl ); > if (success) > { > Console.WriteLine ("Returned sb: {0}", Marshal.PtrToStringUni (sb,560)); > Console.ReadLine(); > } > else > { > Console.WriteLine ("Failed Dummy: {0}", dummy ) ; > Console.WriteLine ("Input: {0}", Marshal.PtrToStringAnsi (sb,560)); > Console.ReadLine(); > } > Marshal.FreeHGlobal (sb); > } > > return CloseHandle (hFile); > } > [STAThread] > static void Main(string[] args) > { > long x = TCSharp.Class1.SCSI(); > Console.WriteLine ("Exit Code ...{0}",x); > Console.ReadLine(); > } > } > } > >>>>>>>>>>>>>>>>>>>>>>>> > > Any help is appreciated. > > -Tanveer > > =================================== > This list is hosted by DevelopMentor® http://www.develop.com > NEW! ASP.NET courses you may be interested in: > > 2 Days of ASP.NET, 29 Sept 2003, in Redmond > http://www.develop.com/courses/2daspdotnet > > Guerrilla ASP.NET, 13 Oct 2003, in Boston > http://www.develop.com/courses/gaspdotnet > > View archives and manage your subscription(s) at http://discuss.develop.com > > > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.512 / Virus Database: 309 - Release Date: 19/08/2003 > > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.512 / Virus Database: 309 - Release Date: 19/08/2003 > > > =================================== > This list is hosted by DevelopMentor® http://www.develop.com > NEW! ASP.NET courses you may be interested in: > > 2 Days of ASP.NET, 29 Sept 2003, in Redmond > http://www.develop.com/courses/2daspdotnet > > Guerrilla ASP.NET, 13 Oct 2003, in Boston > http://www.develop.com/courses/gaspdotnet > > View archives and manage your subscription(s) at http://discuss.develop.com > =================================== This list is hosted by DevelopMentor® http://www.develop.com NEW! ASP.NET courses you may be interested in: 2 Days of ASP.NET, 29 Sept 2003, in Redmond http://www.develop.com/courses/2daspdotnet Guerrilla ASP.NET, 13 Oct 2003, in Boston http://www.develop.com/courses/gaspdotnet View archives and manage your subscription(s) at http://discuss.develop.com
