473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problems with Kernel32 ReadFile!

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?
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com
Nov 16 '05 #1
3 5781

"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:87**********************************@microsof t.com...
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?
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com

Nov 16 '05 #2
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.
"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:87**********************************@microsof t.com...
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?
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com

Nov 16 '05 #3
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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: c | last post by:
I have the following very simple script which works on my desktop (XP-PRO & IIS5.1) but does not work on my laptop with the same config. It also works on other configurations without a problem. I...
4
by: Phil Grimpo | last post by:
I had previously explained this problem in a different thread, but now that I have an IISState log, I figured I'd re-start the thred. My situation and the log are following... I have a very odd...
0
by: Mustafa Ahmad Malik | last post by:
Hello, I have wrapped the Win32 pipes function in C#. I have created named pipe with PIPE_WAIT in pipemode parameter like this pipehandle = CreateNamedPipe(_pipeName, PIPE_ACCESS_DUPLEX,...
3
by: Shawn August | last post by:
Hello: I am converting a working VB6 program to C#. During testing of the C# version, I noticed the ReadFile API is crashing. The parameters going into the this function are identical to the...
4
by: Jerry | last post by:
I am trying to read a physical sector off of the disk (the boot sector for drive C:) from C#. I have no problems doing it from a C/C++ application using the Win32 API CreateFile and ReadFile....
2
by: GTi | last post by:
StringBuilder text = new StringBuilder(lpNextSize+1, lpNextSize+1); int pNumberOfBytesRead = text.Capacity; try { result=win32.ReadFile(_Handle, text, text.Capacity, ref pNumberOfBytesRead, 0); }...
2
by: Schorschi | last post by:
Can't seemd to get ReadFile API to work! Returns invalid handle error? =========================================================================== Ok, the visual basic gurus, help! The...
4
by: Eric Renken | last post by:
I am trying to do an Overlapped ReadFile on a HID device and it just isn't working for me. The WaitForSingleObject keeps giving me an error "The system cannot find the file specified." This...
15
by: Ketchup | last post by:
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.