Dear Fabian,

I have converted this to VB.NET and removed the DllImport attribute...this
seems to solve the GC-ed pointer problem...but still the return buffer is
not as desired.

The complete code is:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Module VBHDD
Public Class clsHDD
Public Const GENERIC_READ As Long = &H80000000L
Public Const GENERIC_WRITE As Long = &H40000000L
Public Const FILE_SHARE_READ As Long = &H1
Public Const FILE_SHARE_WRITE As Long = &H2
Public Const OPEN_EXISTING As Long = 3
Public Declare Unicode Function CreateFile Lib "kernel32" Alias
"CreateFileA" ( _
ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode
As Long, _
ByVal lpSecurityAttributes As Object, ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Declare Unicode Function CloseHandle Lib "kernel32" Alias
"CloseHandle" ( _
ByVal hObject As Long) As Long
Public Declare Ansi Function DeviceIoControl Lib "kernel32" ( _
ByVal hDevice As Long, ByVal dwIoControlCode As Long, ByVal lpInBuffer As
IntPtr, _
ByVal nInBufferSize As Long, ByVal lpOutBuffer As IntPtr, ByVal
nOutBufferSize As Long, _
ByVal lpBytesReturned As IntPtr, ByVal lpOverlapped As IntPtr) As Long
Public Shared Function SCSI() As Long
Dim success As Boolean = False
Dim hFile As Long = CreateFile("\\\\.\\Scsi0:", GENERIC_READ Or
GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
Console.WriteLine("File Pointer ...{0}", hFile)
If (hFile <> -1) Then
Dim Dummy As IntPtr = IntPtr.Zero
Dim buffer() As Byte
ReDim buffer(560)
Dim x As Integer = 0
For x = 0 To 559
buffer(x) = CType(0, Byte)
Next
x = 0
buffer(x) = CType(28, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(83, Byte) : x += 1
buffer(x) = CType(67, Byte) : x += 1
buffer(x) = CType(83, Byte) : x += 1
buffer(x) = CType(73, Byte) : x += 1
buffer(x) = CType(68, Byte) : x += 1
buffer(x) = CType(73, Byte) : x += 1
buffer(x) = CType(83, Byte) : x += 1
buffer(x) = CType(75, Byte) : x += 1
'Timeout
buffer(x) = CType(16, Byte) : x += 1
buffer(x) = CType(39, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
'Control Code
buffer(x) = CType(1, Byte) : x += 1
buffer(x) = CType(5, Byte) : x += 1
buffer(x) = CType(27, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(20, Byte) : x += 1
buffer(x) = CType(2, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1
buffer(x) = CType(&HEC, Byte) : x += 1
buffer(x) = CType(0, Byte) : x += 1

For x = 0 To 63
Console.Write("{0}", Chr(buffer(x)))
Next
Dim sb As IntPtr = Marshal.AllocCoTaskMem(560)
Marshal.Copy(buffer, 0, sb, 560)
success = DeviceIoControl(hFile, 315400, sb, 63, sb, 560, Dummy,
IntPtr.Zero)
If (success) Then
Console.WriteLine("Passed ")
Console.WriteLine("Returned sb: {0}", Marshal.PtrToStringAnsi(sb, 560))
Else
Dim err As Integer = Marshal.GetLastWin32Error()
Throw New Win32Exception(err)
Console.WriteLine("Failed Dummy: {0}", Dummy)
Console.WriteLine("Input: {0}", Marshal.PtrToStringAnsi(sb, 560))
Console.ReadLine()
End If
Marshal.FreeHGlobal(sb)
End If
Return CloseHandle(hFile)
End Function
End Class
Public Sub Main()
Console.WriteLine("Exit Code ...{0}", clsHDD.SCSI)
Console.ReadLine()
End Sub
End Module
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Any ideas...?

-Tanveer

----- Original Message -----
From: "Fabian Schmied" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, August 31, 2003 7:53 PM
Subject: {Mon-Incoming} Re: [ADVANCED-DOTNET] Interop with DeviceIoControl


> 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
>

===================================
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

Reply via email to