Question : PhysicalDrive0 in VBNET

Hi,

I am currently transposing my software from VB6 to VBNET. Most of the time I can struggle through reading manuals, searching the web etc. However i have a problem with some Hard Drive utilities I need to convert.

I use the CreateFile/ReadFile using PhysicalDrive* as as the source/target. I need direct sector access to hard disks and Floppy disks, USB, etc. I can not find how to do this in VBNET. I know how to open files using the System.IO class. However I can find no references to PhysicalDrive*

Answer : PhysicalDrive0 in VBNET

Try this updated code, and see if it fits your requirement:

Public Class DirectAccess

  Private Const INVALID_HANDLE = -1
  Private Const GENERIC_READ = &H80000000
  Private Const GENERIC_WRITE = &H40000000
  Private Const FILE_SHARE_READ = 1
  Private Const FILE_SHARE_WRITE = 2
  Private Const CREATE_ALWAYS = 2
  Private Const OPEN_EXISTING = 3
  Private Const INIT_SUCCESS = 0
  Private Const WRITE_ERROR = 0
  Private Const READ_ERROR = 0

  Private Declare Function CreateFile _
  Lib "kernel32" Alias "CreateFileA" _
  (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
   ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As Int32, _
   ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
   ByVal hTemplateFile As Int32) As Int32

  Private Declare Function WriteFile _
   Lib "kernel32" _
   (ByVal hFile As Int32, ByRef lpBuffer As Int32, ByVal nNumberOfBytesToWrite As Int32, _
    ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As Int32) As Int32

  Private Declare Function ReadFile _
   Lib "kernel32" _
   (ByVal hFile As Int32, ByRef lpBuffer As String, ByVal nNumberOfBytesToRead As Int32, _
   ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As Int32) As Int32

  Private hDevice As Int32
  Private hTarget As Int32

  Public Sub CreateFile(ByVal deviceNumber As Integer, ByVal targetNumber As Integer)

    hDevice = CreateFile("\\.\PHYSICALDRIVE" & deviceNumber, GENERIC_READ, _
     FILE_SHARE_READ, 0&, OPEN_EXISTING, 0&, 0&)

    hTarget = CreateFile("\\.\PHYSICALDRIVE" & targetNumber, GENERIC_WRITE, _
     FILE_SHARE_READ, 0&, OPEN_EXISTING, 0&, 0&)

  End Sub


  Public Sub CopyFile()

    Dim ReadBuffer As New String(" "c, 65536)   'Set to 64k block
    Dim BytesRead As Integer

    ' Read Source file.
    ReadFile(hDevice, ReadBuffer, ReadBuffer.Length, BytesRead, 0&)

    ' Write to Target.
    WriteFile(hTarget, ReadBuffer, ReadBuffer.Length, BytesRead, 0&)

  End Sub

End Class


Bob
Random Solutions  
 
programming4us programming4us