Hi Everyone, When I execute my code it is giving me error 87 and also when I use the Nested SetFilePointer I am getting Error code 6. Any idea why?

Thanks,
-Agent

Code:
Imports System.Runtime.InteropServices

Public Class Form1
    Public Enum EMoveMethod : uint
        FILE_BEGIN = 0
        FILE_CURRENT = 1
        FILE_END = 2
    End Enum

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Shared Function SetFilePointer( _
        ByVal hFile As IntPtr, _
        ByVal lDistanceToMove As Integer, _
        ByRef lpDistanceToMoveHigh As IntPtr, _
        ByVal dwMoveMethod As EMoveMethod) _
        As System.UInt32
    End Function

    <System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
    Shared Function CreateFile(ByVal lpFileName As String, _
    ByVal dwDesiredAccess As EFileAccess, _
    ByVal dwShareMode As EFileShare, _
    ByVal lpSecurityAttributes As IntPtr, _
    ByVal dwCreationDisposition As ECreationDisposition, _
    ByVal dwFlagsAndAttributes As EFileAttributes, _
    ByVal hTemplateFile As IntPtr) As IntPtr
    End Function

    Public Structure STORAGE_DEVICE_NUMBER
        Friend DeviceType As Integer
        Friend DeviceNumber As Integer
        Friend PartitionNumber As Integer
    End Structure

    Public Enum EFileAccess As System.Int32
        ''
        ''  The following are masks for the predefined standard access types
        ''

        DELETE = &H10000
        READ_CONTROL = &H20000
        WRITE_DAC = &H40000
        WRITE_OWNER = &H80000
        SYNCHRONIZE = &H100000

        STANDARD_RIGHTS_REQUIRED = &HF0000
        STANDARD_RIGHTS_READ = READ_CONTROL
        STANDARD_RIGHTS_WRITE = READ_CONTROL
        STANDARD_RIGHTS_EXECUTE = READ_CONTROL
        STANDARD_RIGHTS_ALL = &H1F0000
        SPECIFIC_RIGHTS_ALL = &HFFFF

        ''
        '' AccessSystemAcl access type
        ''

        ACCESS_SYSTEM_SECURITY = &H1000000

        ''
        '' MaximumAllowed access type
        ''

        MAXIMUM_ALLOWED = &H2000000

        ''
        ''  These are the generic rights.
        ''

        GENERIC_READ = &H80000000
        GENERIC_WRITE = &H40000000
        GENERIC_EXECUTE = &H20000000
        GENERIC_ALL = &H10000000
    End Enum

    Public Enum EFileShare
        FILE_SHARE_NONE = &H0
        FILE_SHARE_READ = &H1
        FILE_SHARE_WRITE = &H2
        FILE_SHARE_DELETE = &H4
    End Enum

    Public Enum ECreationDisposition
        ''' <summary>
        ''' Creates a new file, only if it does not already exist.
        ''' If the specified file exists, the function fails and the last-error code is set to ERROR_FILE_EXISTS (80).
        ''' If the specified file does not exist and is a valid path to a writable location, a new file is created.
        ''' </summary>
        CREATE_NEW = 1

        ''' <summary>
        ''' Creates a new file, always.
        ''' If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS (183).
        ''' If the specified file does not exist and is a valid path, a new file is created, the function succeeds, and the last-error code is set to zero.
        ''' For more information, see the Remarks section of this topic.
        ''' </summary>
        CREATE_ALWAYS = 2

        ''' <summary>
        ''' Opens a file or device, only if it exists.
        ''' If the specified file or device does not exist, the function fails and the last-error code is set to ERROR_FILE_NOT_FOUND (2).
        ''' For more information about devices, see the Remarks section.
        ''' </summary>
        OPEN_EXISTING = 3

        ''' <summary>
        ''' Opens a file, always.
        ''' If the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183).
        ''' If the specified file does not exist and is a valid path to a writable location, the function creates a file and the last-error code is set to zero.
        ''' </summary>
        OPEN_ALWAYS = 4

        ''' <summary>
        ''' Opens a file and truncates it so that its size is zero bytes, only if it exists.
        ''' If the specified file does not exist, the function fails and the last-error code is set to ERROR_FILE_NOT_FOUND (2).
        ''' The calling process must open the file with the GENERIC_WRITE bit set as part of the dwDesiredAccess parameter.
        ''' </summary>
        TRUNCATE_EXISTING = 5
    End Enum

    Public Enum EFileAttributes
        FILE_ATTRIBUTE_READONLY = &H1
        FILE_ATTRIBUTE_HIDDEN = &H2
        FILE_ATTRIBUTE_SYSTEM = &H4
        FILE_ATTRIBUTE_DIRECTORY = &H10
        FILE_ATTRIBUTE_ARCHIVE = &H20
        FILE_ATTRIBUTE_DEVICE = &H40
        FILE_ATTRIBUTE_NORMAL = &H80
        FILE_ATTRIBUTE_TEMPORARY = &H100
        FILE_ATTRIBUTE_SPARSE_FILE = &H200
        FILE_ATTRIBUTE_REPARSE_POINT = &H400
        FILE_ATTRIBUTE_COMPRESSED = &H800
        FILE_ATTRIBUTE_OFFLINE = &H1000
        FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = &H2000
        FILE_ATTRIBUTE_ENCRYPTED = &H4000
        FILE_ATTRIBUTE_VIRTUAL = &H10000

        'This parameter can also contain combinations of flags (FILE_FLAG_*)
        FILE_FLAG_BACKUP_SEMANTICS = &H2000000
        FILE_FLAG_DELETE_ON_CLOSE = &H4000000
        FILE_FLAG_NO_BUFFERING = &H20000000
        FILE_FLAG_OPEN_NO_RECALL = &H100000
        FILE_FLAG_OPEN_REPARSE_POINT = &H200000
        FILE_FLAG_OVERLAPPED = &H40000000
        FILE_FLAG_POSIX_SEMANTICS = &H1000000
        FILE_FLAG_RANDOM_ACCESS = &H10000000
        FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
        FILE_FLAG_WRITE_THROUGH = &H80000000
    End Enum

    <DllImport("kernel32.dll", SetlastError:=True)> _
    Private Shared Function ReadFile(ByVal hFile As IntPtr, ByVal Buffer As IntPtr, _
    ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _
    ByRef lpOverlapped As Integer) As Integer
    End Function

    <DllImport("kernel32.dll", SetlastError:=True)> _
    Private Shared Function ReadFile(ByVal hFile As IntPtr, ByRef Buffer As Byte(), _
    ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, _
    ByRef lpOverlapped As Integer) As Integer
    End Function


    Public Const INVALID_SET_FILE_POINTER As Integer = -1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim Drive As String = "\\.\physicaldrive0"
        Dim Drive As String = "\\.\C:"
        Dim m_hDisk = CreateFile(Drive, EFileAccess.GENERIC_READ Or EFileAccess.GENERIC_WRITE, EFileShare.FILE_SHARE_READ Or EFileShare.FILE_SHARE_WRITE, Nothing, ECreationDisposition.OPEN_EXISTING, EFileAttributes.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
        Dim Buffer(1024) As Byte
        Dim BytesRead As Integer = 0
        Dim ip As IntPtr = Marshal.AllocHGlobal(1024)
        Dim Ret = SetFilePointer(m_hDisk, 0, 0, EMoveMethod.FILE_BEGIN)
        If Not Ret = INVALID_SET_FILE_POINTER Then
            ReadFile(m_hDisk, ip, 512, BytesRead, Nothing)
            Debug.WriteLine(Marshal.GetLastWin32Error())
            ReadFile(m_hDisk, Buffer, 512, BytesRead, Nothing)
            Debug.WriteLine(Marshal.GetLastWin32Error())
        End If

    End Sub
End Class