473,508 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting an array to a buffer

Hi.

I'm developing an application using managed C++. My application has to use a
DLL developed in standard C++. The mentioned DLL implements a class for file
operations. There is a class method uses a pointer to a buffer (e.g. BYTE
*pBuf = new BYTE [1024]) as input / output.

My program has to serialize a 'ref class MyClass' and store it in the file
using the mentioned DLL API. Another invocation of the program has to
deserialize the prior stored data.

I'm able to serialize the class using BinaryFormatter() and MemoryStream().
My problem is that serialized data are stored in an array (see below).
Simillary the deserialization uses an array as input. The point is that I'm
unable to extract the data buffer from the array.

Code:
----
MyClass^ myObj;
...
IFormatter^ formatter = gcnew BinaryFormatter();
MemoryStream^ msSer = gcnew MemoryStream();
formatter->Serialize(msSer, myObj);
msSer->Seek(0, SeekOrigin::Begin);
array<Byte>^ dataArray = gcnew array<Byte>(msSer->Length);
int dataArrayLen = msSer->Read(dataArray, 0, msSer->Length);
// -- I need to fill this buffer --
BYTE *pBuf = new BYTE [msSer->Length];

// -- This trick doesn't work of course --
pBuf = reinterpret_cast<BYTE *>(&dataArray);

MemoryStream^ msDeser = gcnew MemoryStream(dataArray, 0,
dataArray->Length, false);
MyClass^ myObjOut = (MyClass^) formatter->Deserialize(msDeser);
----

Thanks for help and best regards,
Damir Dezeljin
Oct 26 '06 #1
1 1582
Hi Damir,

"Damir Dezeljin" <De**@noemail.noemailwrote in message
news:07**********************************@microsof t.com...
Hi.

My program has to serialize a 'ref class MyClass' and store it in the file
using the mentioned DLL API. Another invocation of the program has to
deserialize the prior stored data.

I'm able to serialize the class using BinaryFormatter() and
MemoryStream().
My problem is that serialized data are stored in an array (see below).
Simillary the deserialization uses an array as input. The point is that
I'm
unable to extract the data buffer from the array.

Code:
----
MyClass^ myObj;
...
IFormatter^ formatter = gcnew BinaryFormatter();
MemoryStream^ msSer = gcnew MemoryStream();
formatter->Serialize(msSer, myObj);
msSer->Seek(0, SeekOrigin::Begin);
array<Byte>^ dataArray = gcnew array<Byte>(msSer->Length);
int dataArrayLen = msSer->Read(dataArray, 0, msSer->Length);
// -- I need to fill this buffer --
BYTE *pBuf = new BYTE [msSer->Length];

// -- This trick doesn't work of course --
pBuf = reinterpret_cast<BYTE *>(&dataArray);
pin_ptr<BYTEp = &dataArray[0]; // get a native pointer to byte array
int cb = (long)msSer->Length; // won't work correctly for arrays larger than
2GB
memcpy_s(pBuf, cb, p, cb);

--
SvenC
Oct 27 '06 #2

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

Similar topics

5
13900
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
10277
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
4
5407
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
4
412
by: x | last post by:
converting 1944 to '1','9','4','4' how can I convert a number such as 1944 to a character array? thanks!
3
2328
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
2
1237
by: Eric Kinskofer | last post by:
Hi all, I'm a noobie to C# and am having probs with tyring to convert some legacy C++ code to C#. I've attached some psuedo code to try and explain the problem: ---code-- int headersize,...
1
12373
by: Darrel | last post by:
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster...
5
4158
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
2
11563
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public...
0
7227
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
7331
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
7391
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...
0
7501
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...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3204
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
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.