Problems with Kernel32 ReadFile!

G

Guest

Hi,

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

Example1:
[DllImport("kernel32.dll")]
static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer,
uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr
lpOverlapped);

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

Example2:
[DllImport("kernel32.dll")]
static extern bool ReadFile(IntPtr hFile, ref byte lpBuffer, uint
nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, 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?
 
O

Ollie Riches

Chua Wen Ching said:
Hi,

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

Example1:
[DllImport("kernel32.dll")]
static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer,
uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr
lpOverlapped);

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

Example2:
[DllImport("kernel32.dll")]
static extern bool ReadFile(IntPtr hFile, ref byte lpBuffer, uint
nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, 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?
 
O

Ollie Riches

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


--
HTH

Ollie Riches
http://www.phoneanalyser.net

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.
 
M

Mattias Sjögren

Can i do this instead?
Yes


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

That's weird, it should work as well.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top