By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

Unsafe code: Converting "byte *" to "[] byte"

P: n/a
I've converted "[] byte" to "byte *" at times, using 'unsafe' and
fixed { .... }, but the reverse does not seem to work.

In this case, a C++ DLL returns a byte * and a length. What is the best
way to convert these to straight C#-compatible straight "[] byte" arrays?

PS: The C++ DLL is actually managed and I have access to the source.
Perhaps there is a simpler syntax for doing the conversion there, and
returning a C# compatible array rather than a pointer?

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You'll need to create a byte array and copy the contents of the pointer to
it.

The Marshal class will enable you to do this.

byte[] bytes=new byte[length];
for(int i=0; i<length; i++)
bytes[i]=Marshal.ReadByte(fromPointer,i);

bytes now contains the copy of the memory pointed to by fromPointer.

Marshal.Copy goes the other way, from the byte array to the pointer. Shame
they couldn't have written a copyfrom method.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"_BNC" <_B**@nospam.org> wrote in message
news:ms********************************@4ax.com...
I've converted "[] byte" to "byte *" at times, using 'unsafe' and
fixed { .... }, but the reverse does not seem to work.

In this case, a C++ DLL returns a byte * and a length. What is the best
way to convert these to straight C#-compatible straight "[] byte" arrays?

PS: The C++ DLL is actually managed and I have access to the source.
Perhaps there is a simpler syntax for doing the conversion there, and
returning a C# compatible array rather than a pointer?

Nov 16 '05 #2

P: n/a

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
You'll need to create a byte array and copy the contents of the pointer to
it.

The Marshal class will enable you to do this.

byte[] bytes=new byte[length];
for(int i=0; i<length; i++)
bytes[i]=Marshal.ReadByte(fromPointer,i);

bytes now contains the copy of the memory pointed to by fromPointer.

Marshal.Copy goes the other way, from the byte array to the pointer. Shame
they couldn't have written a copyfrom method.


This has been taken care of in v2.0 using an overload of Marshal.Copy.

Willy.
Nov 16 '05 #3

P: n/a
[re turning 'byte *' into C# '[] byte'

On Thu, 9 Dec 2004 13:48:14 +0100, "Willy Denoyette [MVP]"
<wi*************@pandora.be> wrote:

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
You'll need to create a byte array and copy the contents of the pointer to
it.

The Marshal class will enable you to do this.

byte[] bytes=new byte[length];
for(int i=0; i<length; i++)
bytes[i]=Marshal.ReadByte(fromPointer,i);

bytes now contains the copy of the memory pointed to by fromPointer.

Marshal.Copy goes the other way, from the byte array to the pointer. Shame
they couldn't have written a copyfrom method.


This has been taken care of in v2.0 using an overload of Marshal.Copy.

Willy.


The horror. Runtime is bad enough already. Is there no way to do this
without copying bytes? I'm going through both an unmanaged C++ layer and
then a managed C++ layer before it gets to C#. Is there a simple way to
cast this somewhere in the C++ layers?

I'm also not sure why I'd have to use 'Marshall' to copy the array. If
I've already got a pointer and length, I could just do the copy directly,
right? OK, I'm probably being dense.
Nov 16 '05 #4

P: n/a

"_BNC" <_B**@nospam.org> wrote in message
news:hq********************************@4ax.com...
[re turning 'byte *' into C# '[] byte'

On Thu, 9 Dec 2004 13:48:14 +0100, "Willy Denoyette [MVP]"
<wi*************@pandora.be> wrote:

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl.. .
You'll need to create a byte array and copy the contents of the pointer
to
it.

The Marshal class will enable you to do this.

byte[] bytes=new byte[length];
for(int i=0; i<length; i++)
bytes[i]=Marshal.ReadByte(fromPointer,i);

bytes now contains the copy of the memory pointed to by fromPointer.

Marshal.Copy goes the other way, from the byte array to the pointer.
Shame
they couldn't have written a copyfrom method.


This has been taken care of in v2.0 using an overload of Marshal.Copy.

Willy.


The horror. Runtime is bad enough already. Is there no way to do this
without copying bytes? I'm going through both an unmanaged C++ layer and
then a managed C++ layer before it gets to C#. Is there a simple way to
cast this somewhere in the C++ layers?

I'm also not sure why I'd have to use 'Marshall' to copy the array. If
I've already got a pointer and length, I could just do the copy directly,
right? OK, I'm probably being dense.


There's no need to copy when passing managed types between C# and Managed
C++. You only should copy from managed to unmanaged memory and the other way
arround.

Willy.
Nov 16 '05 #5

P: n/a
I have a similar situation. C# application, unmanaged com object
(directshow filter). Using a callback function I would like to get a copy of
the data that filter has.

I've been trying:

//COM
typedef HRESULT (* MYCALLBACK) ( IMediaSample *pSample, long lSize );
HRESULT MyClass::Transform(IMediaSample *pMediaSample)
{
CheckPointer(pMediaSample,E_POINTER);
if(m_callback)
{
m_callback(pDestBuffer, lSourceSize);
}
return S_OK;
}

//C#
public delegate void CallBackDelegate(IntPtr pSample, int dataLength);
public void CallbackFunction(IntPtr pSample, int dataLength)
{
byte [] buffer = new byte [dataLength];
Marshal.Copy(pSample,buffer,0,dataLength);
}

It crashes on the Marshal.Copy. Any suggestions on how to accomplish this?
I have the source for both the managed and unmanaged sides so I can change
both if necessay.
"Willy Denoyette [MVP]" wrote:

"_BNC" <_B**@nospam.org> wrote in message
news:hq********************************@4ax.com...
[re turning 'byte *' into C# '[] byte'

On Thu, 9 Dec 2004 13:48:14 +0100, "Willy Denoyette [MVP]"
<wi*************@pandora.be> wrote:

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl.. .
You'll need to create a byte array and copy the contents of the pointer
to
it.

The Marshal class will enable you to do this.

byte[] bytes=new byte[length];
for(int i=0; i<length; i++)
bytes[i]=Marshal.ReadByte(fromPointer,i);

bytes now contains the copy of the memory pointed to by fromPointer.

Marshal.Copy goes the other way, from the byte array to the pointer.
Shame
they couldn't have written a copyfrom method.
This has been taken care of in v2.0 using an overload of Marshal.Copy.

Willy.


The horror. Runtime is bad enough already. Is there no way to do this
without copying bytes? I'm going through both an unmanaged C++ layer and
then a managed C++ layer before it gets to C#. Is there a simple way to
cast this somewhere in the C++ layers?

I'm also not sure why I'd have to use 'Marshall' to copy the array. If
I've already got a pointer and length, I could just do the copy directly,
right? OK, I'm probably being dense.


There's no need to copy when passing managed types between C# and Managed
C++. You only should copy from managed to unmanaged memory and the other way
arround.

Willy.

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.