[newbie] populate byte array using p/invoke and ReadFile

E

Einar Høst

I'm using the Win32 ReadFile function to read data from a tape. I've exposed
an unsafe version of ReadFile with this signature:

static extern unsafe bool ReadFile(IntPtr hFile, void* lpBuffer, uint
nNumberOfBytesToRead, IntPtr lpNumberOfBytesRead, IntPtr lpOverlapped);

which works just fine. However, I'd like to expose a safe version instead,
with a signature like:

static extern bool ReadFile(IntPtr hFile, IntPtr lpBuffer, uint
nNumberOfBytesToRead, IntPtr lpNumberOfBytesRead, IntPtr lpOverlapped);

The problem is how to marshal the managed byte array in a way that allows it
to be populated by ReadFile? I've been looking at the samples provided by
Sonja Keserovic, but I don't quite understand what to do...

Any help would be much appreciated!

Regards,
Einar
 
L

Lasse Vågsæther Karlsen

I'm using the Win32 ReadFile function to read data from a tape. I've
exposed an unsafe version of ReadFile with this signature:

static extern unsafe bool ReadFile(IntPtr hFile, void* lpBuffer, uint
nNumberOfBytesToRead, IntPtr lpNumberOfBytesRead, IntPtr
lpOverlapped);

which works just fine. However, I'd like to expose a safe version
instead, with a signature like:

static extern bool ReadFile(IntPtr hFile, IntPtr lpBuffer, uint
nNumberOfBytesToRead, IntPtr lpNumberOfBytesRead, IntPtr
lpOverlapped);

The problem is how to marshal the managed byte array in a way that
allows it to be populated by ReadFile? I've been looking at the
samples provided by Sonja Keserovic, but I don't quite understand what
to do...

Any help would be much appreciated!

Regards,
Einar

how about:

[DllImport(...)]
public static extern Boolean ReadFile(IntPtr hFile, Byte[] lpBuffer,
UInt32 nNumberOfBytesToRead, ref UInt32 lpNumberOfBytesRead,
IntPtr lpOverlapped);

I think that should cover what you want.

Just make sure you allocate the Byte array with the right size before
passing it to the function.
 
E

Einar Høst

Lasse Vågsæther Karlsen said:
I'm using the Win32 ReadFile function to read data from a tape. I've
exposed an unsafe version of ReadFile with this signature:

static extern unsafe bool ReadFile(IntPtr hFile, void* lpBuffer, uint
nNumberOfBytesToRead, IntPtr lpNumberOfBytesRead, IntPtr
lpOverlapped);

which works just fine. However, I'd like to expose a safe version
instead, with a signature like:

static extern bool ReadFile(IntPtr hFile, IntPtr lpBuffer, uint
nNumberOfBytesToRead, IntPtr lpNumberOfBytesRead, IntPtr
lpOverlapped);

The problem is how to marshal the managed byte array in a way that
allows it to be populated by ReadFile? I've been looking at the
samples provided by Sonja Keserovic, but I don't quite understand what
to do...

Any help would be much appreciated!

Regards,
Einar

how about:

[DllImport(...)]
public static extern Boolean ReadFile(IntPtr hFile, Byte[] lpBuffer,
UInt32 nNumberOfBytesToRead, ref UInt32 lpNumberOfBytesRead,
IntPtr lpOverlapped);

I think that should cover what you want.

Just make sure you allocate the Byte array with the right size before
passing it to the function.

Wonderful, it worked like a charm! Thanks!
 

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