home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / vbpg32 / samples5 / ch13 / frmdevio.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  4.1 KB  |  109 lines

  1. VERSION 5.00
  2. Begin VB.Form frmDevIO 
  3.    Caption         =   "DeviceIoControl Example"
  4.    ClientHeight    =   3240
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1515
  7.    ClientWidth     =   5955
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   3240
  11.    ScaleWidth      =   5955
  12.    Begin VB.CommandButton Command2 
  13.       Caption         =   "A:"
  14.       Height          =   435
  15.       Left            =   120
  16.       TabIndex        =   2
  17.       Top             =   840
  18.       Width           =   1575
  19.    End
  20.    Begin VB.ListBox List1 
  21.       Height          =   2760
  22.       Left            =   2160
  23.       TabIndex        =   1
  24.       Top             =   240
  25.       Width           =   3555
  26.    End
  27.    Begin VB.CommandButton Command1 
  28.       Caption         =   "Physical Drive 0"
  29.       Height          =   495
  30.       Left            =   120
  31.       TabIndex        =   0
  32.       Top             =   180
  33.       Width           =   1575
  34.    End
  35.    Begin VB.Label Label1 
  36.       Caption         =   "This sample shows how to retreive disk geometry for physical and logical drives. Some capabilities are NT only."
  37.       Height          =   1035
  38.       Left            =   120
  39.       TabIndex        =   3
  40.       Top             =   1500
  41.       Width           =   1815
  42.    End
  43. Attribute VB_Name = "frmDevIO"
  44. Attribute VB_GlobalNameSpace = False
  45. Attribute VB_Creatable = False
  46. Attribute VB_PredeclaredId = True
  47. Attribute VB_Exposed = False
  48. Option Explicit
  49. ' Copyright 
  50.  1997 by Desaware Inc. All Rights Reserved
  51. Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long
  52. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  53. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  54. Private Declare Function GetLastError Lib "kernel32" () As Long
  55. Private Const FILE_SHARE_READ = &H1
  56. Private Const FILE_SHARE_WRITE = &H2
  57. Private Const OPEN_EXISTING = 3
  58. Private Const IOCTL_DISK_GET_DRIVE_GEOMETRY = &H70000
  59. Private Type DISK_GEOMETRY
  60.     CylindersLow As Long
  61.     CylindersHigh As Long
  62.     MediaType As Long
  63.     TracksPerCylinder As Long
  64.     SectorsPerTrack As Long
  65.     BytesPerSector As Long
  66. End Type
  67. Private Sub Command1_Click()
  68.    Dim fhnd&
  69.    Dim res&
  70.    Dim dg As DISK_GEOMETRY
  71.    Dim bytesread As Long
  72.    fhnd = CreateFile("\\.\PhysicalDrive0", 0, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
  73.    If fhnd = -1 Then
  74.       MsgBox "Unable to open PhysicalDrive0"
  75.       Exit Sub
  76.    End If
  77.    res = DeviceIoControl(fhnd, IOCTL_DISK_GET_DRIVE_GEOMETRY, ByVal 0, 0, dg, Len(dg), bytesread, 0)
  78.    If res = 0 Then
  79.       MsgBox "DeviceIoControl failed " & Hex$(GetLastError())
  80.       Exit Sub
  81.    End If
  82.    List1.AddItem "Cylinders: " & dg.CylindersLow
  83.    List1.AddItem "TracksPerCylinder: " & dg.TracksPerCylinder
  84.    List1.AddItem "SectorsPerTrack: " & dg.SectorsPerTrack
  85.    List1.AddItem "BytesPerSector: " & dg.BytesPerSector
  86.    Call CloseHandle(fhnd)
  87. End Sub
  88. Private Sub Command2_Click()
  89.    Dim fhnd&
  90.    Dim res&
  91.    Dim dg As DISK_GEOMETRY
  92.    Dim bytesread As Long
  93.    fhnd = CreateFile("\\.\A:", 0, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
  94.    If fhnd = -1 Then
  95.       MsgBox "Unable to open A:"
  96.       Exit Sub
  97.    End If
  98.    res = DeviceIoControl(fhnd, IOCTL_DISK_GET_DRIVE_GEOMETRY, ByVal 0, 0, dg, Len(dg), bytesread, 0)
  99.    If res = 0 Then
  100.       MsgBox "DeviceIoControl failed " & Hex$(GetLastError())
  101.       Exit Sub
  102.    End If
  103.    List1.AddItem "Cylinders: " & dg.CylindersLow
  104.    List1.AddItem "TracksPerCylinder: " & dg.TracksPerCylinder
  105.    List1.AddItem "SectorsPerTrack: " & dg.SectorsPerTrack
  106.    List1.AddItem "BytesPerSector: " & dg.BytesPerSector
  107.    Call CloseHandle(fhnd)
  108. End Sub
  109.