473,467 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to pass binary data from C# stream to fcn in Managed C++

I have a byte[] of binary data received from a NetworkStream (C# code) that I need to pass to the IWMWriter object in a DLL written in Managed extensions for C++ (since the Windows Media SDK is not usable in C#) whose WriteSample function takes a byte* parameter

Do I need to marshal data? I thought I did, so I ran into alot of issues with converting the byte[] to a string and then, in the MC++ DLL, marshalling that to an IntPtr via StringToHGlobalAnsi, then casting its ToPointer() to a char*, and finally doing a memcpy of the data from char* to the byte* buffer. I guess I was trying to go from a byte[] to a byte*

If someone can point me in the right direction please, or even be so kind as to explain what I need to do? I've been spending so much time researching these newsgroups but I'm just learning bits and pieces here and there. Thanks in advance!!!
Nov 16 '05 #1
1 2396
> I have a byte[] of binary data received from a NetworkStream (C# code)
that I need to pass to the IWMWriter object in a DLL written in Managed
extensions for C++ (since the Windows Media SDK is not usable in C#) whose
WriteSample function takes a byte* parameter.

Do I need to marshal data? I thought I did, so I ran into alot of issues with converting the byte[] to a string and then, in the MC++ DLL,
marshalling that to an IntPtr via StringToHGlobalAnsi, then casting its
ToPointer() to a char*, and finally doing a memcpy of the data from char* to
the byte* buffer. I guess I was trying to go from a byte[] to a byte*.
If someone can point me in the right direction please, or even be so kind

as to explain what I need to do? I've been spending so much time
researching these newsgroups but I'm just learning bits and pieces here and
there. Thanks in advance!!!
Pin the array. That would be the easies't way around it:

Byte buffer __gc[] = yourBufferFromC#Code;
Byte __pin* umBuffer = &buffer[0];

// now pass umBuffer to your unmananged function.

Now, you shouldn't normally do this if you expect the function you're
calling to keep the array for later use or anything, because as soon as the
__pin pointer goes out of scope, the memory will be released, and the next
GC cycle might just move the memory around, causing eventually an access
violation.

If this is your case, then I suggest using Marshal::Copy() to create a copy
of the array into an unmanaged memory buffer.
--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #2

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

Similar topics

0
by: Channing Jones | last post by:
Hello everyone, I am trying to store data in a binary field of an SQL-Server table using ADODB. So far, I have managed to store a record but not any data in the binary field. I only get...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
8
by: TJ | last post by:
I need to be able to pass a pointer to a (managed code) Stream object to a COM method so it can serialize its data into the stream. I have a C# application that makes uses of Randolf Duke's...
1
by: wbaccay | last post by:
I have a byte of binary data received from a NetworkStream (C# code) that I need to pass to the IWMWriter object in a DLL written in Managed extensions for C++ (since the Windows Media SDK is not...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
2
by: Michael Kolias | last post by:
Hello everyone and happy holidays! I have a situation where I want to parse on the fly a stream of binary data. I am developing an httpmodule for asp.net for file uploading. I do not whant to...
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
12
by: Registered User | last post by:
I've read in a book: <quote> With a binary-mode stream, you can't detect the end-of-file by looking for EOF, because a byte of data from a binary stream could have that value, which would...
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
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
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
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,...
1
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
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...

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.