_lread





VB.Net


Imports System.Runtime.InteropServices

Module _LRead

    Const GENERIC_READ = &H80000000
    Const GENERIC_WRITE = &H40000000
    Const GENERIC_EXECUTE = &H20000000
    Const GENERIC_ALL = &H10000000

    Const CREATE_NEW = 1
    Const CREATE_ALWAYS = 2
    Const OPEN_EXISTING = 3
    Const OPEN_ALWAYS = 4
    Const TRUNCATE_EXISTING = 5

    Const KERNEL32 As String = "kernel32.dll"

    <DllImport(KERNEL32, EntryPoint:="_lread", CallingConvention:=CallingConvention.Winapi)>
    Private Function _lread(hFile As IntPtr, lpBuffer As Byte(), nBytesToRead As Integer) As Integer
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Ansi)>
    Private Function CreateFileA(lpFileName As String, dwDesiredAccess As Integer, dwShareMode As Integer, lpSecurityAttributes As IntPtr, dwCreationDisposition As Integer, dwFlagsAndAttributes As Integer, hTemplateFile As IntPtr) As IntPtr
    End Function

    <DllImport("kernel32", SetLastError:=True)>
    Private Function CloseHandle(hObject As IntPtr) As Boolean
    End Function

    Sub _lread_Main()
        ' Open a file
        Dim hFile As IntPtr = CreateFileA("C:\Kernel32\example.txt", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)

        If hFile <> IntPtr.Zero Then
            ' Read 1024 bytes from the file
            Dim buffer(1023) As Byte
            Dim bytesRead As Integer = _lread(hFile, buffer, 1024)

            ' Process the read data
            Console.WriteLine("Read {0} bytes:", bytesRead)
            For i As Integer = 0 To bytesRead - 1
                Console.Write(buffer(i).ToString("X2") & " ")
            Next
            Console.WriteLine()

            ' Close the file
            CloseHandle(hFile)
        End If
    End Sub
End Module