473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

marshalling a byte array to an unmanaged unsigned char*

I have a managed DLL that I've used to expose a C++ class. One of the
functions in this class reads a line out of a file into a buffer of type
"unsigned char*". Since all the data is just straight byte data (that is,
I'm concerned with individual bits packed into the bytes), I'd like to put
that data into a byte array that's passed from the C# app to the DLL
function.

In the past, I've been able to marshal strings to character strings without
much trouble. But marshaling this byte array, which, for all intensive
purposes, holds exactly the same type and style of data as an unsigned char
array would in C++, has proven to be a ridiculously difficult experience.

Anyone got any ideas?

Lee Crabtree
Nov 17 '05 #1
3 12196
To copy data from the file directly to the byte array , use code similar to
the following :

byte[] arr = new byte[512];
fixed (byte* pArr = arr)
{
// use pArr here...
}

This is only allowed in an unsafe context. Basically the byte array is
pinned (fixed) in memory and then accessed as a buffer.

-Atul, Sky Software http://www.ssware.com
Shell MegaPack For ActiveX & .Net - Windows Explorer Like Shell UI Controls


"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:Os**************@TK2MSFTNGP15.phx.gbl...
I have a managed DLL that I've used to expose a C++ class. One of the
functions in this class reads a line out of a file into a buffer of type
"unsigned char*". Since all the data is just straight byte data (that is,
I'm concerned with individual bits packed into the bytes), I'd like to put
that data into a byte array that's passed from the C# app to the DLL
function.

In the past, I've been able to marshal strings to character strings
without much trouble. But marshaling this byte array, which, for all
intensive purposes, holds exactly the same type and style of data as an
unsigned char array would in C++, has proven to be a ridiculously
difficult experience.

Anyone got any ideas?

Lee Crabtree

Nov 17 '05 #2
To copy data from the file directly to the byte array , use code similar to
the following :

byte[] arr = new byte[512];
fixed (byte* pArr = arr)
{
// use pArr here...
}

This is only allowed in an unsafe context. Basically the byte array is
pinned (fixed) in memory and then accessed as a buffer.

-Atul, Sky Software http://www.ssware.com
Shell MegaPack For ActiveX & .Net - Windows Explorer Like Shell UI Controls


"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:Os**************@TK2MSFTNGP15.phx.gbl...
I have a managed DLL that I've used to expose a C++ class. One of the
functions in this class reads a line out of a file into a buffer of type
"unsigned char*". Since all the data is just straight byte data (that is,
I'm concerned with individual bits packed into the bytes), I'd like to put
that data into a byte array that's passed from the C# app to the DLL
function.

In the past, I've been able to marshal strings to character strings
without much trouble. But marshaling this byte array, which, for all
intensive purposes, holds exactly the same type and style of data as an
unsigned char array would in C++, has proven to be a ridiculously
difficult experience.

Anyone got any ideas?

Lee Crabtree

Nov 17 '05 #3

"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:Os**************@TK2MSFTNGP15.phx.gbl...
I have a managed DLL that I've used to expose a C++ class. One of the
functions in this class reads a line out of a file into a buffer of type
"unsigned char*". Since all the data is just straight byte data (that is,
I'm concerned with individual bits packed into the bytes), I'd like to put
that data into a byte array that's passed from the C# app to the DLL
function.

In the past, I've been able to marshal strings to character strings
without much trouble. But marshaling this byte array, which, for all
intensive purposes, holds exactly the same type and style of data as an
unsigned char array would in C++, has proven to be a ridiculously
difficult experience.

Anyone got any ideas?

Lee Crabtree


Not sure what problems you have with this, but it's rather simple.

In C#
//Following declaration tells the marshaler to pass the address of the
byte[] to C++ unmanaged function
[DllImport("unm.dll")]
private static extern double UnmFunc (byte[] val, int len);

// allocate array large enough to contain filedata
byte[] ar = new byte[xxx];
// pass the array reference and (optional) array length
UnmFunc(ar, ar.Length);
// upon return ar contains file data

In C++ you have two options, in both the first arg. is the address of a
buffer
1. address points to a unsigned char buffer with (optional) length len
__declspec(dllexport) void __stdcall func_c6 (unsigned char* d, int len);

2. address points to an unsigned char[] with length len
__declspec(dllexport) void __stdcall UnmFunc (unsigned char d[], int len);

Note: both 1 and 2 are threaten the same by the interop marshaler, only the
C code semantics differs.

Willy.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lee Crabtree | last post by:
I have a managed DLL that I've used to expose a C++ class. One of the functions in this class reads a line out of a file into a buffer of type "unsigned char*". Since all the data is just...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
1
by: | last post by:
Hi, Is there any good links for datatype interop? I need to pass some structure pointers into an unmanaged method and return char* etc but having some problems in my C++/CLI proxy class. I...
4
by: zon7mail | last post by:
I've two functions. One is managed and receives an array, and the other is unmanaged and receives a pointer, just like this class A { int Foo(unsigned char* m){}; ... } ref class B {
3
by: Beorne | last post by:
I have to pass a byte array as an input parameter to a function in a propertary dll. The c++ signature of the original function is the following: ---------- C++ ---------- int...
11
by: Daniel Bass | last post by:
Greetings! I'm trying to call this method in a c# app... SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE DeviceHandle, unsigned char *pData, long max_length); So far I've got...
1
by: Keaven Pineau | last post by:
Hello All, I am very new to C# since my job consist to design/develop CE drivers but a client wants to use my C API into a .NET envrironment. I have 2 functions I would like to get help with. ...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
2
by: =?Utf-8?B?QmFydE1hbg==?= | last post by:
Greetings, I am working on a project where the interface from the UI application (done in C#) requires that an image buffer be passed back as a byte array as defined by an interface. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.