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 / samples4 / ch13 / frmdevio.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  4.1 KB  |  110 lines

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