473,471 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
5 4360
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

"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
[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

"_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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
2
by: Chris Wood | last post by:
In C#, I am calling a method implemented in Managed C++ that returns an array of booleans. This method in turn calls unto unmanaged C++ code that returns an unsigned byte array, which is...
3
by: tlemcenvisit | last post by:
Hello I'm looking for the VC++.NET equivalent of "unsafe" instruction. unsafe is a VC#.NET instruction Thanks
2
by: shengmin.ruan | last post by:
i need to use the native code(unmanaged code),and i have to use void*,so i use the "unsafe" symbol then i meet the trouble c++code: SC_InitAsk sca; memset(&sca, 0, sizeof(sca));...
1
by: Ben | last post by:
I need to use some pointer-based "unsafe" code, I'm not sure if this option is still available. I selected "Always show solution" after I read Peter Bromberg's tips, but I don't see the option to...
6
by: Nathan Sokalski | last post by:
I recently converted some ASP.NET 1.1 projects of mine, created with Visual Studio .NET 2003, to Web Application Projects in Visual Studio .NET 2005 so that I could use ASP.NET 2.0 (All my ASP.NET...
6
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now,...
2
by: stainless | last post by:
I know this is probably simple but I cannot find a method of converting a date string into a format that matches the DatePicker format in C# eg string "20080131" converted to "31 January 2008" ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.