delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (7)


SCSI-2 device serial numberGo to Alex Konshin's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Using DeviceIoControl to extract SCSI-2 device s/n.
Product:
Delphi 3.x (or higher)
Category:
Win API
Skill Level:
Scoring:
Last Update:
11/18/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet DeviceIoControl serial-number SCSI device
Times Scored:
3
Visits:
9114
Uploader: Alex Konshin
Company: PTC
Reference: www.mtgroup.ru
 
Question/Problem/Abstract:
For IDE harddisk you can use S.M.A.R.T. API function for
extracting serial number. For SCSI it does not work.
Answer:



program ScsiSN;

// PURPOSE: Simple console application that display SCSI harddisk serial number

{$APPTYPE CONSOLE}

uses
  Windows, SysUtils;

//-------------------------------------------------------------
function GetDeviceHandle( sDeviceName : String ) : THandle;
begin
  Result := CreateFile( PChar('\\.\'+sDeviceName),
    GENERIC_READ or GENERIC_WRITE,
    FILE_SHARE_READ or FILE_SHARE_WRITE,
    nil, OPEN_EXISTING, 0, 0 )
end;

//-------------------------------------------------------------
function ScsiHddSerialNumber( DeviceHandle : THandle ) : String;
{$ALIGN ON}
type
  TScsiPassThrough = record
    Length             : Word;
    ScsiStatus         : Byte;
    PathId             : Byte;
    TargetId           : Byte;
    Lun                : Byte;
    CdbLength          : Byte;
    SenseInfoLength    : Byte;
    DataIn             : Byte;
    DataTransferLength : ULONG;
    TimeOutValue       : ULONG;
    DataBufferOffset   : DWORD;
    SenseInfoOffset    : ULONG;
    Cdb                : Array[0..15] of Byte;
  end;
  TScsiPassThroughWithBuffers = record
    spt : TScsiPassThrough;
    bSenseBuf : Array[0..31] of Byte;
    bDataBuf : Array[0..191] of Byte;
  end;
{ALIGN OFF}
var dwReturned : DWORD;
    len : DWORD;
    Buffer : Array[ 0..SizeOf(TScsiPassThroughWithBuffers)+
                       SizeOf(TScsiPassThrough)-1] of Byte;
    sptwb : TScsiPassThroughWithBuffers absolute Buffer;
begin
  Result := '';
  FillChar(Buffer,SizeOf(Buffer),#0);
  with sptwb.spt do
  begin
    Length   := SizeOf(TScsiPassThrough);
    CdbLength := 6; // CDB6GENERIC_LENGTH
    SenseInfoLength := 24;
    DataIn := 1; // SCSI_IOCTL_DATA_IN
    DataTransferLength := 192;
    TimeOutValue := 2;
    DataBufferOffset := PChar(@sptwb.bDataBuf)-PChar(@sptwb);
    SenseInfoOffset := PChar(@sptwb.bSenseBuf)-PChar(@sptwb);
    Cdb[0] := $12; // OperationCode := SCSIOP_INQUIRY;
    Cdb[1] := $01; // Flags := CDB_INQUIRY_EVPD;  Vital product data
    Cdb[2] := $80; // PageCode            Unit serial number
    Cdb[4] := 192; // AllocationLength
  end;
  len := sptwb.spt.DataBufferOffset+sptwb.spt.DataTransferLength;
  if DeviceIoControl( DeviceHandle, $0004d004, @sptwb, SizeOf(TScsiPassThrough), @sptwb, len, dwReturned, nil )
    and ((PChar(@sptwb.bDataBuf)+1)^=#$80)
  then
   SetString( Result, PChar(@sptwb.bDataBuf)+4,
     Ord((PChar(@sptwb.bDataBuf)+3)^) );
end;


//=============================================================
var
  hDevice : THandle = 0;
  sSerNum, sDeviceName : String;

begin
  sDeviceName := ParamStr(1);
  if sDeviceName='' then
  begin
    WriteLn;
    WriteLn('Display SCSI-2 device serial number.');
    WriteLn;
    WriteLn('Using:');
    WriteLn;
    if Win32Platform=VER_PLATFORM_WIN32_NT then // Windows NT/2000
      WriteLn('  ScsiSN PhysicalDrive0')
    else
      WriteLn('  ScsiSN C:');
    WriteLn('  ScsiSN Cdrom0');
    WriteLn('  ScsiSN Tape0');
    WriteLn;
    Exit;
  end;
  hDevice := GetDeviceHandle(sDeviceName);
  if hDevice=INVALID_HANDLE_VALUE then
   WriteLn('Error on GetDeviceHandle: ',SysErrorMessage(GetLastError))
  else
    try
      sSerNum := ScsiHddSerialNumber(hDevice);
      if sSerNum='' then
        WriteLn('Error on DeviceIoControl: ',
          SysErrorMessageGetLastError))
      else
        WriteLn('Device '+sDeviceName
          +' serial number = "',sSerNum,'"');
    finally
      CloseHandle(hDevice);
    end;
end.

// For more information about SCSI commands:
// ftp://ftp.t10.org/t10/drafts/scsi-1/
// ftp://ftp.t10.org/t10/drafts/spc/
// ftp://ftp.t10.org/t10/drafts/spc2/







Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
ASPI
    Alex Konshin (Jul 18 2001 9:29PM)

Look at this:
http://www.adaptec.com/worldwide/support/driverdetail.html?filekey=aspi32.exe
Respond

SATA
Gankhuyag D. (Aug 14 2006 10:02AM)

Can you show me how can I get SATA HD Serial number?
Respond

RE: SATA
Alex Konshin (Aug 14 2006 9:45PM)

See my another article
http://www.delphi3000.com/articles/article_1204.asp
Respond

SCSI under Win9x
    Man^Crowd (Nov 3 2000 4:08PM)

To get model, vendor id, serial you'll have to use ASPI (Advanced SCSI Programming Interface).
All you need is to read the article from MSDN.

Good luck.
Respond

RE: SCSI under Win9x
anonymus (Nov 4 2000 5:57AM)

ASPI is not a part of Windows 9x.

Indeed, It can be downloaded from www.adaptec.com,
but unfortunately on NT is not officially supported by Microsoft.

See MSDN -> KB: Windows development ->
INFO: ASPI on Windows NT Is Not Supported by Microsoft
ID: Q182542





Respond

RE: RE: SCSI under Win9x
Man^Crowd (Nov 16 2000 7:18AM)

If you'll read carefully the source code, you'll understand my comment. All you said is correct!
Under Win9x the code will not work. Why? Because, you can't get an handle to disk using CreateFile(). So...under Win9x you'll have to use ASPI layer.
Respond

RE: RE: RE: SCSI under Win9x
Farrg Eyou (Jul 18 2001 8:41PM)

So where do you find the info on the ASPI layer?

This example program you have made is quite nice and functions very well. But I also need to use it on NT/9x/ME. I would like to have both S.M.A.R.T. and ASPI for various configurations of systems.

Thank you.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
A. B. Talal
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)