retrieve harddisk`s modell,serial,firmware (Windows only)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rings
Moderator
Moderator
Posts: 1427
Joined: Sat Apr 26, 2003 1:11 am

retrieve harddisk`s modell,serial,firmware (Windows only)

Post by Rings »

Code: Select all

Structure IDEREGS
    bFeaturesReg.b
    bSectorCountReg.b
    bSectorNumberReg.b
    bCylLowReg.b
    bCylHighReg.b
    bDriveHeadReg.b
    bCommandReg.b
    bReserved.b
EndStructure
Structure SENDCMDINPARAMS
    cBufferSize.l
    irDriveRegs.IDEREGS
    bDriveNumber.b
    bReserved.b(3)
    dwReserved.l(4)
EndStructure

Structure  DRIVERSTATUS
    bDriveError.b
    bIDEStatus.b
    bReserved.b(2)
    dwReserved.l(2)
EndStructure
Structure  SENDCMDOUTPARAMS
    cBufferSize.l
    DStatus.DRIVERSTATUS     
    bBuffer.b(512) 
EndStructure


#DFP_RECEIVE_DRIVE_DATA = $7C088

bin.SENDCMDINPARAMS
bout.SENDCMDOUTPARAMS



Procedure.s ChangeHighLowByte(Instring.s)
;Change BIG-Little Endian
sdummy.s=""
L=Len(Instring)
For I=1 To L Step 2
 If (I+1)<=L
  sdummy.s=sdummy.s + Mid(Instring,I+1,1)+Mid(Instring,I,1)  
 EndIf
Next I 
ProcedureReturn sdummy.s
EndProcedure 


mvarCurrentDrive=0 ;If you have more hard-disks change it here

