how to pass a byte array from managed code ?

  • Thread starter =?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=
  • Start date
?

=?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=

i'v got:

[DllImport("KERNEL32.DLL")]
public static extern int ReadFile(
IntPtr hFile,
IntPtr lpBuffer,
uint nNumberOfBytesToRead,
ref uint lpNumberOfBytesRead,
uint lpOverlapped);

to map:

BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);

my call in C# is:

byte[] buf = new byte[512];
uint NumberOfBytesRead;
Win32.ReadFile(handle, buf, (uint)buf.Length, ref NumberOfBytesRead, 0);

but of course, buf is not a IntPtr

so, my questions are:
- what is the best signature for ReadFile ?
- id byte[] is adequate for LPVOID ?
 
G

Guest

Hi,

I don't have a lot of time today but I give you a tip that maybe it helps you,

you can use System.InteropServices.Marshal to cast unmanaged to managed and
viceversa.

I think there is a function called Marshal.StructureToPtr()

hope this helps, otherwise tomorrow I can answer with my VS in front of me.

best regards
 
?

=?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=

ok, i've found, it was just evident:

[DllImport("KERNEL32.DLL")]
ublic static extern int ReadFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToRead,
ref uint lpNumberOfBytesRead,
uint lpOverlapped
);
 

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