Issues with API function ReadFile

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ketchup

    #1

    Issues with API function ReadFile

    Hello everyone,

    I have been stuck with this problem for quite some time now. I am working in VB.NET, using framework 1.0. I have to keep the compatibility down to the original .NET framework as much as possible. I am forced to use API calls for several functions, one of which is ReadFile. The reason is because the framework seems to use ANSI versions of all API functions, limiting the MAX_PATH for filenames to about 260 chars for everything in System.IO. API Wide functions (Unicode) do not have this limitation.

    I am having issues implementing the ReadFile function. I seem to only get various exceptions whenever I try to use it. It's declared as follows:

    <DllImport("ker nel32", EntryPoint:="Re adFile", CharSet:=CharSe t.Unicode, SetLastError:=T rue, _
    ExactSpelling:= True, CallingConventi on:=CallingConv ention.StdCall) _
    Shared Function ReadFile(ByVal hFile As Integer, ByRef lpBuffer As Object, _
    ByVal nNumberOfBytesT oRead As Integer, ByRef lpNumberOfBytes Read As Integer, _
    ByRef lpOverlapped As OVERLAPPED) As Integer

    End Function

    I believe the issue is with the lpBuffer arguement. It's an <outarguement that basically populates a buffer with the bytes read from the file. In C++ its declared as a long void pointer.

    I have tried various ways of using this function, passing various types of variables to it. There is always an exception though. For example, this implementation throws an System.Executio nEngineExceptio n exception.

    Dim cBuff(4096) as Char
    for i = 0 to cBuff.Length -1
    cBuff(i) = " "
    Next
    iRetVal = ReadFile(hFlHan dle, cBuff, 100, iBytesRead, OL)

    OL, iBytesRead, and hFlHandle are all declared eariler. The hFlHandle is valid.

    When I use a string for the buffer, I get the "System.Runtime .InteropService s.InvalidOleVar iantTypeExcepti on: Specified OLE variant is invalid" exception.

    Dim sBuffer as String = space(100)
    iRetVal = ReadFile(hFlHan dle, sBufferPtr, sBuffer.Length, iBytesRead, OL)

    I am not entirely sure what the problem is. I must be missing something. Has anyone used the ReadFile function (or anything that requires an LPVOID passed to it) in VB.NET? Can anyone shine some light on this?

    Thanks,

    Dmitry
  • mayayana

    #2
    Re: Issues with API function ReadFile


    This group (winapi) is for VB (mainly v. 5 and 6). Your post
    is about VB.Net. VB and VB.Net are entirely different, so a
    VB answer would leave you even more confused. The winapi
    group should be dropped from your postings.

    For VB.Net you can also try the groups below, or any other
    group with "dotnet" or "vsnet" in the name. If it doesn't
    include "dotnet" then it's not .Net.

    microsoft.publi c.dotnet.genera l
    microsoft.publi c.dotnet.langua ges.vb
    microsoft.publi c.vsnet.general



    Comment

    • Tom Shelton

      #3
      Re: Issues with API function ReadFile

      On 2008-09-26, Ketchup <ketchup@ketchu p.comwrote:
      This is a multi-part message in MIME format.
      >
      ------=_NextPart_000_ 0006_01C91FB5.4 9F3B5E0
      Content-Type: text/plain;
      charset="iso-8859-1"
      Content-Transfer-Encoding: quoted-printable
      >
      Hello everyone,
      >
      I have been stuck with this problem for quite some time now. I am =
      working in VB.NET, using framework 1.0. I have to keep the =
      compatibility down to the original .NET framework as much as possible. =
      I am forced to use API calls for several functions, one of which is =
      ReadFile. The reason is because the framework seems to use ANSI =
      versions of all API functions, limiting the MAX_PATH for filenames to =
      about 260 chars for everything in System.IO. API Wide functions =
      (Unicode) do not have this limitation. =20
      >
      Sorry - but, the framework does use the Wide functions. Even the W functions,
      do not allow paths greater the MAX_PATH (defined as 260), by default. To get
      the full path support, you have to prefix the path with \\?\.

      Unfortunately, you can't just do that in .NET - the framework doesn't allow
      it. The main reason for that is that not all W functions actually honor the
      \\?\ prefix.

      I am having issues implementing the ReadFile function. I seem to only =
      get various exceptions whenever I try to use it. It's declared as =
      follows:
      >
      ><DllImport("ke rnel32", EntryPoint:=3D" ReadFile", =
      CharSet:=3DChar Set.Unicode, SetLastError:=3 DTrue, _
      ExactSpelling:= 3DTrue, CallingConventi on:=3DCallingCo nvention.StdCal l)=
      _
      Shared Function ReadFile(ByVal hFile As Integer, ByRef lpBuffer As =
      Object, _
      ByVal nNumberOfBytesT oRead As Integer, ByRef lpNumberOfBytes Read As =
      Integer, _
      ByRef lpOverlapped As OVERLAPPED) As Integer
      >
      End Function
      >
      Here is a simple example of using ReadFile:

      Option Strict On
      Option Explicit On

      Imports System.IO
      Imports System.Runtime. InteropServices
      Imports System.Text

      Module Module1
      Declare Auto Function ReadFile Lib "kernel32" ( _
      ByVal hFile As IntPtr, _
      ByVal lpBuffer() As Byte, _
      ByVal nNumberOfBytesT oRead As Integer, _
      ByRef lpNumberOfBytes Read As Integer, _
      ByVal lpOverLapped As IntPtr) As Integer

      '#define GENERIC_READ (0x80000000L)
      '#define GENERIC_WRITE (0x40000000L)
      '#define GENERIC_EXECUTE (0x20000000L)
      '#define GENERIC_ALL (0x10000000L)
      <Flags()_
      Enum DesiredAccess
      Read = &H80000000
      Write = &H40000000
      Execute = &H20000000
      All = &H10000000
      End Enum

      ' 0 0x00000000
      'FILE_SHARE_DEL ETE 0x00000004
      'FILE_SHARE_REA D 0x00000001
      'FILE_SHARE_WRI TE 0x00000002
      <Flags()_
      Enum ShareMode
      None = 0
      Read = &H1
      Write = &H2
      Delete = &H4
      End Enum

      Enum CreateMode
      CreateNew = 1
      CreateAlways = 2
      OpenExisting = 3
      OpenAlways = 4
      TruncateExistin g = 5
      End Enum

      ' HANDLE WINAPI CreateFile(
      ' __in LPCTSTR lpFileName,
      ' __in DWORD dwDesiredAccess ,
      ' __in DWORD dwShareMode,
      ' __in_opt LPSECURITY_ATTR IBUTES lpSecurityAttri butes,
      ' __in DWORD dwCreationDispo sition,
      ' __in DWORD dwFlagsAndAttri butes,
      ' __in_opt HANDLE hTemplateFile
      ');
      Declare Auto Function CreateFile Lib "kernel32" ( _
      ByVal lpFileName As String, _
      ByVal dwDesiredAccess As DesiredAccess, _
      ByVal dwShareMode As ShareMode, _
      ByVal lpSecurityAttri butes As IntPtr, _
      ByVal dwCreationDispo sition As CreateMode, _
      ByVal dwFlagsAndAttri butes As Integer, _
      ByVal hTemplateFile As IntPtr) As IntPtr

      Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Boolean

      Sub Main()

      Using f As StreamWriter = File.CreateText ("testing.tx t")
      f.WriteLine("He llo, you little monkey you!")
      End Using

      Dim hFile As IntPtr = CreateFile("tes ting.txt", DesiredAccess.R ead, ShareMode.None, IntPtr.Zero, CreateMode.Open Existing, 0, IntPtr.Zero)

      Dim buffer(256) As Byte
      Dim count As Integer

      ReadFile(hFile, buffer, buffer.Length, count, IntPtr.Zero)
      Console.WriteLi ne(Encoding.Def ault.GetString( buffer, 0, count))

      CloseHandle(hFi le)

      End Sub
      End Module

      --
      Tom Shelton

      Comment

      • Tom Dacon

        #4
        Re: Issues with API function ReadFile


        "mayayana" <mayaXXyana@rcX Xn.comwrote in message
        news:edsk8f9HJH A.4240@TK2MSFTN GP02.phx.gbl...
        >
        This group (winapi) is for VB (mainly v. 5 and 6).
        You new here?

        Tom Dacon
        Dacon Software Consulting


        Comment

        • Ken Halter

          #5
          Re: Issues with API function ReadFile

          "Tom Dacon" <tdacon@communi ty.nospamwrote in message
          news:e51ZwI$HJH A.2580@TK2MSFTN GP05.phx.gbl...
          >
          "mayayana" <mayaXXyana@rcX Xn.comwrote in message
          news:edsk8f9HJH A.4240@TK2MSFTN GP02.phx.gbl...
          >>
          > This group (winapi) is for VB (mainly v. 5 and 6).
          >
          You new here?
          More like the OP's new here. He's the one crossposting between languages.

          --
          Ken Halter
          Part time groupie


          Comment

          • Ketchup

            #6
            Re: Issues with API function ReadFile

            Tom,

            You are a genius! Switching the Object type to an array of Bytes and
            passing it ByVal instead of ByRef worked like a charm.

            Thank you!

            dmitry

            P.S. I didn't know that framework used W functions. It really would be
            nice if MS expanded support to the 32k char MAX_PATH of NTFS.


            "Tom Shelton" <tom_shelton@co mcastXXXXXXX.ne twrote in message
            news:OsadnQnbKf f-nUDVnZ2dnUVZ_gO dnZ2d@comcast.c om...
            On 2008-09-26, Ketchup <ketchup@ketchu p.comwrote:
            >This is a multi-part message in MIME format.
            >>
            >------=_NextPart_000_ 0006_01C91FB5.4 9F3B5E0
            >Content-Type: text/plain;
            >charset="iso-8859-1"
            >Content-Transfer-Encoding: quoted-printable
            >>
            >Hello everyone,
            >>
            >I have been stuck with this problem for quite some time now. I am =
            >working in VB.NET, using framework 1.0. I have to keep the =
            >compatibilit y down to the original .NET framework as much as possible.
            >=
            >I am forced to use API calls for several functions, one of which is =
            >ReadFile. The reason is because the framework seems to use ANSI =
            >versions of all API functions, limiting the MAX_PATH for filenames to =
            >about 260 chars for everything in System.IO. API Wide functions =
            >(Unicode) do not have this limitation. =20
            >>
            >
            Sorry - but, the framework does use the Wide functions. Even the W
            functions,
            do not allow paths greater the MAX_PATH (defined as 260), by default. To
            get
            the full path support, you have to prefix the path with \\?\.
            >
            Unfortunately, you can't just do that in .NET - the framework doesn't
            allow
            it. The main reason for that is that not all W functions actually honor
            the
            \\?\ prefix.
            >
            >
            >I am having issues implementing the ReadFile function. I seem to only
            >=
            >get various exceptions whenever I try to use it. It's declared as =
            >follows:
            >>
            >><DllImport("k ernel32", EntryPoint:=3D" ReadFile", =
            >CharSet:=3DCha rSet.Unicode, SetLastError:=3 DTrue, _
            >ExactSpelling: =3DTrue, CallingConventi on:=3DCallingCo nvention.StdCal l)=
            >_
            >Shared Function ReadFile(ByVal hFile As Integer, ByRef lpBuffer As =
            >Object, _
            >ByVal nNumberOfBytesT oRead As Integer, ByRef lpNumberOfBytes Read As =
            >Integer, _
            >ByRef lpOverlapped As OVERLAPPED) As Integer
            >>
            >End Function
            >>
            >
            Here is a simple example of using ReadFile:
            >
            Option Strict On
            Option Explicit On
            >
            Imports System.IO
            Imports System.Runtime. InteropServices
            Imports System.Text
            >
            Module Module1
            Declare Auto Function ReadFile Lib "kernel32" ( _
            ByVal hFile As IntPtr, _
            ByVal lpBuffer() As Byte, _
            ByVal nNumberOfBytesT oRead As Integer, _
            ByRef lpNumberOfBytes Read As Integer, _
            ByVal lpOverLapped As IntPtr) As Integer
            >
            '#define GENERIC_READ (0x80000000L)
            '#define GENERIC_WRITE (0x40000000L)
            '#define GENERIC_EXECUTE (0x20000000L)
            '#define GENERIC_ALL (0x10000000L)
            <Flags()_
            Enum DesiredAccess
            Read = &H80000000
            Write = &H40000000
            Execute = &H20000000
            All = &H10000000
            End Enum
            >
            ' 0 0x00000000
            'FILE_SHARE_DEL ETE 0x00000004
            'FILE_SHARE_REA D 0x00000001
            'FILE_SHARE_WRI TE 0x00000002
            <Flags()_
            Enum ShareMode
            None = 0
            Read = &H1
            Write = &H2
            Delete = &H4
            End Enum
            >
            Enum CreateMode
            CreateNew = 1
            CreateAlways = 2
            OpenExisting = 3
            OpenAlways = 4
            TruncateExistin g = 5
            End Enum
            >
            ' HANDLE WINAPI CreateFile(
            ' __in LPCTSTR lpFileName,
            ' __in DWORD dwDesiredAccess ,
            ' __in DWORD dwShareMode,
            ' __in_opt LPSECURITY_ATTR IBUTES lpSecurityAttri butes,
            ' __in DWORD dwCreationDispo sition,
            ' __in DWORD dwFlagsAndAttri butes,
            ' __in_opt HANDLE hTemplateFile
            ');
            Declare Auto Function CreateFile Lib "kernel32" ( _
            ByVal lpFileName As String, _
            ByVal dwDesiredAccess As DesiredAccess, _
            ByVal dwShareMode As ShareMode, _
            ByVal lpSecurityAttri butes As IntPtr, _
            ByVal dwCreationDispo sition As CreateMode, _
            ByVal dwFlagsAndAttri butes As Integer, _
            ByVal hTemplateFile As IntPtr) As IntPtr
            >
            Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr)
            As Boolean
            >
            Sub Main()
            >
            Using f As StreamWriter = File.CreateText ("testing.tx t")
            f.WriteLine("He llo, you little monkey you!")
            End Using
            >
            Dim hFile As IntPtr = CreateFile("tes ting.txt", DesiredAccess.R ead,
            ShareMode.None, IntPtr.Zero, CreateMode.Open Existing, 0, IntPtr.Zero)
            >
            Dim buffer(256) As Byte
            Dim count As Integer
            >
            ReadFile(hFile, buffer, buffer.Length, count, IntPtr.Zero)
            Console.WriteLi ne(Encoding.Def ault.GetString( buffer, 0, count))
            >
            CloseHandle(hFi le)
            >
            End Sub
            End Module
            >
            --
            Tom Shelton

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: Issues with API function ReadFile

              Ken,

              We disagree, I find the crossposting from the OP complete correct when I
              had read his question.
              It could not have been better.

              Luckily Tom could help him. I don't know from what newsgroup, as Tom is
              active in more than one.

              jmo

              Cor

              "Ken Halter" <Ken_Halter@Use _Sparingly_Hotm ail.comschreef in bericht
              news:ez579M$HJH A.1304@TK2MSFTN GP02.phx.gbl...
              "Tom Dacon" <tdacon@communi ty.nospamwrote in message
              news:e51ZwI$HJH A.2580@TK2MSFTN GP05.phx.gbl...
              >>
              >"mayayana" <mayaXXyana@rcX Xn.comwrote in message
              >news:edsk8f9HJ HA.4240@TK2MSFT NGP02.phx.gbl.. .
              >>>
              >> This group (winapi) is for VB (mainly v. 5 and 6).
              >>
              >You new here?
              >
              More like the OP's new here. He's the one crossposting between languages.
              >
              --
              Ken Halter
              Part time groupie
              >

              Comment

              • =?Utf-8?B?RGF2aWQgVGhvbXBzb24=?=

                #8
                Re: Issues with API function ReadFile

                "Ketchup" wrote:
                ...
                >
                P.S. I didn't know that framework used W functions. It really would be
                nice if MS expanded support to the 32k char MAX_PATH of NTFS.
                >
                They would like to. Here is what they have been thinking.

                http://blogs.msdn.com/bclteam/archiv...-hamilton.aspx

                http://blogs.msdn.com/bclteam/archiv...-hamilton.aspx

                http://blogs.msdn.com/bclteam/archiv...-hamilton.aspx

                Regards,

                David

                Comment

                • Ketchup

                  #9
                  Re: Issues with API function ReadFile

                  David, thanks for the links. I hadn't realized that there are such issues
                  with the Windows API. I am guessing the MAX_PATH limitation is a throw
                  back to FAT file system days. In the computer forensics world, we can't
                  alter the original paths and must preserve all of the metadata. This is
                  why I am having to revert to API calls for most of my System.IO needs. At
                  this point, I am getting pretty comfortable with it and enjoying it as well
                  :)

                  The idea of moving away from string paths is interesting, as long as we will
                  still be able to produce a report with string paths that exceed the 260 char
                  limit. I guess I will wait an see.

                  thanks again!

                  "David Thompson" <jwdbt@communit y.nospamwrote in message
                  news:DD3E9515-4253-4A03-A4C3-2A7B7CF0FFA3@mi crosoft.com...
                  "Ketchup" wrote:
                  >
                  >...
                  >>
                  >P.S. I didn't know that framework used W functions. It really would be
                  >nice if MS expanded support to the 32k char MAX_PATH of NTFS.
                  >>
                  >
                  They would like to. Here is what they have been thinking.
                  >
                  http://blogs.msdn.com/bclteam/archiv...-hamilton.aspx
                  >
                  http://blogs.msdn.com/bclteam/archiv...-hamilton.aspx
                  >
                  http://blogs.msdn.com/bclteam/archiv...-hamilton.aspx
                  >
                  Regards,
                  >
                  David

                  Comment

                  • Tom Shelton

                    #10
                    Re: Issues with API function ReadFile

                    On 2008-09-26, Ketchup <ketchup@ketchu p.comwrote:
                    David, thanks for the links. I hadn't realized that there are such issues
                    with the Windows API. I am guessing the MAX_PATH limitation is a throw
                    back to FAT file system days. In the computer forensics world, we can't
                    alter the original paths and must preserve all of the metadata. This is
                    why I am having to revert to API calls for most of my System.IO needs. At
                    this point, I am getting pretty comfortable with it and enjoying it as well
                    >:)
                    >
                    The idea of moving away from string paths is interesting, as long as we will
                    still be able to produce a report with string paths that exceed the 260 char
                    limit. I guess I will wait an see.
                    >
                    thanks again!
                    >
                    Not sure if you caught it or not - but, from the information in those links,
                    you should be able to avoid the ReadFile calls. The trick is to simply use
                    CreateFile to open the file, and then create your stream from the file handle
                    returned. Here is my example reworked to eliminate the ReadFile and has the
                    \\?\ - it seems when you do this you must use the fully qualified names:

                    Option Strict On
                    Option Explicit On

                    Imports System.IO
                    Imports System.Runtime. InteropServices
                    Imports System.Text
                    Imports Microsoft.Win32 .SafeHandles

                    Module Module1
                    '#define GENERIC_READ (0x80000000L)
                    '#define GENERIC_WRITE (0x40000000L)
                    '#define GENERIC_EXECUTE (0x20000000L)
                    '#define GENERIC_ALL (0x10000000L)
                    <Flags()_
                    Enum DesiredAccess
                    Read = &H80000000
                    Write = &H40000000
                    Execute = &H20000000
                    All = &H10000000
                    End Enum

                    ' 0 0x00000000
                    'FILE_SHARE_DEL ETE 0x00000004
                    'FILE_SHARE_REA D 0x00000001
                    'FILE_SHARE_WRI TE 0x00000002
                    <Flags()_
                    Enum ShareMode
                    None = 0
                    Read = &H1
                    Write = &H2
                    Delete = &H4
                    End Enum

                    Enum CreateMode
                    CreateNew = 1
                    CreateAlways = 2
                    OpenExisting = 3
                    OpenAlways = 4
                    TruncateExistin g = 5
                    End Enum

                    ' HANDLE WINAPI CreateFile(
                    ' __in LPCTSTR lpFileName,
                    ' __in DWORD dwDesiredAccess ,
                    ' __in DWORD dwShareMode,
                    ' __in_opt LPSECURITY_ATTR IBUTES lpSecurityAttri butes,
                    ' __in DWORD dwCreationDispo sition,
                    ' __in DWORD dwFlagsAndAttri butes,
                    ' __in_opt HANDLE hTemplateFile
                    ');
                    Declare Auto Function CreateFile Lib "kernel32" ( _
                    ByVal lpFileName As String, _
                    ByVal dwDesiredAccess As DesiredAccess, _
                    ByVal dwShareMode As ShareMode, _
                    ByVal lpSecurityAttri butes As IntPtr, _
                    ByVal dwCreationDispo sition As CreateMode, _
                    ByVal dwFlagsAndAttri butes As Integer, _
                    ByVal hTemplateFile As IntPtr) As SafeFileHandle

                    Sub Main()

                    Dim hFile As SafeFileHandle = CreateFile( _
                    "\\?\c:\testing .txt", _
                    DesiredAccess.W rite, _
                    ShareMode.None, _
                    IntPtr.Zero, _
                    CreateMode.Crea teAlways, _
                    0, _
                    IntPtr.Zero)

                    Using f As StreamWriter = New StreamWriter(Ne w FileStream(hFil e, FileAccess.Writ e))
                    f.WriteLine("He llo, you little monkey you!")
                    End Using

                    hFile = CreateFile( _
                    "\\?\c:\testing .txt", _
                    DesiredAccess.R ead, _
                    ShareMode.None, _
                    IntPtr.Zero, _
                    CreateMode.Open Existing, _
                    0, _
                    IntPtr.Zero)

                    Using reader As New StreamReader(Ne w FileStream(hFil e, FileAccess.Read ))
                    Console.WriteLi ne(reader.ReadT oEnd())
                    End Using

                    End Sub
                    End Module

                    The nice thing about using the SafeFileHandle - is that you can check to see
                    if CreateFile returned an INVALID_HANDLE, like this:

                    If Not hFile.IsInvalid Then
                    ' do cool stuff with file handle
                    Else
                    Throw New Win32Exception (Marshal.GetLas tWin32Error())
                    End If

                    Thanks for asking this question! Now, I'll know what I'm doing if this ever
                    comes up for me :)

                    --
                    Tom Shelton

                    Comment

                    • mayayana

                      #11
                      Re: Issues with API function ReadFile

                      We disagree, I find the crossposting from the OP complete correct when I
                      had read his question.
                      It could not have been better.
                      microsoft.publi c.dotnet.langua ges.vb is fine
                      but the group microsoft.publi c.vb.winapi is a VB
                      (VB6 and lower) group. It doesn't matter that
                      it's an API question. VB and VB.Net are different
                      where the API is concerned, just as they're
                      different in other aspects. You're a regular on
                      the .Net groups. You should know which groups
                      are .Net and which are not.


                      Comment

                      • Ketchup

                        #12
                        Re: Issues with API function ReadFile

                        Tom, thank you again. I saw that at first, but I was so excited to get
                        ReadFile working, that I didn't even give a try. After looking at your
                        example, I see the benefits of doing it the way you suggested. One issue
                        I have is that SafeHandle seems to be introduced in framework 2.0. I am
                        working in 1.0. That shouldn't stop this approach from working however.
                        I should be able to use IntPtr as a handle to the file. There are 9
                        versions of the constructor for FileStream, several of which accept a IntPtr
                        for the handle.

                        I think that I will rewrite my code to use the System.IO.FileS tream classes.
                        Error handling is much easier with the framework.

                        thanks again!

                        "Tom Shelton" <tom_shelton@co mcastXXXXXXX.ne twrote in message
                        news:OIKdnXY205 Ny-kDVnZ2dnUVZ_jGd nZ2d@comcast.co m...
                        On 2008-09-26, Ketchup <ketchup@ketchu p.comwrote:
                        Not sure if you caught it or not - but, from the information in those
                        links,
                        you should be able to avoid the ReadFile calls. The trick is to simply
                        use
                        CreateFile to open the file, and then create your stream from the file
                        handle
                        returned. Here is my example reworked to eliminate the ReadFile and has
                        the
                        \\?\ - it seems when you do this you must use the fully qualified names:
                        >
                        Option Strict On
                        Option Explicit On
                        >
                        Imports System.IO
                        Imports System.Runtime. InteropServices
                        Imports System.Text
                        Imports Microsoft.Win32 .SafeHandles
                        >
                        Module Module1
                        '#define GENERIC_READ (0x80000000L)
                        '#define GENERIC_WRITE (0x40000000L)
                        '#define GENERIC_EXECUTE (0x20000000L)
                        '#define GENERIC_ALL (0x10000000L)
                        <Flags()_
                        Enum DesiredAccess
                        Read = &H80000000
                        Write = &H40000000
                        Execute = &H20000000
                        All = &H10000000
                        End Enum
                        >
                        ' 0 0x00000000
                        'FILE_SHARE_DEL ETE 0x00000004
                        'FILE_SHARE_REA D 0x00000001
                        'FILE_SHARE_WRI TE 0x00000002
                        <Flags()_
                        Enum ShareMode
                        None = 0
                        Read = &H1
                        Write = &H2
                        Delete = &H4
                        End Enum
                        >
                        Enum CreateMode
                        CreateNew = 1
                        CreateAlways = 2
                        OpenExisting = 3
                        OpenAlways = 4
                        TruncateExistin g = 5
                        End Enum
                        >
                        ' HANDLE WINAPI CreateFile(
                        ' __in LPCTSTR lpFileName,
                        ' __in DWORD dwDesiredAccess ,
                        ' __in DWORD dwShareMode,
                        ' __in_opt LPSECURITY_ATTR IBUTES lpSecurityAttri butes,
                        ' __in DWORD dwCreationDispo sition,
                        ' __in DWORD dwFlagsAndAttri butes,
                        ' __in_opt HANDLE hTemplateFile
                        ');
                        Declare Auto Function CreateFile Lib "kernel32" ( _
                        ByVal lpFileName As String, _
                        ByVal dwDesiredAccess As DesiredAccess, _
                        ByVal dwShareMode As ShareMode, _
                        ByVal lpSecurityAttri butes As IntPtr, _
                        ByVal dwCreationDispo sition As CreateMode, _
                        ByVal dwFlagsAndAttri butes As Integer, _
                        ByVal hTemplateFile As IntPtr) As SafeFileHandle
                        >
                        Sub Main()
                        >
                        Dim hFile As SafeFileHandle = CreateFile( _
                        "\\?\c:\testing .txt", _
                        DesiredAccess.W rite, _
                        ShareMode.None, _
                        IntPtr.Zero, _
                        CreateMode.Crea teAlways, _
                        0, _
                        IntPtr.Zero)
                        >
                        Using f As StreamWriter = New StreamWriter(Ne w FileStream(hFil e,
                        FileAccess.Writ e))
                        f.WriteLine("He llo, you little monkey you!")
                        End Using
                        >
                        hFile = CreateFile( _
                        "\\?\c:\testing .txt", _
                        DesiredAccess.R ead, _
                        ShareMode.None, _
                        IntPtr.Zero, _
                        CreateMode.Open Existing, _
                        0, _
                        IntPtr.Zero)
                        >
                        Using reader As New StreamReader(Ne w FileStream(hFil e,
                        FileAccess.Read ))
                        Console.WriteLi ne(reader.ReadT oEnd())
                        End Using
                        >
                        End Sub
                        End Module
                        >
                        The nice thing about using the SafeFileHandle - is that you can check to
                        see
                        if CreateFile returned an INVALID_HANDLE, like this:
                        >
                        If Not hFile.IsInvalid Then
                        ' do cool stuff with file handle
                        Else
                        Throw New Win32Exception (Marshal.GetLas tWin32Error())
                        End If
                        >
                        Thanks for asking this question! Now, I'll know what I'm doing if this
                        ever
                        comes up for me :)
                        >
                        --
                        Tom Shelton

                        Comment

                        • Ketchup

                          #13
                          Re: Issues with API function ReadFile

                          Yep, it works with a plain IntPtr! Excellent. Thanks everyone!

                          "Ketchup" <ketchup@ketchu p.comwrote in message
                          news:OrFTbSEIJH A.1364@TK2MSFTN GP04.phx.gbl...
                          Tom, thank you again. I saw that at first, but I was so excited to get
                          ReadFile working, that I didn't even give a try. After looking at your
                          example, I see the benefits of doing it the way you suggested. One
                          issue I have is that SafeHandle seems to be introduced in framework 2.0.
                          I am working in 1.0. That shouldn't stop this approach from working
                          however. I should be able to use IntPtr as a handle to the file. There
                          are 9 versions of the constructor for FileStream, several of which accept
                          a IntPtr for the handle.
                          >
                          I think that I will rewrite my code to use the System.IO.FileS tream
                          classes. Error handling is much easier with the framework.
                          >
                          thanks again!
                          >
                          "Tom Shelton" <tom_shelton@co mcastXXXXXXX.ne twrote in message
                          news:OIKdnXY205 Ny-kDVnZ2dnUVZ_jGd nZ2d@comcast.co m...
                          >On 2008-09-26, Ketchup <ketchup@ketchu p.comwrote:
                          >
                          >Not sure if you caught it or not - but, from the information in those
                          >links,
                          >you should be able to avoid the ReadFile calls. The trick is to simply
                          >use
                          >CreateFile to open the file, and then create your stream from the file
                          >handle
                          >returned. Here is my example reworked to eliminate the ReadFile and has
                          >the
                          >\\?\ - it seems when you do this you must use the fully qualified names:
                          >>
                          >Option Strict On
                          >Option Explicit On
                          >>
                          >Imports System.IO
                          >Imports System.Runtime. InteropServices
                          >Imports System.Text
                          >Imports Microsoft.Win32 .SafeHandles
                          >>
                          >Module Module1
                          > '#define GENERIC_READ (0x80000000L)
                          > '#define GENERIC_WRITE (0x40000000L)
                          > '#define GENERIC_EXECUTE (0x20000000L)
                          > '#define GENERIC_ALL (0x10000000L)
                          > <Flags()_
                          > Enum DesiredAccess
                          > Read = &H80000000
                          > Write = &H40000000
                          > Execute = &H20000000
                          > All = &H10000000
                          > End Enum
                          >>
                          > ' 0 0x00000000
                          > 'FILE_SHARE_DEL ETE 0x00000004
                          > 'FILE_SHARE_REA D 0x00000001
                          > 'FILE_SHARE_WRI TE 0x00000002
                          > <Flags()_
                          > Enum ShareMode
                          > None = 0
                          > Read = &H1
                          > Write = &H2
                          > Delete = &H4
                          > End Enum
                          >>
                          > Enum CreateMode
                          > CreateNew = 1
                          > CreateAlways = 2
                          > OpenExisting = 3
                          > OpenAlways = 4
                          > TruncateExistin g = 5
                          > End Enum
                          >>
                          > ' HANDLE WINAPI CreateFile(
                          > ' __in LPCTSTR lpFileName,
                          > ' __in DWORD dwDesiredAccess ,
                          > ' __in DWORD dwShareMode,
                          > ' __in_opt LPSECURITY_ATTR IBUTES lpSecurityAttri butes,
                          > ' __in DWORD dwCreationDispo sition,
                          > ' __in DWORD dwFlagsAndAttri butes,
                          > ' __in_opt HANDLE hTemplateFile
                          > ');
                          > Declare Auto Function CreateFile Lib "kernel32" ( _
                          > ByVal lpFileName As String, _
                          > ByVal dwDesiredAccess As DesiredAccess, _
                          > ByVal dwShareMode As ShareMode, _
                          > ByVal lpSecurityAttri butes As IntPtr, _
                          > ByVal dwCreationDispo sition As CreateMode, _
                          > ByVal dwFlagsAndAttri butes As Integer, _
                          > ByVal hTemplateFile As IntPtr) As SafeFileHandle
                          >>
                          > Sub Main()
                          >>
                          > Dim hFile As SafeFileHandle = CreateFile( _
                          >"\\?\c:\testin g.txt", _
                          >DesiredAccess. Write, _
                          >ShareMode.None , _
                          >IntPtr.Zero, _
                          >CreateMode.Cre ateAlways, _
                          >0, _
                          >IntPtr.Zero)
                          >>
                          > Using f As StreamWriter = New StreamWriter(Ne w FileStream(hFil e,
                          >FileAccess.Wri te))
                          > f.WriteLine("He llo, you little monkey you!")
                          > End Using
                          >>
                          > hFile = CreateFile( _
                          >"\\?\c:\testin g.txt", _
                          >DesiredAccess. Read, _
                          >ShareMode.None , _
                          >IntPtr.Zero, _
                          >CreateMode.Ope nExisting, _
                          >0, _
                          >IntPtr.Zero)
                          >>
                          > Using reader As New StreamReader(Ne w FileStream(hFil e,
                          >FileAccess.Rea d))
                          > Console.WriteLi ne(reader.ReadT oEnd())
                          > End Using
                          >>
                          > End Sub
                          >End Module
                          >>
                          >The nice thing about using the SafeFileHandle - is that you can check to
                          >see
                          >if CreateFile returned an INVALID_HANDLE, like this:
                          >>
                          >If Not hFile.IsInvalid Then
                          >' do cool stuff with file handle
                          >Else
                          >Throw New Win32Exception (Marshal.GetLas tWin32Error())
                          >End If
                          >>
                          >Thanks for asking this question! Now, I'll know what I'm doing if this
                          >ever
                          >comes up for me :)
                          >>
                          >--
                          >Tom Shelton
                          >
                          >

                          Comment

                          • Thorsten Albers

                            #14
                            Re: Issues with API function ReadFile

                            Cor Ligthert[MVP] <notmyfirstname @planet.nlschri eb im Beitrag
                            <9A9EBE7B-5A8D-4685-B89F-C01D48690D01@mi crosoft.com>...
                            We disagree, I find the crossposting from the OP complete correct when I
                            had read his question.
                            It could not have been better.
                            >
                            Luckily Tom could help him. I don't know from what newsgroup, as Tom is
                            active in more than one.
                            As far as I can see Tom knows it better and has removed
                            microsoft.publi c.vb.winapi from the list of recipients of the answer which
                            seem to talk about. The only posting from Tom which made its way through to
                            m.p.vb.winapi is his "You new here?" question...

                            --
                            ----------------------------------------------------------------------
                            Thorsten Albers albers(a)uni-freiburg.de
                            ----------------------------------------------------------------------

                            Comment

                            • Cor Ligthert[MVP]

                              #15
                              Re: Issues with API function ReadFile

                              Yea I am sure that Tom knows,

                              However why should Ketchup knows it, not everybody is as good as you.

                              Bye

                              Cor

                              "Thorsten Albers" <albersRE@MOVEu ni-freiburg.deschr eef in bericht
                              news:01c920b4$1 55899e0$af01a8c 0@Router...
                              Cor Ligthert[MVP] <notmyfirstname @planet.nlschri eb im Beitrag
                              <9A9EBE7B-5A8D-4685-B89F-C01D48690D01@mi crosoft.com>...
                              >We disagree, I find the crossposting from the OP complete correct when I
                              >
                              >had read his question.
                              >It could not have been better.
                              >>
                              >Luckily Tom could help him. I don't know from what newsgroup, as Tom is
                              >active in more than one.
                              >
                              As far as I can see Tom knows it better and has removed
                              microsoft.publi c.vb.winapi from the list of recipients of the answer which
                              seem to talk about. The only posting from Tom which made its way through
                              to
                              m.p.vb.winapi is his "You new here?" question...
                              >
                              --
                              ----------------------------------------------------------------------
                              Thorsten Albers albers(a)uni-freiburg.de
                              ----------------------------------------------------------------------
                              >

                              Comment

                              Working...