hdh = CreateFile_("\\.\PhysicalDrive" + Str(mvarCurrentDrive),#GENERIC_READ | #GENERIC_WRITE, #FILE_SHARE_READ | #FILE_SHARE_WRITE,0, #OPEN_EXISTING, 0, 0)
If hdh  
        bin\bDriveNumber = mvarCurrentDrive
        bin\cBufferSize = 512
        
        If (mvarCurrentDrive & 1) 
          bin\irDriveRegs\bDriveHeadReg = $B0
        Else
          bin\irDriveRegs\bDriveHeadReg = $A0
        EndIf
        bin\irDriveRegs\bCommandReg = $EC
        bin\irDriveRegs\bSectorCountReg = 1
        bin\irDriveRegs\bSectorNumberReg = 1
    
       br=0
       Result=DeviceIoControl_( hdh, #DFP_RECEIVE_DRIVE_DATA, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0)
       If br>0
        hddfr = 55:hddln = 40          :
        Modell.s=ChangeHighLowByte(PeekS(@bout\bBuffer[0]+hddfr-1 ,hddln  ) )
        hddfr = 21:hddln = 20          :
        Serial.s=Trim(ChangeHighLowByte(PeekS(@bout\bBuffer[0]+hddfr-1 ,hddln  ) ))
        hddfr = 47:hddln = 8          
        Firmware.s=ChangeHighLowByte(PeekS(@bout\bBuffer[0]+hddfr-1 ,hddln  ) )
        MessageRequester("Info about your harddisk","vendor(Modell)="+Modell.s + Chr(13) +"serial="+Serial.s+ Chr(13)+"Firmwareversion="+Firmware ,0)
       EndIf
Else
  beep_(100,100)
endif 
SPAMINATOR NR.1
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: retrieve harddisk`s modell,serial,firmware (Windows only

Post by ricardo »

Dosen't work in w98 :(
ARGENTINA WORLD CHAMPION
User avatar
Rings
Moderator
Moderator
Posts: 1427
Joined: Sat Apr 26, 2003 1:11 am

Re: retrieve harddisk`s modell,serial,firmware (Windows only

Post by Rings »

ricardo wrote:Dosen't work in w98 :(
yes, i use

Code: Select all

\\.\PhysicalDrive"
which is not present under win89.
I'm sorry but i cannot supported win89.
SPAMINATOR NR.1
ricardo
Addict
Addict
Posts: 2402
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: retrieve harddisk`s modell,serial,firmware (Windows only

Post by ricardo »

But then why i can'r make it run there?


Rings wrote: which is not present under win89.
I'm sorry but i cannot supported win89.
??? I don't understand
ARGENTINA WORLD CHAMPION
Revolver
User
User
Posts: 22
Joined: Tue Apr 29, 2003 9:20 pm

Post by Revolver »

Windows 95/98/ME doesn't allow that kind of access to the drives. Thats why in my dll users with those platforms need to copy the .vxd file to the system folder. Its a virtual device driver that allows the dll to talk to the ide drives. Windows NT/2000/XP doesn't need to do this. Also, I believe that you have to have administrator rights in NT/2000/XP for Ring's code to work. With my dll, you can have any user access.
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Re: retrieve harddisk`s modell,serial,firmware (Windows only

Post by Max. »

ricardo wrote:But then why i can'r make it run there???? I don't understand
Ricardo,

I cannot try it at the moment, but from what I read on MSDN
http://support.microsoft.com/default.as ... bContent=1,
you need to change the CreateFile call to

Code: Select all

hdh=CreateFile("\\\\.\\SMARTVSD", 0,0,0,                      #CREATE_NEW, 0, 0)
and check the further requirements:

SMARTVSD Troubleshooting Checklist
If opening SMARTVSD fails in Window 95 or Windows 98, one of the following might be the cause:


You are using the original version of Windows 95, which does not support SMART (the SDI_506.PDR port driver does not contain IDE PASSTHROUGH functionality).
SMARTVSD.VXD is not installed in the \windows\system\iosubsys directory.
You did not restart after installing SMARTVSD.VXD.
Your filesystem is running in Compatibility Mode (see the System Properties dialog box, click the Performance tab). This means that the protected mode IOS subsystem, containing SMARTVSD, is being bypassed.
Your system does not have any IDE drives. ESDI_506.PDR does not remain resident if there are no IDE drives.
Your IDE drives are using a third-party SCSI miniport driver instead of Microsoft's ESDI_506 driver.
Windows 98 inadvertently omitted SMARTVSD
Hope it helps a bit...
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Garbage error??!

Post by Seldon »

I've tried to compile it with PB3.72, but I get a "Garbage at the end of file" error in lines 15. 16,22,23 and 28. What's wrong with arrays inside a structure??? Thank you.
Max.
Enthusiast
Enthusiast
Posts: 225
Joined: Fri Apr 25, 2003 8:39 pm

Re: Garbage error??!

Post by Max. »

Seldon wrote:I've tried to compile it with PB3.72, but I get a "Garbage at the end of file" error in lines 15. 16,22,23 and 28. What's wrong with arrays inside a structure??? Thank you.
Seldon,

change the lines like this

Old:
bReserved.b(3)

New:
bReserved.b[3]

using square brackets instead of parenthesises for any array inside structures.
User avatar
Rings
Moderator
Moderator
Posts: 1427
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

that point with those different brackets is anoying !!!never have this problems under VB or Powerbasic.......time to fix that!
SPAMINATOR NR.1
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

Hi alls and thanks for reply. Using the routine, I noticed that this line:

Code: Select all

If hdh
should be changed into:

Code: Select all

If hdh<>#INVALID_HANDLE_VALUE   
Also, here it is the code I did for easy Win9x/NT+ compatibility (simply add such lines before the others) :

Code: Select all


#VER_PLATFORM_WIN32_WINDOWS=1
VersionInfo.OSVERSIONINFO
 
VersionInfo\dwOSVersionInfoSize = SizeOf(OSVERSIONINFO)
res=GetVersionEx_(VersionInfo)
If res
 If VersionInfo\dwPlatformId=#VER_PLATFORM_WIN32_WINDOWS

  hdh=CreateFile_("\\.\SMARTVSD",#GENERIC_READ|#GENERIC_WRITE,#FILE_SHARE_READ|#FILE_SHARE_WRITE,0,#OPEN_EXISTING,0,0)

 Else

  hdh=CreateFile_("\\.\PhysicalDrive"+Str(ndrive),#GENERIC_READ|#GENERIC_WRITE,#FILE_SHARE_READ|#FILE_SHARE_WRITE,0,#OPEN_EXISTING,0,0)  

 EndIf
EndIf

Remember to move the Smartvsd.vxd file in Windows\System\IOSUBSYS under Windows98 to make it work.
Last edited by Seldon on Thu Dec 18, 2003 8:58 am, edited 1 time in total.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

Thanks for the tip Seldon, but I get nothing with Win98SE and I do have
SMARTVSD.VXD in the correct place.

If you have working code for Win98SE I would certainly like to see it.

Thanks,
Terry
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Terry, check the size of SMARTVSD.VXD. It didn't work for me at first because I have a Promise IDE Controller that put it's own version in place of MS's. I found the original in Windows\System and (after backing-up the Promise version) put this in Windows\System\IOSubsys and it worked fine.

I think the MS one is 18K, the Promise one 9K but look at the file properties to be sure.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

Thanks Dmoc. I do have the correct one in \Windows\System\Iosybsys.

Must be something in the code I have tried to cobble together from this
post.

???
Terry
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Here's my edited code:

Code: Select all

Structure IDEREGS 
  bFeaturesReg.b 
  bSectorCountReg.b 
  bSectorNumberReg.b 
  bCylLowReg.b 
  bCylHighReg.b 
  bDriveHeadReg.b 
  bCommandReg.b 
  bReserved.b 
EndStructure 

Structure SENDCMDINPARAMS 
  cBufferSize.l 
  irDriveRegs.IDEREGS 
  bDriveNumber.b 
  bReserved.b[3] 
  dwReserved.l[4] 
EndStructure 

Structure  DRIVERSTATUS 
  bDriveError.b 
  bIDEStatus.b 
  bReserved.b[2] 
  dwReserved.l[2] 
EndStructure 
Structure  SENDCMDOUTPARAMS 
  cBufferSize.l 
  DStatus.DRIVERSTATUS
  bBuffer.b[512] 
EndStructure 

#DFP_RECEIVE_DRIVE_DATA = $7C088 

bin.SENDCMDINPARAMS 
bout.SENDCMDOUTPARAMS 

Procedure.s ChangeHighLowByte(Instring.s) 
  ;Change BIG-Little Endian 
  sdummy.s="" 
  L=Len(Instring) 
  For I=1 To L Step 2 
    If (I+1)<=L 
      sdummy.s=sdummy.s + Mid(Instring,I+1,1)+Mid(Instring,I,1)  
    EndIf 
  Next I 
  ProcedureReturn sdummy.s 
EndProcedure 


mvarCurrentDrive=0 ;If you have more hard-disks change it here 

;hdh = CreateFile_("\\.\PhysicalDrive" + Str(mvarCurrentDrive),#GENERIC_READ | #GENERIC_WRITE, #FILE_SHARE_READ | #FILE_SHARE_WRITE,0, #OPEN_EXISTING, 0, 0) 
;hdh = CreateFile_("\\.\SMARTVSD" + Str(mvarCurrentDrive),#GENERIC_READ | #GENERIC_WRITE, #FILE_SHARE_READ | #FILE_SHARE_WRITE,0, #OPEN_EXISTING, 0, 0) 
;hdh = CreateFile_("\\.\SMARTVSD", 0,0,0, #CREATE_NEW, 0, 0)
;If hdh

#VER_PLATFORM_WIN32_WINDOWS = 1 
VersionInfo.OSVERSIONINFO 

VersionInfo\dwOSVersionInfoSize = SizeOf(OSVERSIONINFO) 
res=GetVersionEx_(VersionInfo) 
If res 
  If VersionInfo\dwPlatformId=#VER_PLATFORM_WIN32_WINDOWS 
    hdh=CreateFile_("\\.\SMARTVSD",#GENERIC_READ|#GENERIC_WRITE,#FILE_SHARE_READ|#FILE_SHARE_WRITE,0,#OPEN_EXISTING,0,0) 
  Else 
    hdh=CreateFile_("\\.\PhysicalDrive"+Str(ndrive),#GENERIC_READ|#GENERIC_WRITE,#FILE_SHARE_READ|#FILE_SHARE_WRITE,0,#OPEN_EXISTING,0,0) 
  EndIf 
EndIf 

If hdh<>#INVALID_HANDLE_VALUE 
  bin\bDriveNumber = mvarCurrentDrive 
  bin\cBufferSize = 512 
  
  If (mvarCurrentDrive & 1) 
    bin\irDriveRegs\bDriveHeadReg = $B0 
  Else 
    bin\irDriveRegs\bDriveHeadReg = $A0 
  EndIf 
  bin\irDriveRegs\bCommandReg = $EC 
  bin\irDriveRegs\bSectorCountReg = 1 
  bin\irDriveRegs\bSectorNumberReg = 1 
  
  br=0 
  Result=DeviceIoControl_( hdh, #DFP_RECEIVE_DRIVE_DATA, bin, SizeOf(SENDCMDINPARAMS), bout, SizeOf(SENDCMDOUTPARAMS), @br, 0) 
  If br>0 
    hddfr = 55:hddln = 40: Modell.s=ChangeHighLowByte(PeekS(@bout\bBuffer[0]+hddfr-1 ,hddln  ) ) 
    hddfr = 21:hddln = 20: Serial.s=Trim(ChangeHighLowByte(PeekS(@bout\bBuffer[0]+hddfr-1 ,hddln  ) )) 
    hddfr = 47:hddln = 8 : Firmware.s=ChangeHighLowByte(PeekS(@bout\bBuffer[0]+hddfr-1 ,hddln  ) ) 
    MessageRequester("Info about your harddisk","vendor(Modell)="+Modell.s + Chr(13) +"serial="+Serial.s+ Chr(13)+"Firmwareversion="+Firmware ,0) 
  EndIf 
Else 
  Debug "ERROR: Could not open media!"
  beep_(100,100) 
EndIf 

End

PS: This line...

Code: Select all

    hdh=CreateFile_("\\.\SMARTVSD",#GENERIC_READ|#GENERIC_WRITE,#FILE_SHARE_READ|#FILE_SHARE_WRITE,0,#OPEN_EXISTING,0,0) 
...for safety I would change to...

Code: Select all

    hdh=CreateFile_("\\.\SMARTVSD",#GENERIC_READ,#FILE_SHARE_READ,0,#OPEN_EXISTING,0,0) 
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

Thanks dmoc. That works for me. Now I will go see where I messed up.

Terry
Post Reply