Problems with Kernel32 ReadFile!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chua Wen Ching

    Problems with Kernel32 ReadFile!

    Hi,

    I have doubts. From p/invoke and most site, they recommend to use this.

    Example1:
    [DllImport("kern el32.dll")]
    static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer,
    uint nNumberOfBytesT oRead, out uint lpNumberOfBytes Read, IntPtr
    lpOverlapped);

    But I only want to read 1 byte at a time.

    Example2:
    [DllImport("kern el32.dll")]
    static extern bool ReadFile(IntPtr hFile, ref byte lpBuffer, uint
    nNumberOfBytesT oRead, ref uint lpNumberOfBytes Read, IntPtr lpOverlapped);

    Can i do this instead?

    If just say, no choice i have to stick to Example1, when i run that
    function, and only read lpBuffer[0], it does not return what i expected.

    But if only follow Example2, it will. But the only issue i have is,

    Sample snippet:

    for(;;)
    {
    // some processing

    bool test = ReadFile(...)..
    }

    so basically in this loop, it will read till 8 times of data...

    00 11 22 33 44 55 66 77 88 // just say this is what i expect it to give me

    but it give me instead

    00 11 22 33 44 55 66 00 01 // wrong! Sometimes i get this, and sometimes i
    get the above one.

    Why?

    I try to test on separate machine, or restart my machine. Sometimes i just
    get either the one with 88 99 or 00 01!

    I have an existing C SDK, and it is running smoothly. That is why i can
    compare whether my .NET SDK is working properly or not.

    Can anyone provide me any guideslines please?
    --
    Regards,
    Chua Wen Ching
    Visit us at http://www.necoders.com
  • Ollie Riches

    #2
    Re: Problems with Kernel32 ReadFile!


    "Chua Wen Ching" <chua_wen_ching @nospam.hotmail .com> wrote in message
    news:87185FB0-0EF7-4FD8-B734-09B0B2735587@mi crosoft.com...[color=blue]
    > Hi,
    >
    > I have doubts. From p/invoke and most site, they recommend to use this.
    >
    > Example1:
    > [DllImport("kern el32.dll")]
    > static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer,
    > uint nNumberOfBytesT oRead, out uint lpNumberOfBytes Read, IntPtr
    > lpOverlapped);
    >
    > But I only want to read 1 byte at a time.
    >
    > Example2:
    > [DllImport("kern el32.dll")]
    > static extern bool ReadFile(IntPtr hFile, ref byte lpBuffer, uint
    > nNumberOfBytesT oRead, ref uint lpNumberOfBytes Read, IntPtr lpOverlapped);
    >
    > Can i do this instead?
    >
    > If just say, no choice i have to stick to Example1, when i run that
    > function, and only read lpBuffer[0], it does not return what i expected.
    >
    > But if only follow Example2, it will. But the only issue i have is,
    >
    > Sample snippet:
    >
    > for(;;)
    > {
    > // some processing
    >
    > bool test = ReadFile(...)..
    > }
    >
    > so basically in this loop, it will read till 8 times of data...
    >
    > 00 11 22 33 44 55 66 77 88 // just say this is what i expect it to give me
    >
    > but it give me instead
    >
    > 00 11 22 33 44 55 66 00 01 // wrong! Sometimes i get this, and sometimes i
    > get the above one.
    >
    > Why?
    >
    > I try to test on separate machine, or restart my machine. Sometimes i just
    > get either the one with 88 99 or 00 01!
    >
    > I have an existing C SDK, and it is running smoothly. That is why i can
    > compare whether my .NET SDK is working properly or not.
    >
    > Can anyone provide me any guideslines please?
    > --
    > Regards,
    > Chua Wen Ching
    > Visit us at http://www.necoders.com[/color]


    Comment

    • Ollie Riches

      #3
      Re: Problems with Kernel32 ReadFile!

      why aren't you using the System.IO namespace ?


      --
      HTH

      Ollie Riches


      Disclaimer: Opinions expressed in this forum are my own, and not
      representative of my employer.
      I do not answer questions on behalf of my employer. I'm just a programmer
      helping programmers.
      "Chua Wen Ching" <chua_wen_ching @nospam.hotmail .com> wrote in message
      news:87185FB0-0EF7-4FD8-B734-09B0B2735587@mi crosoft.com...[color=blue]
      > Hi,
      >
      > I have doubts. From p/invoke and most site, they recommend to use this.
      >
      > Example1:
      > [DllImport("kern el32.dll")]
      > static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer,
      > uint nNumberOfBytesT oRead, out uint lpNumberOfBytes Read, IntPtr
      > lpOverlapped);
      >
      > But I only want to read 1 byte at a time.
      >
      > Example2:
      > [DllImport("kern el32.dll")]
      > static extern bool ReadFile(IntPtr hFile, ref byte lpBuffer, uint
      > nNumberOfBytesT oRead, ref uint lpNumberOfBytes Read, IntPtr lpOverlapped);
      >
      > Can i do this instead?
      >
      > If just say, no choice i have to stick to Example1, when i run that
      > function, and only read lpBuffer[0], it does not return what i expected.
      >
      > But if only follow Example2, it will. But the only issue i have is,
      >
      > Sample snippet:
      >
      > for(;;)
      > {
      > // some processing
      >
      > bool test = ReadFile(...)..
      > }
      >
      > so basically in this loop, it will read till 8 times of data...
      >
      > 00 11 22 33 44 55 66 77 88 // just say this is what i expect it to give me
      >
      > but it give me instead
      >
      > 00 11 22 33 44 55 66 00 01 // wrong! Sometimes i get this, and sometimes i
      > get the above one.
      >
      > Why?
      >
      > I try to test on separate machine, or restart my machine. Sometimes i just
      > get either the one with 88 99 or 00 01!
      >
      > I have an existing C SDK, and it is running smoothly. That is why i can
      > compare whether my .NET SDK is working properly or not.
      >
      > Can anyone provide me any guideslines please?
      > --
      > Regards,
      > Chua Wen Ching
      > Visit us at http://www.necoders.com[/color]


      Comment

      • Mattias Sjögren

        #4
        Re: Problems with Kernel32 ReadFile!

        [color=blue]
        > Can i do this instead?[/color]

        Yes

        [color=blue]
        >when i run that
        >function, and only read lpBuffer[0], it does not return what i expected.[/color]

        That's weird, it should work as well.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        Working...