Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [VB.NET] Porting di codice comparso sul forum

    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.


  2. #2
    Cosa restituisce Read?
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Mi restituisce -1, che è poi il valore di uscita della funzione se l'API ReadFile incontra
    errori nella lettura.

  4. #4
    Sei loggato come amministratore? Hai provato a verificare che errore si verifica di preciso con Err.LastDLLError?
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Si, il mio è un account di amministrazione.
    Ho fatto ora il test che mi hai consigliato, ed ho ricevuto una segnalazione di errore 126.

    Il significato di questo errore dovrebbe essere: The specified module could not be found.
    ma credo proprio di aver dichiarato correttamente la funzione.

  6. #6
    Già... suona molto strano anche a me.
    Amaro C++, il gusto pieno dell'undefined behavior.

  7. #7
    Non riuscendo a risolvere la questione, ho deciso di tagliare la testa al toro:
    mi sto scrivendo una dll in c++ per esportare le funzini che mi servono.

    Fino ad ora, i risultati sono buoni, speriamo in bene.
    Grazie comunque

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.