Tanveer,
After a first quick glance I think you are confusing data types: in C#, the
type "long" denotes a (signed) 64-bit type. When an API call takes DWORD,
you should use int (or uint), since you need a 32-bit type. For pointers and
handles, you should use IntPtr, which is as large as the native pointer type
(for null pointers use IntPtr.Zero).
Also, I don't think lpOutBuffer should be "out IntPtr", make it "IntPtr"
instead (that's like passing a pointer in C++, "out IntPtr" would be pointer
to pointer). "out byte lpBytesReturned" should be "out int lpBytesReturned",
I think (at least it is LPDWORD in the docs). And I am not sure if you can
simply pass an object for lpOverlapped, maybe you should use an IntPtr
instead (although I don't really know what happens when you pass an object
via PInvoke).
hth
Fabian
----- Original Message -----
From: "Tanveer" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, August 30, 2003 11:23 AM
Subject: [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
>
===================================
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