Salve a tutti,
ho trovato molto interessante un post comparso tra le pillole del forum, relativo all'accesso ai dispositivi fisici da programma, utilizzando le UNC.
Sto cercando di eseguirne il porting in VB.NET (era in C#), ma c'è qualche problema, stranamente non riesco a leggere bytes dal disco.
Qualcuno può aiutarmi?
Questo il codice (la classe):
codice:
Public Enum FileAccess : uint
GenericRead = &H80000000
GenericWrite = &H40000000
GenericExecute = &H20000000
GenericAll = &H10000000
End Enum
Public Enum FileShare
None = &H0
Read = &H1
Write = &H2
Delete = &H4
End Enum
Public Enum CreationDisposition
CreateNew = 1
CreateAlways = 2
OpenExisting = 3
OpenAlways = 4
TruncateExisting = 5
End Enum
Public Enum FileAttributes
Rdonly = &H1
Hidden = &H2
System = &H4
Directory = &H10
Archive = &H20
Device = &H40
Normal = &H80
Temporary = &H100
SparseFile = &H200
ReparsePoint = &H400
Compressed = &H800
Offline = &H1000
NotContentIndexed = &H2000
Encrypted = &H4000
Write_Through = &H80000000
Overlapped = &H40000000
NoBuffering = &H20000000
RandomAccess = &H10000000
SequentialScan = &H8000000
DeleteOnClose = &H4000000
BackupSemantics = &H2000000
PosixSemantics = &H1000000
OpenReparsePoint = &H200000
OpenNoRecall = &H100000
FirstPipeInstance = &H80000
End Enum
Public Class IOWin32Helper
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As FileAccess, ByVal dwShareMode As FileShare, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As CreationDisposition, ByVal dwFlagsAndAttributes As FileAttributes, ByVal hTemplateFile As IntPtr) As IntPtr
Private Declare Function ReadFile Lib "kernel32" Alias "ReadFile" (ByVal hFile As IntPtr, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Long, ByVal lpNumberOfBytesRead As Long, ByVal lpOverlapped As Integer) As Boolean
Private Declare Function WriteFile Lib "kernel32" Alias "WriteFile" (ByVal hFile As IntPtr, ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Long, ByVal lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Integer) As Boolean
Private Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As IntPtr) As Long
Public Function Create(ByVal FileName As String, ByVal FileAccess As FileAccess, ByVal FileShare As FileShare, ByVal CreationDisposition As CreationDisposition, ByVal FileAttributes As FileAttributes) As IntPtr
Dim handle As IntPtr = CreateFile(FileName, FileAccess, FileShare, System.IntPtr.Zero, CreationDisposition, FileAttributes, System.IntPtr.Zero)
If handle.Equals(System.IntPtr.Zero) Then
Return Nothing
End If
Return handle
End Function
Public Function Read(ByVal hObject As IntPtr, ByVal Buffer As Byte(), ByVal Index As Integer, ByVal Count As Integer) As Integer
Dim readBytes As Integer = 0
If (ReadFile(hObject, Buffer, Count, readBytes, 0) = False) Then
Return -1
End If
Return readBytes
End Function
Public Function Write(ByVal hObject As IntPtr, ByVal Buffer As Byte(), ByVal Index As Integer, ByVal Count As Integer) As Integer
Dim writtenBytes As Integer = 0
If (WriteFile(hObject, Buffer, Count, writtenBytes, 0) = False) Then
Return 0
End If
Return writtenBytes
End Function
Public Function Close(ByVal hObject As IntPtr) As Boolean
Return CloseHandle(hObject)
End Function
End Class
E questo il codice che ho usato per testare la classe:
codice:
Dim ioHelp As New IOWin32Helper
Dim handle As IntPtr = ioHelp.Create("\\.\PhysicalDrive0", FileAccess.GenericRead, FileShare.Read, CreationDisposition.OpenExisting, FileAttributes.Normal)
Dim bytes(512) As Byte
MsgBox(ioHelp.Read(handle, bytes, 0, bytes.Length).ToString)
Debug.Write(System.Text.Encoding.ASCII.GetString(bytes))
ioHelp.Close(handle)
Grazie fin d'ora a quanti risponderanno.