Making an IntPtr point to a byte array

A

Andrew Falanga

Hi,

How would I do the following (doesn't work but shows what I want to
do)?

byte[] myByteArray = new byte[1024];

// fill the array with what I need

IntPtr pByteArray = new IntPtr(myByteArray);


I have to use some library functions that take IntPtr's. The data I
need is being prepared in the byte arrays. How do I get an IntPtr to
point to it?

Thanks,
Andy
 
A

Arne Vajhøj

Andrew said:
How would I do the following (doesn't work but shows what I want to
do)?

byte[] myByteArray = new byte[1024];

// fill the array with what I need

IntPtr pByteArray = new IntPtr(myByteArray);

I have to use some library functions that take IntPtr's. The data I
need is being prepared in the byte arrays. How do I get an IntPtr to
point to it?

Several approaches:

1) drop IntPtr and specify byte[] in the extern method

2) unsafe block fixed block with pointer and simple cast

3) allocate memory pointed to by IntPtr with Marshal.AllocHGlobal
and copy data to it with Marshal.Copy (remember to call
Marshal.FreeHGlobal)

Arne
 
A

Andrew Falanga

Andrew said:
How would I do the following (doesn't work but shows what I want to
do)?
byte[] myByteArray = new byte[1024];
// fill the array with what I need
IntPtr pByteArray = new IntPtr(myByteArray);
I have to use some library functions that take IntPtr's.  The data I
need is being prepared in the byte arrays.  How do I get an IntPtr to
point to it?

Several approaches:

1) drop IntPtr and specify byte[] in the extern method

2) unsafe block fixed block with pointer and simple cast

3) allocate memory pointed to by IntPtr with Marshal.AllocHGlobal
    and copy data to it with Marshal.Copy (remember to call
    Marshal.FreeHGlobal)

Arne

Thanks both Pete and Arne for the responses. The dilemma is this, the
code I'm writing uses an unmanaged DLL (a C program) and the C#
reference I'm calling into takes the IntPtr. I wrote the C dll and
that takes a byte[]. So, in my code, I'm making the data fit into a
byte array but then have to get a pointer to that memory to satisfy
the other library I'm linking with. The other library I did not
write. I have little choice about what I pass this function. Hence
the reason why I'd like to know how to make an IntPtr point to the
byte array.

Andy
 
A

Arne Vajhøj

Andrew said:
Andrew said:
How would I do the following (doesn't work but shows what I want to
do)?
byte[] myByteArray = new byte[1024];
// fill the array with what I need
IntPtr pByteArray = new IntPtr(myByteArray);
I have to use some library functions that take IntPtr's. The data I
need is being prepared in the byte arrays. How do I get an IntPtr to
point to it?
Several approaches:

1) drop IntPtr and specify byte[] in the extern method

2) unsafe block fixed block with pointer and simple cast

3) allocate memory pointed to by IntPtr with Marshal.AllocHGlobal
and copy data to it with Marshal.Copy (remember to call
Marshal.FreeHGlobal)

Thanks both Pete and Arne for the responses. The dilemma is this, the
code I'm writing uses an unmanaged DLL (a C program) and the C#
reference I'm calling into takes the IntPtr. I wrote the C dll and
that takes a byte[]. So, in my code, I'm making the data fit into a
byte array but then have to get a pointer to that memory to satisfy
the other library I'm linking with. The other library I did not
write. I have little choice about what I pass this function. Hence
the reason why I'd like to know how to make an IntPtr point to the
byte array.

Then #2 or #3.

Arne
 

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